I have my game all coded and it works fine until I play the game twice. I have a timer and when it ends the game is over and you are taken to a game over scene. in that scene you have a play again button and a game over button. if I push the play again button after I get to that scene it works totally fine but if I push it again my timer is already at 0 and it saves my score from the last game letting you score infinitely. What do you think is wrong and how could I go about fixing it?
You’ve got a variable you’re not resetting or clearing between sessions.
Also, you should check for unnecessary globals. i.e. Variables made global when they should be local.
Ok, that is what I was thinking but I have tried everything I know to remove the timer after it ends but it doesn’t work. Also how would I know if something is a global compared to a local, and why does it work the first and second time?
Anyone know how I should stop my timer and reset my score? timer.cancel(timername) isn’t working for me.
Without any code it’s impossible to identify your issue. If you can post the code in question and any errors you’re receiving, we can assist much better.
Also, it is imperative that you be able to identify global versus local variables in your own code. If Lua/Corona is your first exposure to development, they have created useful docs to help you through your first project.
http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/
http://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/
http://coronalabs.com/blog/2012/04/24/working-with-time-delays-and-counting/
Thanks for your help I actually figured out the whole global thing a couple days ago, thanks though.
here’s the code for my game I know its probably pretty ugly though this is my first actual game that I’ve programmed.
local composer = require( “composer” )
local scene = composer.newScene()
_G.score = 0
function scene:create( event )
local sceneGroup = self.view
local background = display.newRect(0, 0, 700, 1000)
background:setFillColor(0, 0, 0)
sceneGroup:insert(background)
local options =
{
width = 50,
height = 50,
numFrames = 1,
sheetContentWidth = 50,
sheetContentHeight = 50,
}
local imageSheet = graphics.newImageSheet( “cookiebitmap.png”, options )
local cookie = display.newImage( imageSheet, 1 )
sceneGroup:insert(cookie)
local scoretext = display.newText(“Score:”, 210, -20, native.systemFont, 30)
scoretext:setFillColor(1,1,1)
sceneGroup:insert(scoretext)
local timeword = display.newText(“Time:”, 50, -20, native.systemFont, 30)
sceneGroup:insert(timeword)
local scorenumber = display.newText(_G.score, 280, -20, native.systemFont, 30)
scorenumber:setFillColor(1,1,1)
sceneGroup:insert(scorenumber)
local function addToScore()
_G.score = score + 1
scorenumber.text = _G.score
transition.moveTo(cookie, {x= math.random(30, 280), y= math.random(-20, 500), time=0})
end
cookie:addEventListener(“tap”, addToScore)
local randomx = math.random(30, 280)
local randomy = math.random(-20, 500)
cookie.x = randomx
cookie.y = randomy
local timeLimit = 10
– time text
local timeLeft = display.newText(timeLimit, 120, -20, native.systemFont, 30)
sceneGroup:insert(timeLeft)
local timeword = display.newText(“Time:”, 50, -20, native.systemFont, 30)
sceneGroup:insert(timeword)
local function timerdown()
timeLimit = timeLimit-1
timeLeft.text = timeLimit
if(timeLimit ==0) then composer.gotoScene(“end”)
end
end
gametimer = timer.performWithDelay(1000, timerdown, timeLimit)
end
– “scene:show()”
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
elseif ( phase == “did” ) then
end
end
– “scene:hide()”
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
timer.cancel(gametimer)
elseif ( phase == “did” ) then
end
end
– “scene:destroy()”
function scene:destroy( event )
local sceneGroup = self.view
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
The problem that I’m having is when the game finishes it goes to a gameover file which displays the score you just got and has a play again button. The problem occurs when you push play again because the timer is at 0 and the score is saved from the game you just played which lets you score infinitely. how I restart the timer and score when I go back to the scene?
Never mind I figured it all out I just had to move some things to a show function. Thanks anyways!
You’ve got a variable you’re not resetting or clearing between sessions.
Also, you should check for unnecessary globals. i.e. Variables made global when they should be local.
Ok, that is what I was thinking but I have tried everything I know to remove the timer after it ends but it doesn’t work. Also how would I know if something is a global compared to a local, and why does it work the first and second time?
Anyone know how I should stop my timer and reset my score? timer.cancel(timername) isn’t working for me.
Without any code it’s impossible to identify your issue. If you can post the code in question and any errors you’re receiving, we can assist much better.
Also, it is imperative that you be able to identify global versus local variables in your own code. If Lua/Corona is your first exposure to development, they have created useful docs to help you through your first project.
http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/
http://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/
http://coronalabs.com/blog/2012/04/24/working-with-time-delays-and-counting/
Thanks for your help I actually figured out the whole global thing a couple days ago, thanks though.
here’s the code for my game I know its probably pretty ugly though this is my first actual game that I’ve programmed.
local composer = require( “composer” )
local scene = composer.newScene()
_G.score = 0
function scene:create( event )
local sceneGroup = self.view
local background = display.newRect(0, 0, 700, 1000)
background:setFillColor(0, 0, 0)
sceneGroup:insert(background)
local options =
{
width = 50,
height = 50,
numFrames = 1,
sheetContentWidth = 50,
sheetContentHeight = 50,
}
local imageSheet = graphics.newImageSheet( “cookiebitmap.png”, options )
local cookie = display.newImage( imageSheet, 1 )
sceneGroup:insert(cookie)
local scoretext = display.newText(“Score:”, 210, -20, native.systemFont, 30)
scoretext:setFillColor(1,1,1)
sceneGroup:insert(scoretext)
local timeword = display.newText(“Time:”, 50, -20, native.systemFont, 30)
sceneGroup:insert(timeword)
local scorenumber = display.newText(_G.score, 280, -20, native.systemFont, 30)
scorenumber:setFillColor(1,1,1)
sceneGroup:insert(scorenumber)
local function addToScore()
_G.score = score + 1
scorenumber.text = _G.score
transition.moveTo(cookie, {x= math.random(30, 280), y= math.random(-20, 500), time=0})
end
cookie:addEventListener(“tap”, addToScore)
local randomx = math.random(30, 280)
local randomy = math.random(-20, 500)
cookie.x = randomx
cookie.y = randomy
local timeLimit = 10
– time text
local timeLeft = display.newText(timeLimit, 120, -20, native.systemFont, 30)
sceneGroup:insert(timeLeft)
local timeword = display.newText(“Time:”, 50, -20, native.systemFont, 30)
sceneGroup:insert(timeword)
local function timerdown()
timeLimit = timeLimit-1
timeLeft.text = timeLimit
if(timeLimit ==0) then composer.gotoScene(“end”)
end
end
gametimer = timer.performWithDelay(1000, timerdown, timeLimit)
end
– “scene:show()”
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
elseif ( phase == “did” ) then
end
end
– “scene:hide()”
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
timer.cancel(gametimer)
elseif ( phase == “did” ) then
end
end
– “scene:destroy()”
function scene:destroy( event )
local sceneGroup = self.view
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
The problem that I’m having is when the game finishes it goes to a gameover file which displays the score you just got and has a play again button. The problem occurs when you push play again because the timer is at 0 and the score is saved from the game you just played which lets you score infinitely. how I restart the timer and score when I go back to the scene?
Never mind I figured it all out I just had to move some things to a show function. Thanks anyways!