I have problem with my game. This is my code for my gamefile. MY flow is main lua>startgame>>game>>restart>>game>>restart and so on…
local composer=require("composer") local physics=require("physics") local widget=require("widget") local scene=composer.newScene() physics.setDrawMode("normal") --erased system activate check later system.activate("multitouch") \_H=display.contentHeight \_W=display.contentWidth trumps=0 numTrumps=100 startTime=20 totalTime=20 timeLeft=true playerReady=false Random=math.random local function Restart(event) if "began"==event.phase then print("hello") end if "moved"==event.phase then end if "ended"==event.phase then composer.gotoScene("restarttest") print("hello") end end local function gameOver(condition) if (condition=="winner") then screenText.text="Adios Trump!" elseif (condition=="notbad") then screenText.text="Barely stopped Trump" elseif (condition=="loser") then screenText.text="Ready for That Wall!" end end local function removeTrumps(obj) obj:removeSelf() trumps=trumps-1 if (timeLeft~=false) then if(trumps==0) then timer.cancel(gameTimer) gameOver("winner") elseif(trumps\<=40) then gameOver("notbad") elseif(trumps\>=31) then gameOver("loser") end end end local function countDown(event) if (startTime==totalTime) then playerReady=true screenText.text="Hurry!" end startTime=startTime-1 timeText.text="Time: "..startTime if (startTime==0) then timeLeft=false end end local function startGame() myTrump=display.newImageRect("tp.png",25,25) myTrump.x=Random(50,\_W-50) myTrump.y=(\_H+10) physics.addBody(myTrump,"dynamic",{density=.1,friction=0,bounce=.9,radius=10}) function myTrump:touch(event) if(timeLeft~=false) then if (playerReady==true) then if(event.phase=="ended") then removeTrumps(self) end end end end myTrump:addEventListener("touch",myTrump) trumps=trumps+1 if(trumps==numTrumps) then gameTimer=timer.performWithDelay(1000,countDown,totalTime) else playerReady=false end end local gameTimer=timer.performWithDelay(20,startGame,numTrumps) function scene:create(event) local screenGroup=display.newGroup() background=display.newImage(screenGroup,"tflag.png") but1=widget.newButton{screenGroup,label="restart",onEvent=Restart} but1.x=\_W/2+150 but1.y=\_H-10 screenText=display.newText(screenGroup,"...Waiting for Trump...",0,0,native.systemFont,16\*2) screenText.xScale=.5 screenText.yScale=.5 screenText.anchorX=(0) screenText.anchorY=(0) screenText.x=\_W/2-210 screenText.y=\_H-20 timeText=display.newText(screenGroup,"Time:"..startTime,0,0,native.systemFont,16\*2) timeText.xScale=.5 timeText.yScale=.5 timeText.anchorY=0 timeText.anchorX=0 timeText.x=\_W/2 timeText.y=\_H-20 leftWall=display.newRect(screenGroup,483,50,1,1000) rightWall=display.newRect(screenGroup,0,0,1,1000) ceiling=display.newRect(screenGroup,150,0,1000,1) end function scene:show(event) local screenGroup=display.newGroup() if event.phase=="will" then physics.start() physics.setGravity(0,-5) physics.addBody(leftWall,"static",{bounce=.1}) physics.addBody(rightWall,"static",{bounce=.1}) physics.addBody(ceiling,"static",{bounce=.1}) end if event.phase=="did" then end end function scene:hide(event) local screenGroup=display.newGroup() if event.phase=="will" then end if event.phase=="did" then Runtime:removeEventListener("touch",myTrump) end end function scene:destroy(event) local screenGroup=display.newGroup() end scene:addEventListener("create",scene) scene:addEventListener("show",scene) scene:addEventListener("hide",scene) scene:addEventListener("destroy",scene) return scene
Here is my restart files code:
local composer=require("composer") local scene=composer.newScene() --background function scene:create(event) local screenGroup=display.newGroup() background=display.newImage(screenGroup,"tflag.png") end function start(event) if event.phase=="began" then composer.removeScene("game1") end if event.phase=="ended" then composer.gotoScene("game1") end end function scene:show(event) local screenGroup=display.newGroup() background:addEventListener("touch",start) end function scene:hide(event) local screenGroup=display.newGroup() if event.phase=="will" then background:removeEventListener("touch",start) end if event.phase=="did" then end end function scene:destroy(event) local screenGroup=self.view end scene:addEventListener("create",scene) scene:addEventListener("show",scene) scene:addEventListener("hide",scene) scene:addEventListener("destroy",scene) return scene
My problems are:
After the intial run through i click on the restart button and i am taken to the restart scene and from there when i go back to the game scene the timer that starts at 20 goes past 0 (onto negative numbers) each time i try to click the pictures my game just restarts and my game no longer works.
Also in the initial run through my pictures(the trumps that you click and when they are clicked they are gotten rid of) go to the very top but when i go back to the game scene after the restart scene they go midway so it is like the ceiling fell down a bit!
Can i please have help and can you tell me what i need to fix or just copy my code and edit the mistakes and then post it as the answer. Thank you!