1

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

我为每个键使用此代码:(我知道 media.playEventSound 是一个非常弱的选项,我已经看到一些关于在 Coronalabs 上播放音频的库,如 audio.loadSound 等,但如果可能的话,当然,我'想保留基于“媒体...”的功能)

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 个踏板持续音频文件)和需要按键上的音符字母的按钮。这是我的问题 - 如何将这一切都整合到一个代码中?我的意思是你能写下我在下面发布的这个示例的一个键的代码,但包括我刚才提到的那些功能吗?我真的很想解决这个问题..顺便说一句。我知道 soundTable/fileTable 方法,但是它被称为,但我认为我有足够的时间单独执行每个键 - 或者可能使用 table 方法 - 我只希望它很容易,因为它是我的第一个项目,因此应该是。

对不起我的英语,谢谢!

4

1 回答 1

0

您要求提供更多代码;我在 Corona 论坛上推荐了这个

布尔变量:

local isPedalActive = false

当他们触摸踏板按钮时,将其设置为 true:

isPedalActive = true

然后将其添加到 button_C_press 函数中:

if event.phase == "began" then
  if isPedalActive = true then
    media.playEventSound(cPedal) --assuming you already loaded your audio above
  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-01T13:09:54.173 回答