my game is supposed to restart once a collision happens with my penguin and the ice, but once the collision it happens the game just stops and the replay screen is not shown and nothing from the game.lua goes away
if i click where the play button is supposed to be in the restart.lua it’ll start the game but the game just restart and the ice blocks are invisible
this is my code for the game
–
– main.lua
–
display.setStatusBar(display.HiddenStatusBar)
local _W = display.contentWidth
local _H = display.contentHeight
local scrollSpeed = 3
local physics = require “physics”
physics.start()
local composer = require( “composer” )
local scene = composer.newScene()
–21 here you declare all your local variables:
local score = 0
–24 here you declare all your local functions: – whatever your code requires…
local function doSomething()
score = score + 1
end
local function move(event)
bg2.x = bg2.x - scrollSpeed/2
bg3.x = bg3.x - scrollSpeed/2
bg4.x = bg4.x - scrollSpeed/2
bg5.x = bg5.x - scrollSpeed/2
bg6.x = bg6.x - scrollSpeed/2
bg7.x = bg7.x - scrollSpeed
bg8.x = bg8.x - scrollSpeed
bg9.x = bg9.x - scrollSpeed
bg10.x = bg10.x - scrollSpeed
bg11.x = bg11.x - scrollSpeed
if(-bg2.x + bg2.contentWidth) > 1080 then
bg2:translate(2000,0)
end
if(-bg3.x + bg3.contentWidth) > 1080 then
bg3:translate(2000,0)
end
if(-bg4.x + bg4.contentWidth) > 1080 then
bg4:translate(2000,0)
end
if(-bg5.x + bg5.contentWidth) > 1080 then
bg5:translate(2000,0)
end
if(-bg6.x + bg6.contentWidth) > 1080 then
bg6:translate(2000,0)
end
if(-bg7.x + bg7.contentWidth) > 1080 then
bg7:translate(2000,0)
end
if(-bg8.x + bg8.contentWidth) > 1080 then
bg8:translate(2000,0)
end
if(-bg9.x + bg9.contentWidth) > 1080 then
bg9:translate(2000,0)
end
if(-bg10.x + bg10.contentWidth) > 1080 then
bg10:translate(2000,0)
end
if(-bg11.x + bg11.contentWidth) > 1080 then
bg11:translate(2000,0)
end
end
local function activatePengs(self,event)
self:applyForce(0, -45, self.x, self.y)
end
local function touchScreen(event)
– print(“touch”)
if event.phase == “began” then
peng.enterFrame = activatePengs
Runtime:addEventListener(“enterFrame”, peng)
end
if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, peng)
end
end
– local function endGame()
– composer.gotoScene( “restart”, { time=800, effect=“crossFade” } )
– end
local function onCollision(event)
if event.phase == “began” then
print “collide”
composer.gotoScene( “restart” )
end
end
– now comes four required functions for Composer:
function scene:create( event )
local sceneGroup = self.view
– put any thing you need to create here
local bg1 = display.newImageRect(“bg.png”, 800, 1000)
bg1.x = _W*1/2; bg1.y = _H/2;
bg2 = display.newImage(“ice2.png”,140,210)
bg3 = display.newImage(“ice2.png”,540,210)
bg4 = display.newImage(“ice2.png”,940,210)
bg5 = display.newImage(“ice2.png”,1340,210)
bg6 = display.newImage(“ice2.png”,1740,210)
bg7 = display.newImage(“ice1.png”,140,420)
bg8 = display.newImage(“ice1.png”,540,420)
bg9 = display.newImage(“ice1.png”,940,420)
bg10 = display.newImage(“ice1.png”,1340,420)
bg11 = display.newImage(“ice1.png”,1740,420)
peng = display.newImage(“peng.png”, 80, 201)
physics.addBody(peng, “dynamic”, {density=.18, bounce=0.1, friction=.5, radius=55})
icebok = display.newImage(“icebok.png”, 480, 201)
physics.addBody(icebok, “static”, {density=.1, bounce=0.1, friction=.5, radius=1})
icebok.speed = 4
icebok.initY = icebok.y + math.random(0,500)
– icebok.amp = math.random(10,50)
– icebok.angle = math.random(1,720)
icebok1 = display.newImage(“icebok.png”, 680, 301)
physics.addBody(icebok1, “static”, {density=.1, bounce=0.1, friction=.5, radius=1})
icebok1.speed = 4
icebok1.initY = icebok1.y + math.random(0,500)
– icebok1.amp = math.random(10,50)
– icebok1.angle = math.random(1,720)
icebok2 = display.newImage(“icebok.png”, 880, 401)
physics.addBody(icebok2, “static”, {density=.1, bounce=0.1, friction=.5, radius=1})
icebok2.speed = 4
icebok2.initY = icebok2.y + math.random(0,500)
– icebok2.amp = math.random(10,50)
– icebok2.angle = math.random(1,720)
icebok3 = display.newImage(“icebok.png”, 1080, 451)
physics.addBody(icebok3, “static”, {density=.1, bounce=0.1, friction=.5, radius=1})
icebok3.speed = 4
icebok3.initY = icebok3.y + math.random(0,500)
– icebok2.amp = math.random(10,50)
– icebok2.angle = math.random(1,720)
function moveiceboks(self,event)
if self.x < -10 then
self.x = math.random (480,1080)
self.y = math.random(0,500)
self.speed = 4
– self.amp = math.random(10,50)
– self.angle = math.random(1,720)
else
self.x = self.x - self.speed
– self.angle = self.angle + .1
– self.y = self.amp * math.tan(self.angle) + self.initY
– self.y = math.random(0,500)
end
end
– sceneGroup:insert ( background )
– the rest of your creations go here
end
function scene:show( event )
local sceneGroup = self.view
if event.phase == “will” then
– put code here you want to happen just before the scene comes on the screen
– physics.start()
Runtime:addEventListener(“enterFrame”, move)
Runtime:addEventListener(“touch”, touchScreen)
icebok.enterFrame = moveiceboks
Runtime:addEventListener(“enterFrame”,icebok)
icebok1.enterFrame = moveiceboks
Runtime:addEventListener(“enterFrame”,icebok1)
icebok2.enterFrame = moveiceboks
Runtime:addEventListener(“enterFrame”,icebok2)
icebok3.enterFrame = moveiceboks
Runtime:addEventListener(“enterFrame”,icebok3)
Runtime:addEventListener(“collision”, onCollision)
else
– put code here you want to happen after the scene comes on the screen
– physics.start()
– Runtime:addEventListener(“enterFrame”, move)
Runtime:addEventListener(“touch”, touchScreen)
– icebok.enterFrame = moveiceboks
– Runtime:addEventListener(“enterFrame”,icebok)
– icebok1.enterFrame = moveiceboks
– Runtime:addEventListener(“enterFrame”,icebok1)
– icebok2.enterFrame = moveiceboks
– Runtime:addEventListener(“enterFrame”,icebok2)
– icebok3.enterFrame = moveiceboks
– Runtime:addEventListener(“enterFrame”,icebok3)
Runtime:addEventListener(“collision”, onCollision)
end
end
function scene:hide( event )
local sceneGroup = self.view
if event.phase == “will” then
– put code here you want to happen just before the scene leaves the screen
Runtime:removeEventListener(“enterFrame”, move)
Runtime:removeEventListener(“touch”, touchScreen)
– icebok.enterFrame = moveiceboks
Runtime:removeEventListener(“enterFrame”,icebok)
– icebok1.enterFrame = moveiceboks
Runtime:removeEventListener(“enterFrame”,icebok1)
– icebok2.enterFrame = moveiceboks
Runtime:removeEventListener(“enterFrame”,icebok2)
– icebok3.enterFrame = moveiceboks
Runtime:removeEventListener(“enterFrame”,icebok3)
Runtime:removeEventListener(“collision”, onCollision)
else
– put code here you want to happen after the scene has left the screen
– Runtime:removeEventListener(“enterFrame”, move)
Runtime:removeEventListener(“touch”, touchScreen)
Runtime:removeEventListener(“enterFrame”,icebok)
Runtime:removeEventListener(“enterFrame”,icebok1)
Runtime:removeEventListener(“enterFrame”,icebok2)
Runtime:removeEventListener(“enterFrame”,icebok3)
Runtime:removeEventListener(“collision”, onCollision)
end
end
function scene:destroy( event )
local sceneGroup = self.view
– put code here if you have things you need to remove that you created in create scene that does NOT go into the sceneGroup)
end
– these must be the last 5 lines in the file
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
and the restart
–
– main.lua
–
display.setStatusBar(display.HiddenStatusBar)
local _W = display.contentWidth
local _H = display.contentHeight
local scrollSpeed = 3
local composer = require( “composer” )
local scene = composer.newScene()
local function gotoGame()
composer.gotoScene(“game”, { time=800, effect=“crossFade” } )
print “game”
end
– local function gotoHighScores()
– composer.gotoScene( “highscores” )
– end
function scene:create( event )
local sceneGroup = self.view
local bg1 = display.newImageRect( sceneGroup,“bg.png”, 800, 1000)
bg1.x = _W*1/2; bg1.y = _H/2;
bg2 = display.newImage( sceneGroup, “ice2.png”,140,210)
bg7 = display.newImage( sceneGroup, “ice1.png”,140,420)
– title = display.newImage( sceneGroup, “title.png”, 160, 90)
local playButton = display.newText( sceneGroup, “Play”, 160, 250, native.systemFont, 40 )
playButton:setFillColor( 0.23, 0.60, 1 )
– local highScoresButton = display.newText( sceneGroup, “High Score”, 160, 350, native.systemFont, 40 )
– highScoresButton:setFillColor( 0.23, 0.60, 1 )
playButton:addEventListener(“tap”, gotoGame)
– highScoresButton:addEventListener(“tap”, gotoHighScores)
composer.removeScene( “game” )
end
function scene:show( event )
composer.removeScene( “game” )
– local sceneGroup = self.view
– local phase = event.phase
– if ( phase == “will” ) then
– Code here runs when the scene is still off screen (but is about to come on screen)
– elseif ( phase == “did” ) then
– Code here runs when the scene is entirely on screen
– end
end
function scene:hide( event )
end
function scene:destroy( event )
end
– these must be the last 5 lines in the file
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
sorry if i posted this wrong