Page 1 of 1

Looping timers?

PostPosted: Tue Dec 08, 2015 12:22 am
by Spectrogram
I know that there is probably an easy solution, but such solution I am having trouble finding.

How could I do the following and have it work....
- I have an actor (a laser, in this case) that is created, plays the 'on' animation and sets the variable 'laserOn' to 1.
- 2 seconds later the animation switches to 'off' and laserOn is set to 0.
- 2 seconds later it goes back to 'on' and such, looping over and over again until it is destroyed.

Any way of doing this?
Thanks for any replies! :)

Re: Looping timers?

PostPosted: Tue Dec 08, 2015 6:57 am
by skydereign
You can create a timer with CreateTimer that is set to loop infinitely. Create the timer in your laser's create actor event, and set laserOn to 1. In the actual timer event, you can do something like this (replacing the comment with however you are toggling the animation).

laser -> Timer Event -> Script Editor
Code: Select all
switch(laserOn)
{
    case 0:
    // switch the animation on
    laserOn = 1;
    break;

    case 1:
    // switch animation off
    laserOn = 0;
    break;
}

Re: Looping timers?

PostPosted: Tue Dec 08, 2015 8:59 pm
by Spectrogram
What you had was very similar to what I had before, and it works so long as the actor is created at startup, though it doesn't seem to work when the actor is created based on a script event, nor when the actor is cloned. When it's cloned, the laser.0 doesn't change the animation (my guess is the script looks for "laser" when it's now "laser.0") and laser.1, laser.2, ect... doesn't change at all. Is there a way to do this where it works with clones or in script CreateActor events, rather than making multiple actors?

Thanks!

Re: Looping timers?

PostPosted: Wed Dec 09, 2015 12:51 am
by skydereign
Spectrogram wrote:When it's cloned, the laser.0 doesn't change the animation (my guess is the script looks for "laser" when it's now "laser.0") and laser.1, laser.2, ect... doesn't change at all. Is there a way to do this where it works with clones or in script CreateActor events, rather than making multiple actors?

That shouldn't be the case. Is laserOn an actor variable? It needs to be for it to work with clones. I attached an example of what I assume you were asking. Instead of animations though I just changed the actor's color. You can press space to create more laser actors.

-Edit
Actually from what you said, are you using ChangeAnimation("laser", "some_anim", FORWARD) or CreateTimer("laser", ...)? If so that is the problem. You should be using "Event Actor", which will only effect the current clone. That includes the CreateTimer event. In any script, if you want to target the actor executing the script, you should be using "Event Actor".

Re: Looping timers?

PostPosted: Wed Dec 09, 2015 9:49 pm
by Spectrogram
Ah, yes, thank you. The example you provided works for me! :)

skydereign wrote:Are you using ChangeAnimation("laser", "some_anim", FORWARD) or CreateTimer("laser", ...)? If so that is the problem.

I had changed the script events to "Event Actor", but that still didn't solve the problem, the problem (comparing it to yours) was part of how I toggled the actors.

Thanks! (I can now resume progress on my first game)

Re: Looping timers?

PostPosted: Wed Feb 15, 2017 7:01 am
by Developer123
To implement a countdown timer, we will create two objects: a field to display the current timer and a button to start the countdown. We will code two handlers: one for the button and one for the timer.

Perform the following steps to create a countdown timer:

Create a new main stack.
Place a field on the stack's card and name it timerDisplay.
Place a button on the stack's card and name it Count Down.
Add the following code to the Count Down button:
on mouseUp
local pTime

put 19 into pTime
put pTime into fld "timerDisplay"
countDownTimer pTime
end mouseUp
Add the following code to the Count Down button:
on countDownTimer currentTimerValue
subtract 1 from currentTimerValue
put currentTimerValue into fld "timerDisplay"
if currentTimerValue > 0 then
send "countDownTimer" && currentTimerValue to me
in 1 sec
end if
end countDownTimer
Test the code using a mobile simulator or an actual device.
To implement our timer, we created a simple callback situation where the countDownTimer method will be called each second until the timer is zero. We avoided the temptation to use a repeat loop because that would have blocked all other messages and introduced unwanted app behavior.

Moderator's note: Advertisements removed, warning 1/3 issued.

Re: Looping timers?

PostPosted: Wed Feb 15, 2017 8:37 am
by DeltaLeeds
Developer123 wrote:To implement a countdown timer, we will create two objects: a field to display the current timer and a button to start the countdown. We will code two handlers: one for the button and one for the timer.

Perform the following steps to create a countdown timer:

Create a new main stack.
Place a field on the stack's card and name it timerDisplay.
Place a button on the stack's card and name it Count Down.
Add the following code to the Count Down button:
on mouseUp
local pTime

put 19 into pTime
put pTime into fld "timerDisplay"
countDownTimer pTime
end mouseUp
Add the following code to the Count Down button:
on countDownTimer currentTimerValue
subtract 1 from currentTimerValue
put currentTimerValue into fld "timerDisplay"
if currentTimerValue > 0 then
send "countDownTimer" && currentTimerValue to me
in 1 sec
end if
end countDownTimer
Test the code using a mobile simulator or an actual device.
To implement our timer, we created a simple callback situation where the countDownTimer method will be called each second until the timer is zero. We avoided the temptation to use a repeat loop because that would have blocked all other messages and introduced unwanted app behavior.

Moderator's note: Advertisements removed, warning 1/3 issued.


Hello and welcome to the forums, what programming language or programming is that for? Don't think anyone using gE would be familiar with that code you posted. GE uses C programming language and doesn't have any functions to place a button or have the function put and etc.