In my main.lua I have the following code:
sounds = {}
 
sounds.engine = Sound.new("audio/engine.mp3")
 
 
sounds.on = function()
	sets.sounds = true
	dataSaver.saveValue("sets", sets)
end
 
sounds.off = function()
	sets.sounds = false
	dataSaver.saveValue("sets", sets)
end
 
sounds.play = function(sound)
if sets.sounds and sounds[sound] then
 
		print("playing sound")
 
		soundChannel1 = sounds[sound]:play(0, true, false)
	end
end | 
 
I'm able to use "soundChannel1" in my gamescene.lua:
function gamescene:init()
 
 self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
 
	local function onTouchesBegin(event)
 
 
mousedown = true
print("mousedown")
if fuel > 1 and success == false and failure == false and escaped == false and paused == false then
sounds.play("engine")
end
 
 
	end
 
 
	local function onTouchesMove(event)
 
 
 
	end
 
 
	local function onTouchesEnd(event)
 
mousedown = false
print("mouseup")
soundChannel1:stop()
 
	end
 
 
	local function onTouchesCancel(event)
 
mousedown = false
print("mouseup")
soundChannel1:stop()
 
	end
 
	self:addEventListener(Event.TOUCHES_BEGIN, onTouchesBegin)
	self:addEventListener(Event.TOUCHES_MOVE, onTouchesMove)
	self:addEventListener(Event.TOUCHES_END, onTouchesEnd)
self:addEventListener(Event.TOUCHES_CANCEL, onTouchesCancel)
 
end | 
 
However, I also need to use the stopping function inside the onEnterFrame of gamescene class.
 
 
function gamescene:onEnterFrame()
if fuel < 1 or mousedown == false then
	soundChannel1:stop()  | 
 
I get the error: 
gamescene.lua:698: attempt to index field 'soundChannel1' (a nil value)
stack traceback:
	gamescene.lua:698: in function 
Any ideas on how could I get around this?                
 
                
Comments
Likes: Karriz
I fixed it like this:
if mousedown == false then
--other stuff
elseif fuel < 1 then
--other stuff
soundChannel1:stop()
end