Runtime event doesnt start in next scene

Hi all,
I have a main file and a scene file(next level). The scene file runs perfectly on its own( if its made as main itself) but when called as a scene, the runtime event listeners in it dont work.

--main file  
local storyboard = require "storyboard"  
local title = display.newImage ("title.png",20, 300)  
title:setFillColor(210,210,255)  
local function starter(event)  
 title:removeEventListener("touch", starter )  
 remover(title) --remover is a function that takes any object and does removeSelf() on it. Works fine.  
 storyboard.gotoScene( "Level3")  
end  
title:addEventListener("touch", starter )  
--Scene file (level3)  
local function stop (event)  
 if event.phase == "ended" then  
 motionx = 0  
 motiony = 0  
  
 end  
  
end  
Runtime:addEventListener("touch", stop )  

The “Runtime:addEventListener(“touch”, stop )” from the scene file doesnt work when the scene is called. Thanx in advance! [import]uid: 176335 topic_id: 33411 reply_id: 333411[/import]

You aren’t loading storyboard in your scene file or using the normal storyboard events. Take a look at the template here: http://docs.coronalabs.com/api/library/storyboard/index.html#overview

Try the following for your scene file (I haven’t tested it, but looks about right):

[code]
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local function stop (event)
if event.phase == “ended” then
motionx = 0
motiony = 0
end
end

function scene:enterScene( event )
–here you can add your runtime event listener
Runtime:addEventListener(“touch”, stop )
end

function scene:exitScene( event )
Runtime:removeEventListener(“touch”, stop )

end

scene:addEventListener( “enterScene”, scene )

scene:addEventListener( “exitScene”, scene )

return scene
[/code] [import]uid: 106799 topic_id: 33411 reply_id: 132751[/import]

You’re looking for a touch event on title, which generates more than one phase. Even though you are removing that event listener inside starter(), I wonder if the ended phase is also showing up there, but after storyboard.gotoScene() has already happened.

My first thought would be to either change the title event listener to “tap” or inside starter() only do the code if event.phase == “began” (or “ended”).

Jay [import]uid: 9440 topic_id: 33411 reply_id: 132788[/import]

Thanx for the response J.

But there was no problem with the main file. It was in the scene(level3) file.
Even when i dont remove the event listener in the starter() in the main, i still get that error in the scene file. Kindly refer to my first post of this series. [import]uid: 176335 topic_id: 33411 reply_id: 132791[/import]

@sherrulz, I think what Jay was trying to point out is the way you have starter() coded it will actually call storyboard.gotoScene() multiple times, meaning your event listener gets added multiple times, though they should also get removed multiple times too. Without seeing level3.lua and how it’s structured, it will be hard to pin it down. Try this to address Jay’s concern

local function starter(event)  
 if event.phase == "ended" then  
 audio.pause(menuTheme)  
 remover(fire)  
 title:removeEventListener("touch", starter )  
 remover(title)  
 storyboard.loadScene("Level3", false)  
 storyboard.gotoScene("Level3", "fade")  
 end  
end  
title:addEventListener("touch", starter )  

Couple of unrelated points… First your remover function should be:

local remover = function (obj)  
 obj:removeSelf()  
 obj = nil  
end  

to avoid memory leaks.

You are also calling “Level3.lua” not “level3.lua”. In the simulator, case doesn’t matter but when you put this on a device, it does matter and you need to make sure Level3.lua has a capital L at the beginning or change your code to a lowercase l if you’re file starts with a lower case l.
[import]uid: 199310 topic_id: 33411 reply_id: 132807[/import]

It’s a bit hard to see what is going on with just your scene extract, but the Runtime call below your stop function is:
Runtime:removeEventListener(“touch”, stop )

You are removing the event listener, not adding - or are you adding in enterScene? [import]uid: 106799 topic_id: 33411 reply_id: 132907[/import]

You aren’t loading storyboard in your scene file or using the normal storyboard events. Take a look at the template here: http://docs.coronalabs.com/api/library/storyboard/index.html#overview

Try the following for your scene file (I haven’t tested it, but looks about right):

[code]
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local function stop (event)
if event.phase == “ended” then
motionx = 0
motiony = 0
end
end

function scene:enterScene( event )
–here you can add your runtime event listener
Runtime:addEventListener(“touch”, stop )
end

function scene:exitScene( event )
Runtime:removeEventListener(“touch”, stop )

end

scene:addEventListener( “enterScene”, scene )

scene:addEventListener( “exitScene”, scene )

return scene
[/code] [import]uid: 106799 topic_id: 33411 reply_id: 132751[/import]

Hi jcborden,
Thanx for the response.

Yes i did as you said, it actually took a while to figure out how to place what, where. But still, it doesnt work. Actually i just posted only the related functions from both my main.lua and level3.lua files.
Its a whole complete game level, containing everything.
My main file only contains an image, a sprite and a “Play” button. While everything else is in the level file.
All OBJ:addEventListener(“touch”, function ) listeners work perfectly, everything else runs fine. Its just that one Runtime event listener which doesnt work. Here are the complete related functions.
This one from the scene

local function stop (event)  
 if event.phase == "ended" then  
 hero:prepare("herohover")  
 hero:play("herohover")  
 motionx = 0  
 motiony = 0  
 firebutt.alpha = 1  
 right.alpha = 1  
 left.alpha = 1  
 up.alpha = 1  
 down.alpha = 1  
 end  
end  
Runtime:addEventListener("touch", stop )  

And this one is my complete main.lua.

require 'sprite'  
local storyboard = require "storyboard"  
local remover = function (obj)  
 obj:removeSelf()  
end  
  
local menuTheme = audio.loadSound("menuTheme.mp3")  
audio.play(menuTheme)  
local firesheet = sprite.newSpriteSheet("spr.png", 800, 450)  
local fireset = sprite.newSpriteSet (firesheet, 1, 17)  
sprite.add (fireset, "burn", 1, 17, 870, 0)  
  
local fire = sprite.newSprite (fireset)  
fire.x = 430  
fire.y = 240  
fire:scale(1.1,1.07)  
fire:prepare("burn")  
fire:play("burn")  
  
local title = display.newImage ("title.png",20, 300)  
title:setFillColor(210,210,255)  
local function starter(event)  
 audio.pause(menuTheme)  
 remover(fire)  
 title:removeEventListener("touch", starter )  
 remover(title)  
 storyboard.loadScene("Level3", false)  
 storyboard.gotoScene("Level3", "fade")  
end  
title:addEventListener("touch", starter )  

Oh and the error screen shows “0: attempt to concatenate a global scene (a nil value)”
[import]uid: 176335 topic_id: 33411 reply_id: 132780[/import]

You’re looking for a touch event on title, which generates more than one phase. Even though you are removing that event listener inside starter(), I wonder if the ended phase is also showing up there, but after storyboard.gotoScene() has already happened.

My first thought would be to either change the title event listener to “tap” or inside starter() only do the code if event.phase == “began” (or “ended”).

Jay [import]uid: 9440 topic_id: 33411 reply_id: 132788[/import]

Thanx for the response J.

But there was no problem with the main file. It was in the scene(level3) file.
Even when i dont remove the event listener in the starter() in the main, i still get that error in the scene file. Kindly refer to my first post of this series. [import]uid: 176335 topic_id: 33411 reply_id: 132791[/import]

@sherrulz, I think what Jay was trying to point out is the way you have starter() coded it will actually call storyboard.gotoScene() multiple times, meaning your event listener gets added multiple times, though they should also get removed multiple times too. Without seeing level3.lua and how it’s structured, it will be hard to pin it down. Try this to address Jay’s concern

local function starter(event)  
 if event.phase == "ended" then  
 audio.pause(menuTheme)  
 remover(fire)  
 title:removeEventListener("touch", starter )  
 remover(title)  
 storyboard.loadScene("Level3", false)  
 storyboard.gotoScene("Level3", "fade")  
 end  
end  
title:addEventListener("touch", starter )  

Couple of unrelated points… First your remover function should be:

local remover = function (obj)  
 obj:removeSelf()  
 obj = nil  
end  

to avoid memory leaks.

You are also calling “Level3.lua” not “level3.lua”. In the simulator, case doesn’t matter but when you put this on a device, it does matter and you need to make sure Level3.lua has a capital L at the beginning or change your code to a lowercase l if you’re file starts with a lower case l.
[import]uid: 199310 topic_id: 33411 reply_id: 132807[/import]

It’s a bit hard to see what is going on with just your scene extract, but the Runtime call below your stop function is:
Runtime:removeEventListener(“touch”, stop )

You are removing the event listener, not adding - or are you adding in enterScene? [import]uid: 106799 topic_id: 33411 reply_id: 132907[/import]

Hi jcborden,
Thanx for the response.

Yes i did as you said, it actually took a while to figure out how to place what, where. But still, it doesnt work. Actually i just posted only the related functions from both my main.lua and level3.lua files.
Its a whole complete game level, containing everything.
My main file only contains an image, a sprite and a “Play” button. While everything else is in the level file.
All OBJ:addEventListener(“touch”, function ) listeners work perfectly, everything else runs fine. Its just that one Runtime event listener which doesnt work. Here are the complete related functions.
This one from the scene

local function stop (event)  
 if event.phase == "ended" then  
 hero:prepare("herohover")  
 hero:play("herohover")  
 motionx = 0  
 motiony = 0  
 firebutt.alpha = 1  
 right.alpha = 1  
 left.alpha = 1  
 up.alpha = 1  
 down.alpha = 1  
 end  
end  
Runtime:addEventListener("touch", stop )  

And this one is my complete main.lua.

require 'sprite'  
local storyboard = require "storyboard"  
local remover = function (obj)  
 obj:removeSelf()  
end  
  
local menuTheme = audio.loadSound("menuTheme.mp3")  
audio.play(menuTheme)  
local firesheet = sprite.newSpriteSheet("spr.png", 800, 450)  
local fireset = sprite.newSpriteSet (firesheet, 1, 17)  
sprite.add (fireset, "burn", 1, 17, 870, 0)  
  
local fire = sprite.newSprite (fireset)  
fire.x = 430  
fire.y = 240  
fire:scale(1.1,1.07)  
fire:prepare("burn")  
fire:play("burn")  
  
local title = display.newImage ("title.png",20, 300)  
title:setFillColor(210,210,255)  
local function starter(event)  
 audio.pause(menuTheme)  
 remover(fire)  
 title:removeEventListener("touch", starter )  
 remover(title)  
 storyboard.loadScene("Level3", false)  
 storyboard.gotoScene("Level3", "fade")  
end  
title:addEventListener("touch", starter )  

Oh and the error screen shows “0: attempt to concatenate a global scene (a nil value)”
[import]uid: 176335 topic_id: 33411 reply_id: 132780[/import]

Rob Miracle,

You’re a miracle indeed :stuck_out_tongue:

That worked!
So it was just being called multiple times coz of “touch” ?
I did change it to “tap” too but that didnt work either.
Just the phase ended fixed it?
how ?
And thankyou, everyone for the responses and help. If someday im able to finish this game, i’l remember to credit you all.
Cheers! [import]uid: 176335 topic_id: 33411 reply_id: 133332[/import]

Rob Miracle,

You’re a miracle indeed :stuck_out_tongue:

That worked!
So it was just being called multiple times coz of “touch” ?
I did change it to “tap” too but that didnt work either.
Just the phase ended fixed it?
how ?
And thankyou, everyone for the responses and help. If someday im able to finish this game, i’l remember to credit you all.
Cheers! [import]uid: 176335 topic_id: 33411 reply_id: 133332[/import]