Looping timers?

Non-platform specific questions.

Looping timers?

Postby Spectrogram » Tue Dec 08, 2015 12:22 am

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! :)
User avatar
Spectrogram
 
Posts: 36
Joined: Fri Aug 28, 2015 4:35 pm
Location: 0x7C00
Score: 3 Give a positive score

Re: Looping timers?

Postby skydereign » Tue Dec 08, 2015 6:57 am

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;
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Looping timers?

Postby Spectrogram » Tue Dec 08, 2015 8:59 pm

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!
User avatar
Spectrogram
 
Posts: 36
Joined: Fri Aug 28, 2015 4:35 pm
Location: 0x7C00
Score: 3 Give a positive score

Re: Looping timers?

Postby skydereign » Wed Dec 09, 2015 12:51 am

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".
Attachments
timer.ged
(1.46 KiB) Downloaded 149 times
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Looping timers?

Postby Spectrogram » Wed Dec 09, 2015 9:49 pm

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)
User avatar
Spectrogram
 
Posts: 36
Joined: Fri Aug 28, 2015 4:35 pm
Location: 0x7C00
Score: 3 Give a positive score

Re: Looping timers?

Postby Developer123 » Wed Feb 15, 2017 7:01 am

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.
Developer123
 
Posts: 1
Joined: Wed Nov 23, 2016 8:33 am
Score: 1 Give a positive score

Re: Looping timers?

Postby DeltaLeeds » Wed Feb 15, 2017 8:37 am

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.
Currently: Semi-Active.
Working on: Maybe putting my test games in gamejolt, polishing them a bit, but I still don't know when.
User avatar
DeltaLeeds
 
Posts: 693
Joined: Fri May 06, 2011 12:45 pm
Location: In front of my computer.
Score: 38 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron