audio.play无效

时间:2018-05-27 15:15:28

标签: lua corona

当我尝试在我的应用中播放音频时,我收到“upvalue错误”。 我有2个文件:

sound_board.lua

local enemy_damaged = audio.loadSound( "assets/audio/enemy_damaged.wav" )
local ouch = audio.loadSound( "assets/audio/ouch.wav" )
local pew = audio.loadSound( "assets/audio/pew.wav" )

local function playSound(to_play)
    audio.play( to_play )
end

level1.lua

local sound_board = require("sound_board")

-- some code
function fireSinglebullet()
    sound_board:playSound(pew) -- line 295

    -- some other code
end

发布时我收到此错误:

level1.lua:295: attempt to index upvalue 'sound_board' (a boolean value)

怎么了?

1 个答案:

答案 0 :(得分:2)

仔细查看您在sound_board.lua文件中返回的内容。错误消息告诉sound_board中的局部变量level.lua是布尔值。

要从另一个文件访问变量,请使用以下模块:

-- sound_board.lua

local M = {}

M.sounds = { 
  "enemy_damaged" = audio.loadSound( "assets/audio/enemy_damaged.wav" )
  "ouch" = audio.loadSound( "assets/audio/ouch.wav" )
  "pew" = audio.loadSound( "assets/audio/pew.wav" )
}

function M:playSound( to_play )

    audio.play( self.sounds[to_play] )

end

return M

-- level1.lua

local sound_board = require( "sound_board" )

-- some code
function fireSinglebullet()

    sound_board:playSound( "pew" ) -- line 295

    -- some other code
end 

了解详情:External Modules in Corona