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]