0
votes

The following as3 is inside a movie clip of instance name 'counter_int' on root level and the targeted movieclip instance name 'win_message' lives separately on root level too.

Prior to using the listener/function and if statement method below, this worked perfectly and the 'win_message' movicelip played fine WHEN entering a new frame within 'counter_int' which contained the same as3, minus the listener and if statement.

All still works except i.e. it still communicates with targeted movicelip due to the fact, it, the 'win_message' movieclip stops on frame 2, instead of continuing to play. There are no stop(); commands in the targeted movieclip other than on frame 1 btw.

addEventListener(Event.ENTER_FRAME, success);

function success(ev : Event) : void

{
    if (linked_total.text == (5).toString () )
    { 
       Object(root).win_message.gotoAndPlay(2);
    }
}

Any help would be much appreciated! Apologies if I'm not great at explaining my problem!

Thanks

1

1 Answers

2
votes

Your problem is because you are using the gotoAndPlay() inside the listener of the Event.ENTER_FRAME event, so it's called repeatedly until the condition is false.

Although, It's a bad manner to do like that, you can instead use any punctual action (button click, text field change, ...) which you know that will be executed just when your user need that.

If you still need, for any other cause, to use the Event.ENTER_FRAME event listener, you should ensure the your condition will be true just once ( or at least you will call gotoAndPlay() once ), then you can call the gotoAndPlay(), by using a Boolean var for example :

var first_run:Boolean = true;

addEventListener(Event.ENTER_FRAME, success);
function success(ev:Event) : void
{
    // when first_run is false, we are sure that even if linked_total.text still equals to "5"
    // this condition will always be false
    if (int(linked_total.text) == 5 && first_run)
    {       
        first_run = false;
        MovieClip(root).win_message.gotoAndPlay(2);
    }
}

You can also remove the event listener when the condition is true for the first time, but in this case, I don't know why you will use the Event.ENTER_FRAME event listener !

function success(ev:Event) : void
{
    if (int(linked_total.text) == 5)
    {       
        removeEventListener(Event.ENTER_FRAME, success);
        MovieClip(root).win_message.gotoAndPlay(2);
    }
}

... There are really many manners to accomplish what you want with avoiding the behavior that you got.

Hope that can help.