8-bit mate
8-bit mate

Reputation: 85

While loop not working in ActionScript 3.0

I'm trying to create a function (called by a mouseover event) which will play the movie backwards while the mouse is over the instance, and stop while it's not.

This is what I have so far:

var b2:rightButton = new rightButton(); //new instance
b2.X = 550; //instances position
addChild(b2); //add instance to stage
b2.alpha = .4; // set instances alpha
var num = 0; // new variable called 'num'
b2.addEventListener(MouseEvent.ROLL_OVER, rightScroll); //mouse event for roll over 

function rightScroll(event:MouseEvent) { //the function
    num = 1; //set num to 1
    b2.alpha = .8; //set alpha to 80%
}

b2.addEventListener(MouseEvent.ROLL_OUT, no_rightScroll); //event for roll out

function no_rightScroll(event:MouseEvent){ //roll- out function
    num = 0; //set num back to 0
    b2.alpha = .4; //set alpha back to 40%
}

while (num == 1){ // while num =1 (while mouse is over)
    prevFrame(); //goto previous frame
}

Does anyone knows how to fix that, or a better way to do this?

Upvotes: 1

Views: 872

Answers (2)

shanethehat
shanethehat

Reputation: 15580

The problem with your code at the moment is that loops do not execute in line with the framerate of your movie, so your code will call prevFrame() many, many times in a single frame. This can result in endless loops, which will cause your program to crash, and is completely useless in terms of animation.

The best approach is to listen to the ENTER_FRAME event of your movie, and move the playhead back one frame each time the event is dispatched. By adding and removing the listener in your button actions, you will get the effect you're looking for:

var b2:rightButton = new rightButton(); //new instance
b2.X = 550; //instances position
addChild(b2); //add instance to stage
b2.alpha = .4; // set instances alpha

b2.addEventListener(MouseEvent.ROLL_OVER, rightScroll); //mouse event for roll over 
function rightScroll(event:MouseEvent):void { //the function
    stage.addEventListener(Event.ENTER_FRAME,moveBackwards); //add stage listener
    b2.alpha = .8; //set alpha to 80%
}

b2.addEventListener(MouseEvent.ROLL_OUT, no_rightScroll); //event for roll out
function no_rightScroll(event:MouseEvent):void { //roll- out function
    stage.removeEventListener(Event.ENTER_FRAME,moveBackwards); //remove stage listener
    b2.alpha = .4; //set alpha back to 40%
}

function moveBackwards(evt:Event):void {
    prevFrame();
}

Upvotes: 4

James Tomasino
James Tomasino

Reputation: 3581

You'd probably find it much easier to write and read if you use the Greensock TweenLite library. Tweenlite can tween frames just as easily as other numeric values.

Inside your event listener, try adding this:

TweenLite.to( targetMC, 1, { frame:1, ease:fl.transitions.easing.None.easeNone } );

It will tween your movieclip (targetMC in this example) to frame 1 linearly with a duration of 1 second. You can play around with it using any of the other Tweenlite parameters as well.

Upvotes: 2

Related Questions