Time to end the game

Ok, I’m working on a mildly simple app for a presentational marketing piece for work. It’s using the “Samurai Fruit” as the basis for the app to get me started. It’s not for retail - simply a prototype to use at a conference to get attention to our booth.

So I have it edited and changed so it works and looks more unique to our company. I wanted to add a timer so people who are really good with Fruit Ninja style apps don’t stand there for ages playing one round of the game.

So I came across a nice little script over at www.techority.com

I made it a module, attached it to my main.lau and it works. Timer bar draws down.

But, obviously, it doesn’t end the game.

So I’m trying my best to figure out how to get it to end … I just want to at this point when the bar gets to the end say “Times up”. I can figure out how to get it to go to the end screen later. One step at a time, ya know?

So here is my code. I get no errors - but the if statement at the end doesn’t do squat …

I’m a LUA noob … so I apologize if the answer is staring me in the faca with bright, blinking LED lights shining at me.

[code]

– Timer
–Show a basic white bar
local timeBar = display.newRect( 20, 165, 280, 20 )
timeBar:setReferencePoint(display.BottomLeftReferencePoint)
timeBar.x = 20
timeBar.y = 40

–Make the bar shrink over time
local function loseTime (event)
timeBar.width = timeBar.width - 2
timeBar:setReferencePoint(display.BottomLeftReferencePoint)
timeBar.x = 20
end
gameTimer = timer.performWithDelay(5,loseTime, 0)

if(gameTimer == 0) then
textObject = display.newText( “Times Up!”, 50, 50, nil, 24 )
textObject:setTextColor( 255, 0, 0 )
end

[/code] [import]uid: 97023 topic_id: 16352 reply_id: 316352[/import]

Are you using director or do you have a gameover function already in there?

If so you can just call that just before your final [lua]end[/lua] above.

If you also need a gameover function written for you, then you’ll want to look at premium support.

If not, then yeah, just whack gameover() before your final [lua]end[/lua] and it should work fine :slight_smile:

Peach [import]uid: 52491 topic_id: 16352 reply_id: 60947[/import]

Wrap 16-19 lines in a function like say checkTime() and then at the end of you looseTime() function add a line to to call it. Change the check to timeBar.width <= instead too.

[code]
– Timer
–Show a basic white bar
local timeBar = display.newRect( 20, 165, 280, 20 )
timeBar:setReferencePoint(display.BottomLeftReferencePoint)
timeBar.x = 20
timeBar.y = 40

–Make the bar shrink over time
local function loseTime (event)
timeBar.width = timeBar.width - 2
timeBar:setReferencePoint(display.BottomLeftReferencePoint)
timeBar.x = 20
checkTime()
end
gameTimer = timer.performWithDelay(5,loseTime, 0)

local checkTime()
if(timeBar.width <= 0) then
textObject = display.newText( “Times Up!”, 50, 50, nil, 24 )
textObject:setTextColor( 255, 0, 0 )
timer.cancel(gameTimer)
gameTimer = nil
end
end
[/code] [import]uid: 78015 topic_id: 16352 reply_id: 60943[/import]

I tried what you did Paul but it actually STOPPED the timer form working. Strangely enough.
I do have an end spot already - which ends when you click the wrong item and then it says game over, play again? Basiaclly.

So, the idea is (to reitterate) to add a timer so they have 90 seconds (or whatever) to click the items and get as many points (which I didn’t add the point system yet) as they can. This is being used for a conference so we don’t want people standing around playing this game forever.

So, I moved some code around as recommended here and in the IRC.

[code]-- Timer
–Show a basic white bar
local timeBar = display.newImage(“timebar.png”)
timeBar:setReferencePoint(display.BottomLeftReferencePoint)
timeBar.x = 20
timeBar.y = 40

–Make the bar shrink over time
local function loseTime (event)
timeBar.width = timeBar.width - 2
timeBar:setReferencePoint(display.BottomLeftReferencePoint)
timeBar.x = 20
if(timeBar.x == 0 ) then
local gameOver = displayGameOver()

end

end
gameTimer = timer.performWithDelay(5,loseTime, 0)

[/code]

The game over script is working, BUT not completely…

Basically it’s not calculating it seems. The timer bar shrinks down, but nothing.

now if I change the if(timeBar.x ==0) then to something like if(timeBar.x ~= 0) then it tosses the next line and runs the gameOver part of the game.

SO … I’m lost. HAHA Any hints or tips or thrown boots would be greatly appreciated. [import]uid: 97023 topic_id: 16352 reply_id: 61087[/import]

Sorry I thought you wanted the timer to stop once you reached Zero so ya just take out the timer.cancel() like you did it appears.

Change line 13 to:

if(timeBar.width == 0 ) then [import]uid: 78015 topic_id: 16352 reply_id: 61117[/import]

Ok, I got this working:

  
local timeBar = display.newRect( 200, 165, 800, 40 )  
timeBar:setFillColor (176,23,31)  
timeBar:setReferencePoint(display.BottomLeftReferencePoint)  
timeBar.x = 10  
timeBar.y = 50  
  
local function loseTime()  
  
 if (timeBar.width == nil or timeBar.width \<= 1 ) then  
  
 displayGameOver()  
 else  
 timeBar.width = timeBar.width -1  
 timeBar:setReferencePoint(display.BottomLeftReferencePoint)  
 timeBar.x = 20  
 end  
end  
  
gameTimer = timer.performWithDelay(1,loseTime, 0)  
  

Then in the displayGameOver() function I put this:

  
function displayGameOver()  
  
 local group = display.newGroup()  
  
 local back = display.newRect( 0,0, display.contentWidth, display.contentHeight )  
 back:setFillColor(0,0,0, 255 \* .1)  
 group:insert(back)  
  
 timer.cancel(gameTimer)  
 timer.cancel(coinTimer)  
 timer.cancel(ektronTimer)  
 timer.cancel(bombTimer)  
 director:changeScene( "gameover", "fade" )  
  
  
 return group  
  
end  

Yes, this is a hacked up version of the Samarai Fruit. It’s a prototype for a conference. [import]uid: 97023 topic_id: 16352 reply_id: 63864[/import]