"win" and "fail" with timer!!!! please help!!

i was triying to follow this tutorial http://www.youtube.com/watch?v=6TsDdLY7VXk but i have always the same error and i try to do what they say but i am very newbie and i don’t now,if i do it fine but the screen didn’t do what in tutorial do… i want that when all the balls are exploitt and you have time remain you win and you lose when you can’t exploit all the balls in the time the is myy code
i hope you can help me …this is my code (sorry for the bad english i am from venezuela)

[code]

o = 0;
time_remain = 10;
time_up = false;
ready = false;
total_balls = 25
–set music
local pop_sound = audio.loadSound(“pop.mp3”)
local win_sound = audio.loadSound(“win.mp3”)

local display_txt = display.newText(“Wait”, 0, 0, native.sistemFont, 16*2);
display_txt.xScale = .5; display_txt.yScale = .5;
display_txt:setReferencePoint(display.bottomLeftReferencePoint);
display_txt.x = 20; display_txt.y = display.contentHeight-20;

local countdowntxt = display.newText(time_remain, 0, 0, native.systemFont, 16*2);
countdowntxt.xScale = .5; countdowntxt.yScale = .5 ;
countdowntxt:setReferencePoint(display.BottomRigthReferencePoint) ;
countdowntxt.x = display.contentWidth-20; countdowntxt.y = display.contentHeight-20;

– add physics engine, start up the engine, and apply gravity
local physics = require(“physics”)
physics.start()
– Set gravity to act “down” (ie, toward the bottom of the device )
physics.setGravity (0, 9)
–set wall image and position
local wall= display.newImage(“bara.png”)
wall.x = display.contentWidth/99,9
–turn wall into physics body
physics.addBody(wall,“static”)
–set wally image and position
local wally = display.newImage(“bara.png”)
wally.x = display.contentWidth/1
–turn wally into physics body
physics.addBody(wally,“static”)

–add floor image and position
local floor = display.newImage(“bar.png”)
floor.y = display.contentHeight/1
– turn floor into physics body
physics.addBody(floor,“static”)

local function trackBalls (obj)
obj:removeSelf()
o = o-1;

if(o == 0) then
audio.play(win_sound);
timer.cancel(gametimer);

end
end

local function countDown(e)
if(time_remain == 10) then
ready = true;
display_txt.text = “Go”;
end

time_remain = time_remain-1;
countdowntxt.text = time_remain;
end

–set ball to stage and position
local function spawnBall()
local ball =display.newImage(“bead2.png”)
ball.x = math.random ( 320 )
ball.y = -100

– turn ball into physics body
physics.addBody(ball,{bounce = 0.1 } )

function ball:touch(e)
if(ready == true ) then
if(e.phase == “ended”) then
–play pop
audio.play(pop_sound)
–remove the balls
trackBalls(self);
end
end

return true;

end
–increment o for every ball created
o = o+1;

ball:addEventListener(“touch”, ball) ;

–if all balls created, start the game timer
if(o == total_balls) then
gametmr = timer.performWithDelay(1000, countDown, 10 );
else
ready = false;

end

end

timer.performWithDelay( 20,spawnBall,total_balls)

[import]uid: 179948 topic_id: 31681 reply_id: 331681[/import]

Hey, I noticed you replied to the other thread too but I’ll go ahead and post a response to this one.

It still looks like you need to acknowledge when time runs out and then set “time_up” to true when that happens.

Try changing “trackBalls()” to

local function trackBalls(obj)  
 obj:removeSelf()  
 o = o-1  
  
 if(time\_up ~= true) then  
  
 if(o == 0) then  
 audio.play(win\_sound)  
 timer.cancel(gametmr)  
 end  
 end  
end  

…And change “countdown()” to:

[code]local function countDown(event)
if(time_remain == 10) then
ready = true
display_txt.text = “Go!”
audio.play(soundtrack, {loops =-1})
end
time_remain = time_remain-1
countdowntxt.text = time_remain

if(time_remain == 0) then
time_up = true

if(o ~= 0) then
audio.play(fail_sound)
display_txt.text = “FAIL”
ready = false
end
end
end
[/code]

Kinda like I was saying in the other post, I think your problem occurs when you try to cancel a timer that has run its course. Hopefully this will prevent that. Good luck!

Brandon [import]uid: 136211 topic_id: 31681 reply_id: 126556[/import]

hey brandon, thank it work a little but not at all…when the total of ball is 25 ,always that i finish to exploit the balls i got a fail text and sound,but when i change the total of ball to five it work perfect when i fail,i got a fail text and fail sound,and when i exploit all the balls the timer stop an play the win sound,thanks for all the help that you gave to my
this is all my code (with 25 = total_balls), if you can help me again… to put 25 as total of balls and that work perfect i appreciated very very much
(sorry for the bad english,i hope you can understand the question)

[code]
o = 0;
time_remain = 10;
time_up = false;
ready = false;
total_balls = 25
–set music
local pop_sound = audio.loadSound(“pop.mp3”)
local win_sound = audio.loadSound(“win.mp3”)
local fail_sound = audio.loadSound(“fail.mp3”)

local display_txt = display.newText(“Wait”, 0, 0, native.sistemFont, 16*2);
display_txt.xScale = .5; display_txt.yScale = .5;
display_txt:setReferencePoint(display.bottomLeftReferencePoint);
display_txt.x = 20; display_txt.y = display.contentHeight-20;

local countdowntxt = display.newText(time_remain, 0, 0, native.systemFont, 16*2);
countdowntxt.xScale = .5; countdowntxt.yScale = .5 ;
countdowntxt:setReferencePoint(display.BottomRigthReferencePoint) ;
countdowntxt.x = display.contentWidth-20; countdowntxt.y = display.contentHeight-20;

– add physics engine, start up the engine, and apply gravity
local physics = require(“physics”)
physics.start()
– Set gravity to act “down” (ie, toward the bottom of the device )
physics.setGravity (0, 20)
–set wall image and position
local wall= display.newImage(“bara.png”)
wall.x = display.contentWidth/99,9
–turn wall into physics body
physics.addBody(wall,“static”)
–set wally image and position
local wally = display.newImage(“bara.png”)
wally.x = display.contentWidth/1
–turn wally into physics body
physics.addBody(wally,“static”)

–add floor image and position
local floor = display.newImage(“bar.png”)
floor.y = display.contentHeight/1
– turn floor into physics body
physics.addBody(floor,“static”)

local function trackBalls(obj)
obj:removeSelf()
o = o-1

if(time_up ~= true) then

if(o == 0) then
audio.play(win_sound)
timer.cancel(gametmr)
end
end
end

local function countDown(event)
if(time_remain == 10) then
ready = true
display_txt.text = “Go!”
audio.play(soundtrack, {loops =-1})
end
time_remain = time_remain-1
countdowntxt.text = time_remain

if(time_remain == 0) then
time_up = true

if(o ~= 0) then
audio.play(fail_sound)
display_txt.text = “FAIL”
ready = false
end
end
end

–set ball to stage and position
local function spawnBall()
local ball =display.newImage(“bead2.png”)
ball.x = math.random ( 320 )
ball.y = -100

– turn ball into physics body
physics.addBody(ball,{bounce = 0.3 } )

function ball:touch(e)
if(ready == true ) then
if(e.phase == “ended”) then
–play pop
audio.play(pop_sound)
–remove the balls
trackBalls(self);
end
end

return true;

end
–increment o for every ball created
o = o+1;

ball:addEventListener(“touch”, ball) ;

–if all balls created, start the game timer
if(o == total_balls) then
gametmr = timer.performWithDelay(1000, countDown, 10 );
else
ready = false;

end

end

timer.performWithDelay( 20,spawnBall,total_balls) [import]uid: 179948 topic_id: 31681 reply_id: 126602[/import]

hi brandon sorry for annoy again but today in the morning i was testing the app and when i change the total of balls to 25 never i seen 25 ball normally i see 12 or 15 balls i guest that this is error but i don’t now how fixed,and when i change the total of balls to 5 sometimes i got only 4 or 3 ball and when i exploit all the timer didn’t stop ,when time is out i got fail and fail sound if you scan help me to fix that again i appreciated very much [import]uid: 179948 topic_id: 31681 reply_id: 126641[/import]

Hey, I noticed you replied to the other thread too but I’ll go ahead and post a response to this one.

It still looks like you need to acknowledge when time runs out and then set “time_up” to true when that happens.

Try changing “trackBalls()” to

local function trackBalls(obj)  
 obj:removeSelf()  
 o = o-1  
  
 if(time\_up ~= true) then  
  
 if(o == 0) then  
 audio.play(win\_sound)  
 timer.cancel(gametmr)  
 end  
 end  
end  

…And change “countdown()” to:

[code]local function countDown(event)
if(time_remain == 10) then
ready = true
display_txt.text = “Go!”
audio.play(soundtrack, {loops =-1})
end
time_remain = time_remain-1
countdowntxt.text = time_remain

if(time_remain == 0) then
time_up = true

if(o ~= 0) then
audio.play(fail_sound)
display_txt.text = “FAIL”
ready = false
end
end
end
[/code]

Kinda like I was saying in the other post, I think your problem occurs when you try to cancel a timer that has run its course. Hopefully this will prevent that. Good luck!

Brandon [import]uid: 136211 topic_id: 31681 reply_id: 126556[/import]

hey brandon, thank it work a little but not at all…when the total of ball is 25 ,always that i finish to exploit the balls i got a fail text and sound,but when i change the total of ball to five it work perfect when i fail,i got a fail text and fail sound,and when i exploit all the balls the timer stop an play the win sound,thanks for all the help that you gave to my
this is all my code (with 25 = total_balls), if you can help me again… to put 25 as total of balls and that work perfect i appreciated very very much
(sorry for the bad english,i hope you can understand the question)

[code]
o = 0;
time_remain = 10;
time_up = false;
ready = false;
total_balls = 25
–set music
local pop_sound = audio.loadSound(“pop.mp3”)
local win_sound = audio.loadSound(“win.mp3”)
local fail_sound = audio.loadSound(“fail.mp3”)

local display_txt = display.newText(“Wait”, 0, 0, native.sistemFont, 16*2);
display_txt.xScale = .5; display_txt.yScale = .5;
display_txt:setReferencePoint(display.bottomLeftReferencePoint);
display_txt.x = 20; display_txt.y = display.contentHeight-20;

local countdowntxt = display.newText(time_remain, 0, 0, native.systemFont, 16*2);
countdowntxt.xScale = .5; countdowntxt.yScale = .5 ;
countdowntxt:setReferencePoint(display.BottomRigthReferencePoint) ;
countdowntxt.x = display.contentWidth-20; countdowntxt.y = display.contentHeight-20;

– add physics engine, start up the engine, and apply gravity
local physics = require(“physics”)
physics.start()
– Set gravity to act “down” (ie, toward the bottom of the device )
physics.setGravity (0, 20)
–set wall image and position
local wall= display.newImage(“bara.png”)
wall.x = display.contentWidth/99,9
–turn wall into physics body
physics.addBody(wall,“static”)
–set wally image and position
local wally = display.newImage(“bara.png”)
wally.x = display.contentWidth/1
–turn wally into physics body
physics.addBody(wally,“static”)

–add floor image and position
local floor = display.newImage(“bar.png”)
floor.y = display.contentHeight/1
– turn floor into physics body
physics.addBody(floor,“static”)

local function trackBalls(obj)
obj:removeSelf()
o = o-1

if(time_up ~= true) then

if(o == 0) then
audio.play(win_sound)
timer.cancel(gametmr)
end
end
end

local function countDown(event)
if(time_remain == 10) then
ready = true
display_txt.text = “Go!”
audio.play(soundtrack, {loops =-1})
end
time_remain = time_remain-1
countdowntxt.text = time_remain

if(time_remain == 0) then
time_up = true

if(o ~= 0) then
audio.play(fail_sound)
display_txt.text = “FAIL”
ready = false
end
end
end

–set ball to stage and position
local function spawnBall()
local ball =display.newImage(“bead2.png”)
ball.x = math.random ( 320 )
ball.y = -100

– turn ball into physics body
physics.addBody(ball,{bounce = 0.3 } )

function ball:touch(e)
if(ready == true ) then
if(e.phase == “ended”) then
–play pop
audio.play(pop_sound)
–remove the balls
trackBalls(self);
end
end

return true;

end
–increment o for every ball created
o = o+1;

ball:addEventListener(“touch”, ball) ;

–if all balls created, start the game timer
if(o == total_balls) then
gametmr = timer.performWithDelay(1000, countDown, 10 );
else
ready = false;

end

end

timer.performWithDelay( 20,spawnBall,total_balls) [import]uid: 179948 topic_id: 31681 reply_id: 126602[/import]

hi brandon sorry for annoy again but today in the morning i was testing the app and when i change the total of balls to 25 never i seen 25 ball normally i see 12 or 15 balls i guest that this is error but i don’t now how fixed,and when i change the total of balls to 5 sometimes i got only 4 or 3 ball and when i exploit all the timer didn’t stop ,when time is out i got fail and fail sound if you scan help me to fix that again i appreciated very much [import]uid: 179948 topic_id: 31681 reply_id: 126641[/import]