Dead end for me, have been trying for hours..

Hi everyone!

This should be easy, and I hate to bother for help, but after trying to do this for hours, I realize that I just canät make it work…

I’m using the standard storyboard templete, where I have a meny screen with two buttons, one for level1 and one for level2. The two levels are pretty much the same, they just contain “enemies” at differend location and small overall tweaks to the gameplay physics.

From the meny I get to the level 1… But when the character “dies” I want to go back to the menu, and from there be able to restart the level or choose level2…

Just don’t know how to do this properly… Probably because I haven’t removed all the elements in a proper way…

I would so much appreciate some help. I’ve been reading everything I’ve found, but just get more confused… I have my gameplay working the way I want now, but being stuck after the character dies just put an end to my development…

Here the important parts of my code…

First, MENU.LUA

[lua]-----------------------------------------------------------------------------------------

– menu.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

– include Corona’s “widget” library
local widget = require “widget”


– forward declarations and other locals
local playBtn
local playBtn2

– ‘onRelease’ event listener for playBtn
local function onPlayBtnRelease()

– go to level1.lua scene
storyboard.gotoScene( “level1”, “fade”, 500 )

return true – indicates successful touch
end

local function onPlayBtn2Release()

– go to level2.lua scene
storyboard.gotoScene( “level2”, “fade”, 500 )

return true – indicates successful touch
end


– BEGINNING OF YOUR IMPLEMENTATION

– NOTE: Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.


– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

– display a background image
local background = display.newImageRect( “wood.PNG”, display.contentWidth, display.contentHeight )
background:setReferencePoint( display.TopLeftReferencePoint )
background.x, background.y = 0, 0

– create/position logo/title image on upper-half of the screen
local titleLogo = display.newImageRect( “logga.png”, 264, 62 )
titleLogo:setReferencePoint( display.CenterReferencePoint )
titleLogo.x = display.contentWidth * 0.5
titleLogo.y = 100

– create a widget button (which will loads level1.lua on release)
playBtn = widget.newButton{
label=“LEVEL 1 '”,
labelColor = { default={128}, over={85} },
default=“playbutton1.png”,
over=“playbutton2.png”,
width=158, height=45,
onRelease = onPlayBtnRelease – event listener function
}
playBtn:setReferencePoint( display.CenterReferencePoint )
playBtn.x = display.contentWidth*0.5
playBtn.y = display.contentHeight - 125

– create a widget button (which will loads level1.lua on release)
playBtn2 = widget.newButton{
label=“LEVEL 2 '”,
labelColor = { default={128}, over={85} },
default=“playbutton1.png”,
over=“playbutton2.png”,
width=158, height=45,
onRelease = onPlayBtn2Release – event listener function
}
playBtn2:setReferencePoint( display.CenterReferencePoint )
playBtn2.x = display.contentWidth*0.5
playBtn2.y = display.contentHeight - 70

– all display objects must be inserted into group
group:insert( background )
group:insert( titleLogo )
group:insert( playBtn )
group:insert( playBtn2 )
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view

– INSERT code here (e.g. start timers, load audio, start listeners, etc.)

end

– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view

– INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view

if playBtn then
playBtn:removeSelf() – widgets must be manually removed
playBtn = nil
end

if playBtn2 then
playBtn2:removeSelf() – widgets must be manually removed
playBtn2 = nil
end

end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene[/lua]
Nothing wrong there…

Then, LEVEL1.LUA (i’ve have lots more objects, but if I would figure out how to solve this with only the code below I will be able to solve everything…

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

local physics = require “physics”
physics.start(); physics.pause()
physics.setGravity( 0, 25 )

local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
function scene:createScene( event )
local group = self.view
– display a background image
local background = display.newImageRect( “wood.PNG”, display.contentWidth, display.contentHeight )
background:setReferencePoint( display.TopLeftReferencePoint )
background.x, background.y = 0, 0

local stick = display.newRect(240, 200, 20, 40)

physics.addBody( stick, { density=3.0, friction=0.0, bounce=1.0 } )

local obstr = display.newRoundedRect(-100,80, 80, 30,5)
physics.addBody( obstr, “kinematic”, { density=1000.0, friction=0.0, bounce=3.0 } )
obstr:setLinearVelocity(350,0)

local obstl = display.newRoundedRect(800, 50, 50, 180,5)
physics.addBody( obstl, “kinematic”, { density=1000.0, friction=0.0, bounce=3.0 } )
obstl:setLinearVelocity(-100,0) --eg -120

– create a FLOOR
local fbox = display.newRoundedRect(100, 240,300, 5,3)
physics.addBody( fbox, “static”, { density=100.0, friction=1.0, bounce=0.0 } )
– create a ROOF object and add physics (with custom shape)
local grass2 = display.newImageRect( “woodfloor.png”, screenW, 35 )
grass2.x, grass2.y = 240, 1

physics.addBody( grass2, “static”, { friction=0.3, shape=grassShape2 } )

local function onLocalCollision(self, event )
if ( event.phase == “began” ) then
canjump = “true”

elseif ( event.phase == “ended” ) then
canjump = “false”

end
end

stick.collision = onLocalCollision
local function died(event)
if stick.x < 190 then
Runtime:removeEventListener(“enterFrame”,died)
transition.to( stick, { time=500, alpha=0.0, } )
scene:exitScene()
storyboard.gotoScene( “menu”, “fade”, 500 )
end

if stick.x > 320 then
Runtime:removeEventListener(“enterFrame”,died)
transition.to( stick, { time=500, alpha=0.0, } )
scene:exitScene()
storyboard.gotoScene( “menu”, “fade”, 500 )
end

end

Runtime:addEventListener(“enterFrame”,died)
local xpos = “165”
local ypos = “260”
local wid = “30”
local hig = “40”

local touchObjects = { “t1”, “t2”, “t3”,“t4”,“t5” }
local yForce = { “-950”, “-900”, “-860”, “-900”,"-950"}
local torque = { “-57”, “-20”, “0”,“20”,“57”}
local addx = { “0”, “32”, “64”,“96”,“128” }
local addy = { “0”, “0”, “0”,“0”,“0”,}
local jsound = { “axe”, “axe”, “axe”,“axe”,“axe”}

for i=1,#touchObjects do
touchObjects[i] = display.newRect( (xpos + (addx[i])),(ypos + (addy[i])), (wid), (hig))
touchObjects[i].xForce = 0 ; touchObjects[i].yForce = (yForce[i])
touchObjects[i].torque = (torque[i])
touchObjects[i].alpha = 0.1
touchObjects[1].isHitTestable = true
touchObjects[i].jsound = (jsound[i])
end

local function onTouch( event )

local touched = event.target --event.target is the circle touched
if ( canjump == “true” and event.phase == “began” ) then
stick:applyForce( touched.xForce, touched.yForce, stick.x, stick.y )
stick:applyTorque( touched.torque )
print(touched.jsound)
sound = audio.loadSound(touched.jsound … “.wav”)
audio.play(sound)
end

end

–apply listeners using a loop
for i=1,#touchObjects do
touchObjects[i]:addEventListener ( “touch”, onTouch )
–you can, and must, eventually remove the listeners in a similar way, on scene cleanup
end

stick: addEventListener ( “collision”, stick )

– all display objects must be inserted into group
group:insert( background )
group:insert( fbox)
group:insert( stick )
group:insert( obstr )
group:insert( obstl )

end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view

physics.start()

end

– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view

physics.stop()
removeEventListener(“enterFrame”,died)

for i=1,#touchObjects do
touchObjects[i]:removeEventListener ( “touch”, onTouch )
end

storyboard.gotoScene( “menu”, “fade”, 500 )

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view

package.loaded[physics] = nil
physics = nil

end

– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().

return scene[/lua]
This is probably not even close to be working, I know… But I’ve been trying lots of different ways of doing it, without success…

Oh would I want some help!!

Thank so much! [import]uid: 187595 topic_id: 32570 reply_id: 332570[/import]

Showing so much code is probably a bad idea; the number one method to fix bad bugs is to scale back your code so much that you know exactly where it’s going wrong. (Also makes it a lot easier to get replies…) Secondly, you haven’t said what the error is, so that makes it even harder to tell what you’re doing wrong.

That being said I feel like coding right now so let’s take a swing at it…

  1. With the died() function, keep in mind that code after your transition will execute IMMEDIATELY, and will not wait for the transition to end. If you want it to happen after the transition (a good idea, since you should be cancelling all active transitions manually when exiting a scene) you should do it in one of two ways:

[code]-- Seperate method
local function finish()
– do your exit stuff here
end

transition.to(stick, {time=500, alpha=0, onComplete=finish} )[/code]

[code]-- Combined method
transition.to(stick, {time=500, alpha=0, onComplete=function()
– do stuff, just end each line with ;
end} )

– Example
transition.to(banana, {time=500, alpha=0, onComplete=function() eatBanana(); banana.peel = 1 end})[/code]

  1. No idea why you’re manually calling exitScene(). It should call automatically when you go to the new Scene. It’s possible you’re forcing it to call a second time as a result?

  2. You’re calling a gotoScene in exitScene, again not needed and possibly harmful. You should simply be calling gotoScene in your code and it will call things like exit on your behalf.

Hope this helps [import]uid: 41884 topic_id: 32570 reply_id: 129478[/import]

Hi!

Thanks you for your input! I know it’s a lot of code, and perhaps it gets overwhelming to look at…

Hmm yes as I said it’s probably not even close to be working… But if i just from within the died() function do

storyboard.gotoScene( “menu”, “fade”, 500 )

That wont work… I need to put in some clean-up code as well right? But where? And how? Mainly the TouchObjects loop that created touch handlers… How to remove those…

Thank you so much!

[import]uid: 187595 topic_id: 32570 reply_id: 129480[/import]

Try this little snippet in menu.lua under ‘function scene:enterScene( event )’ - it should reset level1 once you go from level1 to the menu.

Care to give us the printout from your terminal when your game crashes?

[lua]-- get the name of the last active scene
local lastScene = storyboard.getPrevious()

– remove the last scene when last scene was level1
if lastScene == ‘level1’ then
storyboard.removeScene(lastScene)
end[/lua] [import]uid: 62706 topic_id: 32570 reply_id: 129487[/import]

Hi!

Well, that almost works, except, the objects from scene 1 are still visible in the menu screen, and will stay there even while going back into level1. So I suppose I need to have those removed somehow?

For example, this objects;

[lua]for i=1,#touchObjects do
touchObjects[i] = display.newRect( (xpos + (addx[i])),(ypos + (addy[i])), (wid), (hig))
touchObjects[i].xForce = 0 ; touchObjects[i].yForce = (yForce[i])
touchObjects[i].torque = (torque[i])
touchObjects[i].alpha = 0.1
touchObjects[1].isHitTestable = true
touchObjects[i].jsound = (jsound[i])
end

–apply listeners using a loop
for i=1,#touchObjects do
touchObjects[i]:addEventListener ( “touch”, onTouch )
–you can, and must, eventually remove the listeners in a similar way, on scene cleanup
end[/lua]

How to remove them?

Thanks so much! [import]uid: 187595 topic_id: 32570 reply_id: 129491[/import]

you have to add all your display objects to storyboards view otherwise they will not be removed with the scene.
something like this:

[lua]for i=1,#touchObjects do
touchObjects[i] = display.newRect( (xpos + (addx[i])),(ypos + (addy[i])), (wid), (hig))
touchObjects[i].xForce = 0 ; touchObjects[i].yForce = (yForce[i])
touchObjects[i].torque = (torque[i])
touchObjects[i].alpha = 0.1
touchObjects[1].isHitTestable = true
touchObjects[i].jsound = (jsound[i])
group:insert ( touchObjects[i] ) – add this!!!
end [import]uid: 13632 topic_id: 32570 reply_id: 129506[/import]

oh la la, there we go, I didn’t think of that! Thanks a lot, now I’m much closer getting this to work!

How about the touch handlers? Where should I put the code to remove them? I have tried several alternatives.

Putting this within the function scene:exitScene( event ) will not work… Why is that?

[lua]for i=1,#touchObjects do
touchObjects[i]:removeEventListener ( “touch”, onTouch )
end[/lua]

again, many thanks! [import]uid: 187595 topic_id: 32570 reply_id: 129508[/import]

a. exitScene is called by storyboard itself when you exit the scene; same with enterScene, createScene, etc - these scene functions are all called automatically based on the context of the situation. So when you gotoScene() it calls exitScene on the way out.

b. touchObjects{} needs to be declared outside of your scene functions. You made it a local within createScene (I think?) so you can’t magically detect it anywhere else unless you pre-declare the variable outside. [import]uid: 41884 topic_id: 32570 reply_id: 129516[/import]

aha! Your point b is what Iäve totally missed! All code Iäve done have always been inside the scene functions… Going to give this some try now!

Thank you so much! [import]uid: 187595 topic_id: 32570 reply_id: 129520[/import]

Showing so much code is probably a bad idea; the number one method to fix bad bugs is to scale back your code so much that you know exactly where it’s going wrong. (Also makes it a lot easier to get replies…) Secondly, you haven’t said what the error is, so that makes it even harder to tell what you’re doing wrong.

That being said I feel like coding right now so let’s take a swing at it…

  1. With the died() function, keep in mind that code after your transition will execute IMMEDIATELY, and will not wait for the transition to end. If you want it to happen after the transition (a good idea, since you should be cancelling all active transitions manually when exiting a scene) you should do it in one of two ways:

[code]-- Seperate method
local function finish()
– do your exit stuff here
end

transition.to(stick, {time=500, alpha=0, onComplete=finish} )[/code]

[code]-- Combined method
transition.to(stick, {time=500, alpha=0, onComplete=function()
– do stuff, just end each line with ;
end} )

– Example
transition.to(banana, {time=500, alpha=0, onComplete=function() eatBanana(); banana.peel = 1 end})[/code]

  1. No idea why you’re manually calling exitScene(). It should call automatically when you go to the new Scene. It’s possible you’re forcing it to call a second time as a result?

  2. You’re calling a gotoScene in exitScene, again not needed and possibly harmful. You should simply be calling gotoScene in your code and it will call things like exit on your behalf.

Hope this helps [import]uid: 41884 topic_id: 32570 reply_id: 129478[/import]

Hi!

Thanks you for your input! I know it’s a lot of code, and perhaps it gets overwhelming to look at…

Hmm yes as I said it’s probably not even close to be working… But if i just from within the died() function do

storyboard.gotoScene( “menu”, “fade”, 500 )

That wont work… I need to put in some clean-up code as well right? But where? And how? Mainly the TouchObjects loop that created touch handlers… How to remove those…

Thank you so much!

[import]uid: 187595 topic_id: 32570 reply_id: 129480[/import]

Try this little snippet in menu.lua under ‘function scene:enterScene( event )’ - it should reset level1 once you go from level1 to the menu.

Care to give us the printout from your terminal when your game crashes?

[lua]-- get the name of the last active scene
local lastScene = storyboard.getPrevious()

– remove the last scene when last scene was level1
if lastScene == ‘level1’ then
storyboard.removeScene(lastScene)
end[/lua] [import]uid: 62706 topic_id: 32570 reply_id: 129487[/import]

Hi!

Well, that almost works, except, the objects from scene 1 are still visible in the menu screen, and will stay there even while going back into level1. So I suppose I need to have those removed somehow?

For example, this objects;

[lua]for i=1,#touchObjects do
touchObjects[i] = display.newRect( (xpos + (addx[i])),(ypos + (addy[i])), (wid), (hig))
touchObjects[i].xForce = 0 ; touchObjects[i].yForce = (yForce[i])
touchObjects[i].torque = (torque[i])
touchObjects[i].alpha = 0.1
touchObjects[1].isHitTestable = true
touchObjects[i].jsound = (jsound[i])
end

–apply listeners using a loop
for i=1,#touchObjects do
touchObjects[i]:addEventListener ( “touch”, onTouch )
–you can, and must, eventually remove the listeners in a similar way, on scene cleanup
end[/lua]

How to remove them?

Thanks so much! [import]uid: 187595 topic_id: 32570 reply_id: 129491[/import]

you have to add all your display objects to storyboards view otherwise they will not be removed with the scene.
something like this:

[lua]for i=1,#touchObjects do
touchObjects[i] = display.newRect( (xpos + (addx[i])),(ypos + (addy[i])), (wid), (hig))
touchObjects[i].xForce = 0 ; touchObjects[i].yForce = (yForce[i])
touchObjects[i].torque = (torque[i])
touchObjects[i].alpha = 0.1
touchObjects[1].isHitTestable = true
touchObjects[i].jsound = (jsound[i])
group:insert ( touchObjects[i] ) – add this!!!
end [import]uid: 13632 topic_id: 32570 reply_id: 129506[/import]

oh la la, there we go, I didn’t think of that! Thanks a lot, now I’m much closer getting this to work!

How about the touch handlers? Where should I put the code to remove them? I have tried several alternatives.

Putting this within the function scene:exitScene( event ) will not work… Why is that?

[lua]for i=1,#touchObjects do
touchObjects[i]:removeEventListener ( “touch”, onTouch )
end[/lua]

again, many thanks! [import]uid: 187595 topic_id: 32570 reply_id: 129508[/import]

a. exitScene is called by storyboard itself when you exit the scene; same with enterScene, createScene, etc - these scene functions are all called automatically based on the context of the situation. So when you gotoScene() it calls exitScene on the way out.

b. touchObjects{} needs to be declared outside of your scene functions. You made it a local within createScene (I think?) so you can’t magically detect it anywhere else unless you pre-declare the variable outside. [import]uid: 41884 topic_id: 32570 reply_id: 129516[/import]

aha! Your point b is what Iäve totally missed! All code Iäve done have always been inside the scene functions… Going to give this some try now!

Thank you so much! [import]uid: 187595 topic_id: 32570 reply_id: 129520[/import]