Including score function in game for distance traveled.

I need to find a way to include a scoring mechanism for my game. Right now its just a simple side scroller that the background scrolls left and repeats while my rocket just moves up and down on the y axis. I want the score to be kept for distance that is supposed to be traveled or somehow keep track of the time passed without colliding with anything. Anyone have any suggestions on how to go about this?

EDIT: Right now I have a few ways to keep score… If an enemy hits the back wall and respawns you get +1 and if you click to raise your rocket you get +1. Now what I am looking for is a way to constantly having my score going up to mimic distance being traveled by the rocket. I used os.time() last night and had not much luck. I had it working for +1 per second but in only was added and displayed to the screen when I added it to my onTouch rocket function which made it look like the score was added by clicking the rocket. I couldn’t get it to work by itself… any help? [import]uid: 20272 topic_id: 35220 reply_id: 335220[/import]

Can’t you use a timer? Like this:

local points=0  
  
 local function addPoints( event )  
 points = points +1  
 end  
   
 timer.performWithDelay(1000, addPoints, 0)  

You can start the timer when your game starts and cancel it when its finished/player dies. You can also pause and resume the timer when the user pauses the game or some other action. [import]uid: 58885 topic_id: 35220 reply_id: 140068[/import]

I finally used that code and got it to work… I still cannot get it to pause though. I have this written in a function so when the game is ready the timer will start… Now when I take out hte timer.pause it works fine but once you die the time keeps going. I’ve tried a million different things to incorporate the pause and adding functions etc trying to get it to pause and I cannot get it to work… Any ideas why?

[lua] if rocket.ready == true then
timer.performWithDelay(100, scoreTime, 0)
timer.pause(scoreTime)
end [import]uid: 20272 topic_id: 35220 reply_id: 140083[/import]

You’ll need to reference your timer differently:

Forward declaration:

[lua]local scoreTimer – timer reference[/lua]

…later on start timer

[lua]local function scoreTime()
score = score + 1
end
scoreTimer = timer.performWithDelay( 100, scoreTime ) – start timer[/lua]

…even later, pause timer

[lua]timer.pause( scoreTimer )[/lua] [import]uid: 202223 topic_id: 35220 reply_id: 140086[/import]

Thank you… I have everything like you do and it works but I still cannot get the pause working. I put it in my onCollision function to pause right before the rocket stops explodes and calls the game over function and get the following error…

2013-01-23 15:58:55.493 Corona Simulator[605:707] WARNING: timer.pause() cannot pause a timerId that is already expired.

[lua]function onCollision(event)
if event.phase == “began” then
print(“Collision”)
if rocket.collided == false then
timer.pause(scoreTimer)
rocket.collided = true
rocket.bodyType = “static”
explosion()
end
end
end
[import]uid: 20272 topic_id: 35220 reply_id: 140110[/import]

I even do things like this that calls a function and sets a variable to false that should stop the timer from working since its called if rocket.ready == true… This way I get no errors but the timer still does not pause…

[lua]function rocketReady()
rocket.ready = true
rocket.bodyType = “dynamic”
rocket.score = true
if rocket.ready == true then
timer.performWithDelay(100, scoreTime, 0)
if rocket.ready == false then
timer.pause(scoreTimer)
end
end
end

function rocketFalse()
rocket.ready = false
end

function onCollision(event)
if event.phase == “began” then
print(“Collision”)
if rocket.collided == false then
rocket.collided = true
rocketFalse()
rocket.bodyType = “static”
explosion()
end
end
end

[import]uid: 20272 topic_id: 35220 reply_id: 140112[/import]

How often does your rocketReady function get called? Also I see you trying to pause a timer with ID scoreTimer, but I never see you set that ID anywhere. Look at the change on line 6. So assuming you set a forward reference somewhere change your code to this:

[code]
function rocketReady()
rocket.ready = true
rocket.bodyType = “dynamic”
rocket.score = true
if rocket.ready == true then
scoreTimer = timer.performWithDelay(100, scoreTime, 0)
if rocket.ready == false then
timer.pause(scoreTimer)
end
end
end

function rocketFalse()
rocket.ready = false
end

function onCollision(event)
if event.phase == “began” then
print(“Collision”)
if rocket.collided == false then
rocket.collided = true
rocketFalse()
rocket.bodyType = “static”
explosion()
end
end
end

[/code] [import]uid: 58885 topic_id: 35220 reply_id: 140117[/import]

@chevol: Good eye. Yes, of course you need to use the scoreTimer variable. I should have seen that.

[import]uid: 202223 topic_id: 35220 reply_id: 140118[/import]

Lol, we’ve all been there. [import]uid: 58885 topic_id: 35220 reply_id: 140120[/import]

Ok I do have the variable declared right above all my functions didnt post that sorry… Also my rocketReady function I guess only gets called once. I have it up top where I declare my rocket itself like this…

[lua]rocketIntro = transition.to(rocket,{time = 1000, x = 100, onComplete = rocketReady}) [import]uid: 20272 topic_id: 35220 reply_id: 140122[/import]

@mkjt88 look at my post (#6) that should be all you need. [import]uid: 58885 topic_id: 35220 reply_id: 140128[/import]

Alright I took out this part and made sure everything was the same and it works now. Appreciate all the help.

[lua] if rocket.ready == false then
timer.pause(scoreTimer) [import]uid: 20272 topic_id: 35220 reply_id: 140133[/import]

Can’t you use a timer? Like this:

local points=0  
  
 local function addPoints( event )  
 points = points +1  
 end  
   
 timer.performWithDelay(1000, addPoints, 0)  

You can start the timer when your game starts and cancel it when its finished/player dies. You can also pause and resume the timer when the user pauses the game or some other action. [import]uid: 58885 topic_id: 35220 reply_id: 140068[/import]

I finally used that code and got it to work… I still cannot get it to pause though. I have this written in a function so when the game is ready the timer will start… Now when I take out hte timer.pause it works fine but once you die the time keeps going. I’ve tried a million different things to incorporate the pause and adding functions etc trying to get it to pause and I cannot get it to work… Any ideas why?

[lua] if rocket.ready == true then
timer.performWithDelay(100, scoreTime, 0)
timer.pause(scoreTime)
end [import]uid: 20272 topic_id: 35220 reply_id: 140083[/import]

You’ll need to reference your timer differently:

Forward declaration:

[lua]local scoreTimer – timer reference[/lua]

…later on start timer

[lua]local function scoreTime()
score = score + 1
end
scoreTimer = timer.performWithDelay( 100, scoreTime ) – start timer[/lua]

…even later, pause timer

[lua]timer.pause( scoreTimer )[/lua] [import]uid: 202223 topic_id: 35220 reply_id: 140086[/import]

Thank you… I have everything like you do and it works but I still cannot get the pause working. I put it in my onCollision function to pause right before the rocket stops explodes and calls the game over function and get the following error…

2013-01-23 15:58:55.493 Corona Simulator[605:707] WARNING: timer.pause() cannot pause a timerId that is already expired.

[lua]function onCollision(event)
if event.phase == “began” then
print(“Collision”)
if rocket.collided == false then
timer.pause(scoreTimer)
rocket.collided = true
rocket.bodyType = “static”
explosion()
end
end
end
[import]uid: 20272 topic_id: 35220 reply_id: 140110[/import]

I even do things like this that calls a function and sets a variable to false that should stop the timer from working since its called if rocket.ready == true… This way I get no errors but the timer still does not pause…

[lua]function rocketReady()
rocket.ready = true
rocket.bodyType = “dynamic”
rocket.score = true
if rocket.ready == true then
timer.performWithDelay(100, scoreTime, 0)
if rocket.ready == false then
timer.pause(scoreTimer)
end
end
end

function rocketFalse()
rocket.ready = false
end

function onCollision(event)
if event.phase == “began” then
print(“Collision”)
if rocket.collided == false then
rocket.collided = true
rocketFalse()
rocket.bodyType = “static”
explosion()
end
end
end

[import]uid: 20272 topic_id: 35220 reply_id: 140112[/import]

How often does your rocketReady function get called? Also I see you trying to pause a timer with ID scoreTimer, but I never see you set that ID anywhere. Look at the change on line 6. So assuming you set a forward reference somewhere change your code to this:

[code]
function rocketReady()
rocket.ready = true
rocket.bodyType = “dynamic”
rocket.score = true
if rocket.ready == true then
scoreTimer = timer.performWithDelay(100, scoreTime, 0)
if rocket.ready == false then
timer.pause(scoreTimer)
end
end
end

function rocketFalse()
rocket.ready = false
end

function onCollision(event)
if event.phase == “began” then
print(“Collision”)
if rocket.collided == false then
rocket.collided = true
rocketFalse()
rocket.bodyType = “static”
explosion()
end
end
end

[/code] [import]uid: 58885 topic_id: 35220 reply_id: 140117[/import]

@chevol: Good eye. Yes, of course you need to use the scoreTimer variable. I should have seen that.

[import]uid: 202223 topic_id: 35220 reply_id: 140118[/import]

Lol, we’ve all been there. [import]uid: 58885 topic_id: 35220 reply_id: 140120[/import]