Khaled Garbaya
Khaled Garbaya

Reputation: 1497

Timer in Starling Framework

Hello everybody I am developing a game with starling and i want to set a timer for example every 2 sec i want something to happen. I used the juggler elapsed prop but i wonder is there a more effecient way to do that

thank you,

Khaled

Upvotes: 2

Views: 4752

Answers (2)

PrimaryFeather
PrimaryFeather

Reputation: 489

Alternatively, you can use the "DelayedCall" class. It's easy to miss! ;-)

var delayedCall:DelayedCall = new DelayedCall(method, 2.0);
delayedCall.repeatCount = int.MAX_VALUE;
Starling.juggler.add(delayedCall);

function method():void
{
    trace("ping");
}

Upvotes: 13

Jason Sturges
Jason Sturges

Reputation: 15955

If this does not relate to animation, it is recommended to use a Timer for non-animated content.

Timer implementation would be higher performance than additional time calculations on enter frame handler.

If you are advancing Starling Jugglers, you can set the frame rate of the Juggler to every 2-seconds.

Jugglers also have delayCall in which you could infinitely loop every 2-seconds if your functor redundantly called delayCall:

juggler.delayCall(functor, 2.0);

To tie in to Starlings frame / render lifecycle, you can test time since your last call.

private var lastCallTime:int

protected function frameHandler():void
{
    var now:int = getTimer();
    var ellapsed:int = now - lastCallTime;
    if(ellapsed >= 2000)
    {
        /* execute implementation */
        lastCallTime = now;
    }
}

Upvotes: 2

Related Questions