fat_mike
fat_mike

Reputation: 887

AS3 loop doesn't work

I want after the 3rd timer to create a loop until the end of the program and then back at the 3rd timer but the program stops after the last timer. I tried "for" function too, but without success.

Here is my code:

var i:Number = 0;

disco.visible = false;
palouki.visible = false;
konto.visible = false;
runner.visible = false;
mtext.visible = false;
container.visible = false;
atfa.visible = false;


var playert1: Timer = new Timer(1000, 1);
var playert2: Timer = new Timer(1500, 1);
var playert3: Timer = new Timer(2000, 1);
var playert4: Timer = new Timer(2500, 1);
var playert5: Timer = new Timer(3000, 1);
var playert6: Timer = new Timer(3500, 1);
var playert7: Timer = new Timer(5000, 1);


playert1.addEventListener(TimerEvent.TIMER, timert1F);
playert2.addEventListener(TimerEvent.TIMER, timert2F);
playert3.addEventListener(TimerEvent.TIMER, timert3F);
playert4.addEventListener(TimerEvent.TIMER, timert4F);
playert5.addEventListener(TimerEvent.TIMER, timert5F);
playert6.addEventListener(TimerEvent.TIMER, timert6F);
playert7.addEventListener(TimerEvent.TIMER, timert7F);

playert1.start();
playert2.start();
playert3.start();
playert4.start();
playert5.start();
playert6.start();
playert7.start();


function timert1F(e:TimerEvent):void{
    mtext.visible = true;
    TweenLite.from(mtext, 0.5, {alpha:0});

}

function timert2F(e:TimerEvent):void{
    container.visible = true;
    TweenLite.from(container, 0.5, {alpha:0});
}

while (i<100){

    function timert3F(e:TimerEvent):void{
        disco.visible = true;
        TweenLite.from(disco, 0.5, {alpha:0});
    }

    function timert4F(e:TimerEvent):void{
        runner.visible = true;
        TweenLite.from(runner, 0.5, {alpha:0});
    }
    function timert5F(e:TimerEvent):void{
        palouki.visible = true;
        TweenLite.from(palouki, 0.5, {alpha:0});
    }
    function timert6F(e:TimerEvent):void{
        konto.visible = true;
        TweenLite.from(konto, 0.5, {alpha:0});
    }

    function timert7F(e:TimerEvent):void{
        atfa.visible = true;
        TweenLite.from(atfa, 0.5, {alpha:0});
    }

trace(i);
i++;
}
stop();

Upvotes: 1

Views: 477

Answers (1)

Josh
Josh

Reputation: 8159

You have your scope completely screwed up here.

Basics of scope:

  • Any object or function declared within a function or loop is only accessible from within that function or loop
  • The same is true for objects or functions declared in a class, unless an access modifier other than "private" is used
  • Any object declared in a loop is accessible only within that iteration of the loop. So an object declared in iteration 1 will never be accessible in iterations 2-100.

So with those rules, we can point out one problem in your code: you are declaring a function within a loop and trying to access it from outside that loop. When you are attaching those event listeners, those functions do not technically exist.

Now onto your second problem. You are declaring a series of functions in that loop 100 times (which is a GIGANTIC memory issue, for the record). Declaring a function is not running a function. So you create these functions but they are never actually run, which is why this doesn't work the way you are expecting them to.

If you want those functions to run 100 times, you need to declare the functions outside of the loop in global space and then call the functions within the loop. But you are using a Tween (via TweenLite) in those functions. Your loop will run in less than a second. So calling it 100 times will do absolutely nothing since it will only result in one tween actually appearing.

You really need to do some research into how scope, loops, and tweens work. The code you provided is an incredible mess at the moment.

Upvotes: 5

Related Questions