Tieme
Tieme

Reputation: 65479

AS3.0 Two For-loops with a delay

I've got the following code and i would like to add an delay of 200 ms after each trace statement

for (var x_pos:uint = 0; x_pos <= 12; x_pos++){ 
    for (var y_pos:uint = 0; y_pos <=12; y_pos++){
        trace("hello world " +"("x_pos+","+y_pos+")");
        //CODE FOR DELAY OF 200ms    
    }
}

The real situation is a bit more complex but kind of the same:

//For each Row 
for (var x_pos:uint = 0; x_pos <= tile_amount-1; x_pos++){
    //For each column 
    for (var y_pos:uint = 0; y_pos <= tile_amount-1; y_pos++){
        //New tile;
        var newtile:Tile = new Tile;

        //Set position
        newtile.x = ((40*y_pos)-(40*x_pos));
        newtile.y = ((20*y_pos)+(20*x_pos));

        //Add to stage 
        addChild(newtile);
    }
}

Anyone any suggestions ?

Upvotes: 1

Views: 2381

Answers (4)

Jevgenij Dmitrijev
Jevgenij Dmitrijev

Reputation: 2238

You can not delay the loops in as3.

For this purpose you need to use timers. Some help for your solution you can find here: How to show the current progressBar value of process within a loop in flex-as3?

At the end you just need to modify the function logic.

Upvotes: 0

alecmce
alecmce

Reputation: 1456

private var x_pos:uint;
private var y_pos:uint;
private var timer:Timer;

public function startLoop():void
{
    x_pos = 0;
    y_pos = 0;

    timer = new Timer(200);
    timer.addEventListener(TimerEvent.TIMER, onTick);
    timer.start();
}

private function onTick(event:TimerEvent):void
{
    trace("hello world " +"("x_pos+","+y_pos+")");

    if (++y_pos <= 12)
        return;

    y_pos = 0;
    if (++x_pos <= 12)
        return;

    timer.stop();
    timer.removeEventListener(TimerEvent.TIMER, onTick);
    timer = null;
}

Upvotes: 4

cwallenpoole
cwallenpoole

Reputation: 82078

Actionscript does not have a blocking timeout system -- you need to do a recursive function of your own. This following perfect, but it is a start.

import flash.utils.setTimeout;

// call the final function.
delayedRecursion(12,12,200, 
   function(curX:Number, curY:Number):void
   {
      trace("hello world " +"("+curX+","+curY+")");
   });

//This is really a wrapper around delayedRecursionHelper
function delayedRecursion(maxX:Number, maxY:Number, 
                          delay:Number, callback:Function):void
{
     delayedRecursionHelper(0,-1,maxX,maxY,delay,callback);
}

// each time you call this, it creates a function which holds the variables
// passed in, but incremented by 1.
function delayedRecursionHelper( 
                                 curX:Number, cury:Number, 
                                 maxX:Number, maxY:Number, 
                                 delay:Number, called:Function ):Function
{
    return function():void
    {
        called(curX, curY);
        // Exit condition: nothing to do here!
        if( curX == maxX && curY == maxY ) return; 
        if( curY == maxY )
        {
            curY = -1;
            curX++;
        }
        curY++;
        setTimeout(delayedRecursionHelper(curX, curY, maxX, maxY, delay), delay);
    }
}

Upvotes: 0

grapefrukt
grapefrukt

Reputation: 27045

You can't stop the execution of code in the middle of a statement like that, your best bet is to use a timer:

package 
{
    import flash.events.TimerEvent;

    public class Foo
    {

        private var x_pos:uint = 0;
        private var y_pos:uint = 0;
        private var timer:Timer;

        public function Foo()
        {
            timer = new Timer(200, 0);
            timer.addEventListener(TimerEvent.TIMER, handleTick);
            timer.start();
        }


        public function handleTick(e:TimerEvent):void {

            trace("hello world " +"("x_pos+","+y_pos+")");
            y_pos++;
            if(y_pos > 12){
                x_pos++;
                y_pos = 0;
            }

            if(x_pos > 12) timer.stop();
        }

    }
}

Upvotes: 3

Related Questions