Count Down Loop
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Count Down Loop
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Count Down Loop
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.
Craig Newman
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
Re: Count Down Loop
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.
Re: Count Down Loop
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!
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!
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Count Down Loop
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
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
-
- VIP Livecode Opensource Backer
- Posts: 327
- Joined: Sun Apr 15, 2012 1:17 am
- Location: USA
- Contact:
Re: Count Down Loop
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/
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
-
- VIP Livecode Opensource Backer
- Posts: 7230
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Count Down Loop
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.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.
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
HyperActive Software | http://www.hyperactivesw.com
Re: Count Down Loop
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"
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Count Down Loop
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:
Craig
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"
Re: Count Down Loop
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?
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Count Down Loop
Hi.
I doubt this is the case. Why not:
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
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
Craig
-
- Posts: 1
- Joined: Fri Mar 11, 2016 10:33 am
Re: Count Down Loop
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
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
Re: Count Down Loop
Hi KatieKat123543,
1. welcome to the forum!
2.
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
1. welcome to the forum!
2.
well, just what it says!KatieKat123543 wrote:(Object: can't set script while it is executing)"
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
Re: Count Down Loop
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.
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.
Re: Count Down Loop
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.
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.