当我尝试在我的应用中播放音频时,我收到“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)
怎么了?
答案 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