views:

639

answers:

4

I'm trying to get a movie clip to play in reverse when I mouse_out from it (it plays on mouse_over).

My actionscript is as follows:

mc.stop();
mc.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc.addEventListener(MouseEvent.MOUSE_OUT,mout);

function mover(e:MouseEvent):void
{
    mc.play();
}

function mout(e:MouseEvent):void
{
   //Play in reverse
}

How would I achieve this?

Thanks

+2  A: 

The best way would be to use an ENTER_FRAME event listener. Basically this is what you want to do

function mover(e:MouseEvent):void {
    stopPlayReverse();
    mc.play();
}

function mout(e:MouseEvent):void {
    this.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}

function playReverse(e:Event):void {
    if (mc.currentFrame == 1) {
        stopPlayReverse();
    } else {
        mc.prevFrame();
    }
}

function stopPlayReverse():void {
    if this.hasEventListener(Event.ENTER_FRAME) {
        this.removeEventListener(Event.ENTER_FRAME, playReverse);
    }
}

This would play your MovieClip in reverse until it hits frame 1, then it will stop.

sberry2A
Thanks a lot that worked perfectly!
Probocop
No problem. As suggested, you could try using the built-in Tween class or another tweening library. The best thing about the ENTER_FRAME approach is that your animation takes the same amount of time backward or forward regardless of your frame rate.
sberry2A
+3  A: 

If the motion allows, you can use a Tween (for example if you want to change the alpha, location or scale). On the MouseOut you can call .yoyo() for the Tween, which will play it in reverse.

Something like this:

var tween:Tween;

mc.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc.addEventListener(MouseEvent.MOUSE_OUT,mout);

function mover(e:MouseEvent):void
{
    tween = new Tween(obj, "alpha", None.easeNone, 1, 0, 1, true);
}

function mout(e:MouseEvent):void
{
   tween.yoyo();
}
Pbirkoff
A: 

If your tween is not all that serious you can literally make a whole new set of frames in reverse and just play it from that keyframe.

That's the poor man's reverse tween.

Jasconius
Very true, and how it has been done for ages.
sberry2A
+1  A: 

TweenLite.to(mc, 2, {frame:1});

:)

dome