Corona SDK 钢琴应用 - 切换音效等

所以基本上我正在用 Corona SDK 制作钢琴应用(我的第一个项目),我是新手。我在 Corona 论坛上提出了一些关于我的问题的问题,但是我没有得到确切的答案,所以我要求你的帮助。正如我所说,我是新手,因此可能很难为我破译所需的代码,但我知道您这些更有经验的 Corona 用户可以轻松做到这一点。

对于每个键,我使用此代码:(我知道 media.playEventSound 是一个相当弱的选项,我已经看到了一些库,如 Coronalabs 上的 audio.loadSound 等用于播放音频,但如果可能的话,我当然想使用“media ...”为基础的函数)

local widget = require("widget")
local C      = media.newEventSound("C.mp3")

local button_C_Press = function(event)
  media.playEventSound(C, button_C_Press)
end

local button_C = widget.newButton
{
  defaultFile = "NewKey.png",
  overFile    = "NewKey2.png",
  onPress     = button_C_Press,
}
button_C.x = 20; button_C.y = 295

我希望钢琴有 2 个踏板,仅在按下时切换其声音(我在项目文件夹中总共有 3 种不同的声音配置 - 默认和 2 个踏板持续音频文件),并且需要在键上显 示音符字母的按钮。 这是我的问题 - 如何将所有内容都放入一个代码中? 我的意思是,您能否为我编写一个像下面这个示例的一键代码,但包括我刚才提到的那些功能?我真的很想解决这个问题。 Btw。我知道 soundTable / fileTable 方法,不管它是叫什么,但我认为我有足够的时间逐个处理所有键 - 或者使用表方法 - 我只希望它很容易,因为这是我的第一个项目,因此应该。

对不起我的英语,谢谢!

点赞
用户8701476
用户8701476

你要求更多的代码;在Corona论坛上我推荐这样做。

布尔变量:

local isPedalActive = false

当他们点击踏板按钮时,设置为true:

isPedalActive = true

然后将其添加到button_C_press功能中:

if event.phase == "began" then
  if isPedalActive = true then
    media.playEventSound(cPedal) --假设你已经在上面加载了音频
  end
end

当然,如果你有大量的钢琴键,最好不要为每个函数分别进行操作,而是:

1.为widget.newButton表中的每个键设置特定的id。

2.在if语句中,在那里加载声音,但是您将检索按钮的id并播放该mp3文件。

(只支持一个踏板)

 --create table of key button ids and mp3 files for their pedal noises
local keys = {
  {buttonId = "C", pedalNoise = "Cpedal.mp3"},
  {buttonId = "D", pedalNoise = "Dpedal.mp3"}
}

function pianoKeys(event)
  for i = 1, #keys do -- for each table in the keys table, load the sound for each key
    local keySound = media.newEventSound(keys[i].buttonId .. ".mp3") -- normal sound loaded
    local keypedalSound = media.newEventSound(keys[i].pedalNoise) --pedal sound loaded
    function buttonPress(event)  --When they press the key, detect if the pedal is active
      if event.phase == "began" then
        if isPedalActive == true then
          media.playEventSound(keyPedalSound) --is active, play pedal sound
        else
          media.playEventSound(keySound) -- is not active, play regular sound
        end
      end
    end
    local pianoKey = widget.newButton({
      id = keys[i].buttonId, -- place appropriate id
      defaultFile = "new" .. keys[i].buttonId .. "key.png", -- place appropriate defaultFile
      overFile = "new" .. keys[i].buttonId .. "key2.png", -- place appropriate overFile
      onPress = buttonPress -- apply above function to each key
    })
  end
end

我的问题 - 我不想制作声音表。我宁愿单独处理每个键。像我下面发布的一个键的代码示例。但是如何做到?我不知道如何将一切放入一个有效的东西中:/(2个踏板+音符按钮)

2017-10-01 13:09:54