Count Down Loop

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
bhh32
Posts: 7
Joined: Tue Feb 16, 2016 8:31 am

Count Down Loop

Post by bhh32 » Tue Mar 08, 2016 11:53 pm

I want to count down from 60 (1 minute) for a game that I'm making. I apologize in advance because this may be a really easy solution. I have been trying to use repeat, but it just counts down too fast. It to countdown a second at a time. Here is the code I have so far.

local timerCount

on mouseUp
put 60 into timerCount
repeat
send startTimer to me in 1 second
end repeat
end mouseUp

on startTimer
subtract 1 from timerCount

if timerCount > 0 then
put timerCount into field "Timer"
send timerCount to me in 1 second
else
exit repeat
put timerCount into field "Timer"
answer "To start the next wave press Start Now!"
end if
end startTimer

So I tried the send ... in 1 second before I tried repeat... it just hangs on 59 every time.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Count Down Loop

Post by dunbarx » Wed Mar 09, 2016 12:25 am

Hi

Have you stepped through your handlers? Definitely lose the repeat in the "mouseUp" handler. That looks like trouble. As payment for the following (and I have not tested it, but it solves at least some of your issues), you have to examine everything and tell me what you find.

Code: Select all

on mouseUp
   send startTimer && "60" to me in 1 second
end mouseUp

on startTimer timerCount
   if the optionKey is down then exit to top --always add something like this as an escape
   subtract 1 from timerCount
   
   if timerCount > 0 then
      put timerCount into field "Timer"
      send "startTimer" && timerCount to me in 1 second -- "startTimer" NOT "timerCount", and you need to reaffirm the parameter "timerCount" each pass
   else
      exit repeat  -- I JUST BET this needs to be moved, eh?
      put timerCount into field "Timer"
      answer "To start the next wave press Start Now!"
   end if
end startTimer

Craig Newman

bhh32
Posts: 7
Joined: Tue Feb 16, 2016 8:31 am

Re: Count Down Loop

Post by bhh32 » Wed Mar 09, 2016 9:07 am

Thank you so much. That totally works! I'm not sure what you mean by step through each handler. I prefer languages such as Java, C++, and C#. I don't know why this goes so far over my head.

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Count Down Loop

Post by AxWald » Wed Mar 09, 2016 11:45 am

Hi,

a similar approach is shown here.

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Count Down Loop

Post by dunbarx » Wed Mar 09, 2016 3:18 pm

Hi.

It worked completely? Hmmm. Lucky, I guess.

So in those fancy other languages, how do you debug your code? I assume you are able to step through, line by line, and examine the contents of variables, perhaps while watching the evolution of your project.

Look up "breakpoint" in the dictionary, and do install one in the lower handler. Oh, and once you get the hang of LC, you will never go back.

Craig

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: Count Down Loop

Post by Newbie4 » Wed Mar 09, 2016 3:55 pm

more on timers and different types here: Section 5 https://sites.google.com/a/pgcps.org/li ... rogramming
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Count Down Loop

Post by jacque » Wed Mar 09, 2016 4:46 pm

bhh32 wrote:Thank you so much. That totally works! I'm not sure what you mean by step through each handler. I prefer languages such as Java, C++, and C#. I don't know why this goes so far over my head.
It's because there's more to any language than just learning the syntax and vocabulary, you also need to know the behaviors. LiveCode is event based and acts differently than the languages you're used to. There is no main event loop, so you need to change your approach a bit.

Your original mouseUp handler has an infinite loop, there's no exit condition in the repeat. It will cycle endlessly, piling up pending messages forever. It will also block everything else, so LiveCode will appear to freeze. When you send a message in time, the message is queued and does not activate until the sending handler completes. Since your handler is in an infinite loop, it never completes.

There are ways around that but the better approach is what Craig suggested. Use the mouseUp to trigger the countdown and then send the next message from within the counting handler until a certain condition is met.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bhh32
Posts: 7
Joined: Tue Feb 16, 2016 8:31 am

Re: Count Down Loop

Post by bhh32 » Thu Mar 10, 2016 7:31 pm

Thank you everyone. I'm having a problem now with clicking the main menu button and it throwing an error when I do. The error is when it goes back to the main menu card and the timer hasn't finished its loop it tells me button "startButton": execution error at line 16 (Chunk: no such object), char 1 and gives the orange circle with an arrow on line 16 "put timerCount into field "Timer"

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Count Down Loop

Post by dunbarx » Thu Mar 10, 2016 8:03 pm

Hi.

You are moving around while the clock is running. This gives you flexibility, since the "send in time" gadget is not blocking. But if you are loading data into a field on a card, and you leave that card, what is the handler supposed to do? How do it know?

The most robust way is to explicitly tell LC what you want. I used to give this as homework, but, well, here:

Code: Select all

put timerCount into field "Timer" of cd "theCorrectCard"
Craig

bhh32
Posts: 7
Joined: Tue Feb 16, 2016 8:31 am

Re: Count Down Loop

Post by bhh32 » Fri Mar 11, 2016 12:26 am

Thank you Craig. That definitely did the trick. I've found out that I cannot subtract a variable holding a number from another variable holding a number. Is it just a bug I've run into or is this actually the case?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Count Down Loop

Post by dunbarx » Fri Mar 11, 2016 1:56 am

Hi.

I doubt this is the case. Why not:

Code: Select all

if var1 is a number and var2 is a number then subtract var1 from var2
But better, you need to validate them from early on. Try something like that in your handler. I bet you will find that something is turning a number into a string.

Craig

KatieKat123543
Posts: 1
Joined: Fri Mar 11, 2016 10:33 am

Re: Count Down Loop

Post by KatieKat123543 » Fri Mar 11, 2016 10:41 am

Hi,
I'm having a similar problem with my timer - basically it counts down to zero, says your time is up but keeps letting you play the game. Now i'm being told this: " execution error at line n/a (Object: can't set script while it is executing)"
What do I do??
Thanks

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Count Down Loop

Post by Klaus » Fri Mar 11, 2016 12:35 pm

Hi KatieKat123543,

1. welcome to the forum! :D

2.
KatieKat123543 wrote:(Object: can't set script while it is executing)"
well, just what it says!

Obviously the script is still running, maybe due to some WAIT statements in it,
and you cannot change it until it has finished. So wait a little and then try again.

Please post your script so we can surely remove this inconvenience!


Best

Klaus

bhh32
Posts: 7
Joined: Tue Feb 16, 2016 8:31 am

Re: Count Down Loop

Post by bhh32 » Fri Mar 11, 2016 3:00 pm

Craig, the code was:

global bank -- given a number in the stack that was carried over to the card, and it worked
global cost

put 150 into cost -- cost of the tower

on mouseUp
if bank > cost then
subtract cost from bank
else if bank = cost then
subtract cost from bank
else
answer "You don't have enough money!"
end if
end mouseUp

The above code wasn't working, but when I put the number that was supposed to be cost into all the cost spots, the button works fine.

bhh32
Posts: 7
Joined: Tue Feb 16, 2016 8:31 am

Re: Count Down Loop

Post by bhh32 » Fri Mar 11, 2016 3:13 pm

KatieKat,
I got around your problem by making a it go back to the level select card when the game is over and also making a gameRunning boolean. gameRunning is false when the card opens, you press the start button and it becomes true and everything is depedent on gameRunning being true. I put it in my waveNumberUpdate command:

command waveNumberUpdate
if waveNumber <= field "Wave" then
add 1 to waveNumber
put waveNumber into field "Wave'
if waveNumber = 10 then
subtract 1 from waveNumber
answer "You Beat the Level!"
wait .5 seconds
put false into gameRunning
go to card "Level Select Card"
end if
end if
end waveNumberUpdate

The waveNumberUpdate command is then called in the in on startTimer:

// Starts and runs the timer, initiates new enemy wave, and takes lives
on startTimer
subtract 1 from timerCount
if timerCount > 0 then
put timerCount into field "Timer" of cd "Level 15"
send "startTimer" && timerCount to me in 1 second
else if waveNumber > 10 then // Ensures timer doesn't continue when Wave 10 is passed.
else
takeLife
waveNumberUpdate -- Also called here to update the wave number after each wave
put 60 into timerCount
put timerCount into field "Timer"
send "startTimer" && timercount to me in 1 second
enemyStart
end if
end startTimer

The takeLife command also sets gameRunning to false then takes the user back to the Level Select Card after all of the lives have been lost.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”