Composor

Hi, i wish convert below code to composor and i manage to do so but the image still on the screen after i have change to a new screen. Anyone can teach me how to solve it? TQ.

original code

-- 2013 by Christian Peeters -- christianpeeters.com for complete tutorial -- general stuff display.setStatusBar(display.HiddenStatusBar) local cWidth = display.contentCenterX local cHeight = display.contentCenterY -- global variables local createGame local touchCar local createCar local stopGame local hitWall local score = 0 local timeLimit =3 local scorelabel local wall local car local carArray local BLUE\_CAR = 1 -- loaded audio local crash = audio.loadSound ("crash.mp3") local brake = audio.loadSound ("break.mp3") -- Create titleScreen local function createTitleScreen() local titlescreen = display.newImage("titlescreen.png") local startgametext = display.newText("Click here to start", 0 , 0, "Helvetica" , 24 ) startgametext.x = cWidth startgametext.y = cHeight +100 startgametext:setTextColor(0,0,0) local function startPressed(event) display.remove(event.target) display.remove(titlescreen) startgametext = nil createGame() end startgametext:addEventListener ( "tap", startPressed ) end -- GameMethods function createGame() local gamebackground = display.newImage("gamebackground.png") wall = display.newImage("wall.png") wall.x = display.contentWidth local backbutton = display.newImage("back.png") backbutton.xScale = 0.4; backbutton.yScale = 0.4 backbutton.x = 40 backbutton.y = 40 function stopGame() car:removeSelf() car = nil createTitleScreen() end backbutton:addEventListener ( "tap", stopGame ) scorelabel = display.newText( "Score: 0", 0, 0, "Helvetica", 22 ) scorelabel.x = display.contentWidth - 75 score = 0 timeLimit = 3 wall.crashnumber = 3 timeLeft = display.newText(timeLimit, cWidth-10, cHeight-30, "Helvetica", 50) local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then createCar() timeLeft.isVisible = false --timeLimit:removeSelf() timeLimit = nil end end timer.performWithDelay(1000,timerDown,timeLimit) end function createCar() carArray = { "carblue.png", "cargreen.png", "carred.png", "caryellow.png" } carType = math.random (#carArray ) car = display.newImage( carArray [carType] ) --car = display.newImage( carArray [math.random (#carArray )] ) car.x = -200 car.y = math.random (0, display.contentHeight) car.xScale = 0.2 car.yScale = 0.2 car:rotate (90) car.type = carType car.moves = transition.to (car, {time = 3000, x=display.contentWidth-30 , y=math.random(0,display.contentHeight), onComplete = hitWall } ) car:addEventListener ( "tap", touchCar ) return car end function touchCar(event) local cartouched = event.target transition.cancel ( event.target.moves ) audio.play(brake) cartouched:rotate (370) if cartouched.type == BLUE\_CAR then score = score + 15 else score = score +5 end scorelabel.text = "Score: ".. score local function removeCar() display.remove(cartouched) end timer.performWithDelay(600, removeCar) createCar() end function hitWall(obj) local function resetWall() wall.xScale =1 wall.yScale = 1 local function removeCar() display.remove(obj) end timer.performWithDelay ( 1200, removeCar ) end car.xScale =0.22 car.yScale = 0.22 car:rotate (math.random (-90, 90)) lifeCount() audio.play(crash) transition.to ( wall, { time=200, xScale=1.1, yScale=1.1, alpha=1, onComplete = resetWall } ) if wall.crashnumber \> 0 then createCar() end end function lifeCount() wall.crashnumber = wall.crashnumber - 1 if wall.crashnumber \<1 then gameovertext = display.newText("Game Over", cWidth-80, cHeight, "Helvetica", 24) timer.performWithDelay ( 1500, createTitleScreen ) end end createTitleScreen()

my code on composor

--------------------------------------------------------------------------------- -- -- scene2.lua -- --------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() local hitWall local score = 0 local timeLimit =3 local car local function homeButtonTouch( self, event ) if event.phase == "ended" then function stopGame() car:removeSelf() car = nil end gameovertext.isVisible=false composer.gotoScene( "menu", "fade", 400 ) return true end end function stopGame() car:removeSelf() car = nil end function touchCar(event) local cartouched = event.target transition.cancel ( event.target.moves ) audio.play(brake) cartouched:rotate (100) if cartouched.type == BLUE\_CAR then score = score + 15 else score = score +5 end scorelabel.text = "Score: ".. score local function removeCar() display.remove(cartouched) end timer.performWithDelay(100, removeCar) createCar() end function hitWall(obj) local function resetWall() wall.xScale =1 wall.yScale = 1 local function removeCar() display.remove(obj) end timer.performWithDelay ( 1200, removeCar ) end car.xScale =0.22 car.yScale = 0.22 car:rotate (math.random (-90, 90)) lifeCount() audio.play(crash) transition.to ( wall, { time=200, xScale=1.1, yScale=1.1, alpha=1, onComplete = resetWall } ) if wall.crashnumber \> 0 then createCar() end end function lifeCount() wall.crashnumber = wall.crashnumber - 1 if wall.crashnumber \<1 then gameovertext.isVisible=true timer.performWithDelay ( 1500, createTitleScreen ) end end local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then createCar() timeLeft.isVisible = false --timeLimit:removeSelf() timeLimit = nil end end function scene:create( event ) local sceneGroup = self.view image = display.newImage( "background/gamebackground.png" ) image.x = 515 image.y = 385 sceneGroup:insert( image ) image.touch = onSceneTouch wall = display.newImage("background/wall.png") wall.x = 1000 wall.y = 385 sceneGroup:insert( wall ) text1 = display.newText( "try", 0, 0, native.systemFontBold, 24 ) text1:setFillColor( 255 ) text1.x, text1.y = display.contentWidth \* 0.5, 50 sceneGroup:insert( text1 ) homeButton = display.newImage( "button/home.png" ) homeButton.x = 980 homeButton.y = 50 sceneGroup:insert( homeButton ) homeButton.touch = homeButtonTouch scorelabel = display.newText( "Score: 0", 0, 0, "Helvetica", 30 ) scorelabel.x = 50 scorelabel.y = 50 sceneGroup:insert( scorelabel ) score = 0 timeLimit = 3 wall.crashnumber = 3 gameovertext = display.newText("Game Over", 500, 500, "Helvetica", 24) gameovertext.isVisible=false timeLeft = display.newText(timeLimit, 500, 500, "Helvetica", 50) sceneGroup:insert( timeLeft ) function createCar() carArray = { "images/carblue.png", "images/cargreen.png", "images/carred.png", "images/caryellow.png" } carType = math.random (#carArray ) car = display.newImage( carArray [carType] ) --car = display.newImage( carArray [math.random (#carArray )] ) car.x = 50 car.y = math.random (0, display.contentHeight) car.isVisible=true car.xScale = 0.2 car.yScale = 0.2 car:rotate (90) car.type = carType car.moves = transition.to (car, {time = 3000, x=display.contentWidth-30 , y=math.random(0,display.contentHeight), onComplete = hitWall } ) sceneGroup:insert( car ) car:addEventListener ( "tap", touchCar ) car:removeEventListener ( "tap", car ) return car end timer.performWithDelay(1000,timerDown,timeLimit) print( "\n2: create event" ) end function scene:show( event ) local phase = event.phase if "did" == phase then print( "2: show event, phase did" ) -- remove previous scene's view -- Update Lua memory text display homeButton:addEventListener( "touch", homeButton ) end end function scene:hide( event ) local phase = event.phase if "will" == phase then print( "2: hide event, phase will" ) -- remove touch listener for image homeButton:removeEventListener( "touch", homeButton ) end end function scene:destroy( event ) print( "((destroying scene 2's view))" ) end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

Did you figure this out? It’s a lot of code to read, which is why I think you haven’t had a response yet. Which image stays on the screen?

Hi,harocebury. The image of the car and the game doesn’t stop when i move to another screen. TQ.

I think you have something, probably a transition, holding onto a reference to the car image after the scene is shifted off screen. You also have a global (well, local to the module) reference to the car image.

Check that the transitions are all complete before allowing the scene to end. You can also try cancelling them.

Which car image stays on the screen? I notice you have .

now the car image doesn’t stays on the screen when i move out from the screen but yet the game doesn’t stop. I not so sure whether is the  transition problem or not and i how to edit in order to make it stop after i move out from the screen. Please help me

I think you should clean your code and maybe try with smaller code. That will make it easier to identify what is going on.

What do you mean by “game doesn’t stop”?

I have try to run part by part, but when it come to function createCar(), things all go wrong.

“game doesn’t stop” mean the game still carry on when i move out of the screen.

just a very quick look at the code and a guess on my part, but I would ‘rem-out’ the createCar() call in the timerDown function. Run the code and see if the car still is on screen when changing scenes.  If not, then that function is where the problem lies… that timer may still be running when a scene ends(just guessing), and is creating a car post scene somehow.

I don’t have a lot of time to really look the code over right now.  If the issue is not intuit function I can try and look at the code a little closer later tonight.

Good luck

I have try to remove the createCar() call  in the timerDown function but yet the game still carry on

I read some of the other responses above…  so the initial issue ‘car’ stays visible on screen and game does not stop; the car according to the responses above is no longer the issue, just the ‘game doesn’t stop’ is apparently the remaining issue. Is that correct?

Can you post a more detailed explanation of ‘game doesn’t stop’  or  ‘the game still carry on when i move out of the screen’?  Do you mean there are some images (other then the car), such as background or some other images that are still on the screen?

ok, the game is a racing game. When i move to the screen, the game started to run and play. While, when i move out of the screen (changing to menu screen or others ), the game suppose to stop and the game will reload if i move back to the game. Yet, the game still continue to run even i move to other screen and the game doesn’t reload when i move back to screen. 

The game is ok when i just using the original code but when move to composer it have  this kind of problem.

chyman,

I have a quick fix for you. (keep in mind i am not that experienced with composer myself). So there are probably better(more efficient ) ways to do this. But, this will work.

  1. When you leave scene2, let’s say you go to menu scene;  in the menuScene  ‘scene:show’  insert this line of code :

composer.removeScene(“scene2”)

  1. In scene2, the ‘scene:destroy’ function, be sure to cancel all timers and transitions.  This is a little tricky since you need to make sure that none of them are ‘nil’ if you attempt to cancel them.  It looks like you have 4 timerPerformWithDelay calls, and at least one transiton.to call.  Cancel them all. You may have to do some if checks like so:

if timer1 ~= nil then

   timer.cancel(timer1)

end

* you will also have to assign the variable/handle to each of those timerPerformWithDelay calls as such

timer1 = timer.performWithDelay…

timer2 = timer.performWithDelay…  etc…

so you will have a way of identifying which timers to cancel

  1. When menu or any other scene calls ‘goto’ scene2 it will re-create scene2 from scratch.  The app is not that complex or heavy graphics or such and to destroy scene when leaving is not overly taxing on the performance.

Just my opinion: consider using a game loop in the scene2 instead of all the timers.  Create a Runtime ‘enterFrame’ event listener and activate it when scene shows, remove it when scene hides and/or destroys.  In that loop do all the timing stuff rather then having 4 or 5 timers functioning. You would have to do some studying on how timing works in the game loop, but I feel it is going to be more efficient in the long run.  

But do whichever you are comfortable with while you are still fairly new with this stuff.

There is a little code-snippet floating around the forum that you might try to find that would allow you to type just a single line of code that would stop all transitions (maybe even timers) … I just do not recall where I saw that on the forums.

Good luck.

Did you figure this out? It’s a lot of code to read, which is why I think you haven’t had a response yet. Which image stays on the screen?

Hi,harocebury. The image of the car and the game doesn’t stop when i move to another screen. TQ.

I think you have something, probably a transition, holding onto a reference to the car image after the scene is shifted off screen. You also have a global (well, local to the module) reference to the car image.

Check that the transitions are all complete before allowing the scene to end. You can also try cancelling them.

Which car image stays on the screen? I notice you have .

now the car image doesn’t stays on the screen when i move out from the screen but yet the game doesn’t stop. I not so sure whether is the  transition problem or not and i how to edit in order to make it stop after i move out from the screen. Please help me

I think you should clean your code and maybe try with smaller code. That will make it easier to identify what is going on.

What do you mean by “game doesn’t stop”?

I have try to run part by part, but when it come to function createCar(), things all go wrong.

“game doesn’t stop” mean the game still carry on when i move out of the screen.

just a very quick look at the code and a guess on my part, but I would ‘rem-out’ the createCar() call in the timerDown function. Run the code and see if the car still is on screen when changing scenes.  If not, then that function is where the problem lies… that timer may still be running when a scene ends(just guessing), and is creating a car post scene somehow.

I don’t have a lot of time to really look the code over right now.  If the issue is not intuit function I can try and look at the code a little closer later tonight.

Good luck

I have try to remove the createCar() call  in the timerDown function but yet the game still carry on