Help with a Delay


(Secret) #1

Hi guys. So I wanted to make a 3 second delay before going in to the next command which on the ff. code is setting a variable for all Tile Entity. The problem however is that it doesn’t seem to work and the tiles instantly closes as soon as the game starts. What am I doing wrong?

Btw, this code is inside the constructor for the World.

		var timer:Number = 0;
		while(timer < 3){
			timer += FP.elapsed;
			trace(timer);
		}

		for each (var q:Tile in gameTiles){
			q.animatingClose = true;
		}

(Secret) #3

Hmm. no it doesn’t work.


(JP Mortiboys) #4

See this thread - while and other control structures just don’t work that way.


(Secret) #5

Yea, I realized I made the same mistake. I got it working by putting the check inside the Entity class itself.


(Jacob Albano) #6

Use an alarm tween!

function onDelay()
{
    for each (var q:Tile in gameTiles)
    {
        q.animatingClose = true;
    }
}

addTween(new Alarm(3, onDelay, Tweener.ONESHOT), true);

(Secret) #7

That’s interesting. I’m not sure if I understand the concept of Tweens though, are there any reading materials you can recommend?


(Jacob Albano) #8

Basically, a Tween is a class that interpolates a value from a start point to an end point. Flashpunk has a bunch of them built in, and they have a bunch of applications; you can use Vartweens and Multivartweens to make your game feel “juicy” by adding bounces and wobbles and things to your objects, or motion tweens to move your objects along paths like quadratic curves…

The Alarm tween is a bit different from the rest. It simply takes a duration and a complete callback function, which it runs when the specified time has passed. ONESHOT is an optional argument that tells the Tweener (the world or entity you added the alarm to) to remove the alarm as soon as it’s finished.

Here’s a very informative thread about getting started with tweens of all types.