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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- 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

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- 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

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 end 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 end and it should work fine :)

Peach

views:1426 update:2011/10/19 8:59:29
corona forums © 2003-2011