xflea
xflea

Reputation: 177

audio.play not working

I get an "upvalue error" while I try to play audio in my app. I have 2 files:

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

At launch I get this error:

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

What's wrong?

Upvotes: 0

Views: 318

Answers (1)

ldurniat
ldurniat

Reputation: 1702

Look carefully what you return in sound_board.lua file. Error message tells that local variable sound_board in level.lua is a boolean value.

To get access to variables from another file use modules like that:

-- 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

and

-- level1.lua

local sound_board = require( "sound_board" )

-- some code
function fireSinglebullet()

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

    -- some other code
end 

Read more: External Modules in Corona

Upvotes: 2

Related Questions