views:

448

answers:

1

I've been trying to get this to work for a while. I have 25 frames I want to loop, but on a mouseover I want it to jump to frame 26 and continue. Any suggestions?

Actionscript 2 or 3 is fine...

+1  A: 

If you want to avoid using the timeline at all, you can either check currentFrame on each frame using an ENTER_FRAME handler, or you can use the addFrameScript() method:

var isIdle : Boolean = true;

var loopIfIdle: Function = function() : void
{
  if (isIdle)
    mc.gotoAndPlay(1);
};

mc.addFrameScript(24, loopIfIdle);
mc.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);


// The mouse handler:
function handleMouseOver(ev : MouseEvent) : void
{
  isIdle = false;
}

Essentially, what happens here is that a boolean variable is declared, which will be used to indicate whether the Flash stage has been interacted with (hovered) yet. Using a closure (a method that inherits variables in it's surrounding scope) we create the loopIfIdle function which will have access to this flag.

The addFrameScript() does more or less exactly the same thing as adding code on a frame in the Flash CS3/CS4 timeline. As such, the loopIfIdle function will be executed each time the playhead passes frame 25. But now, because we are using a closure, we can check the state of the isIdle flag from within the frame script.

The MOUSE_OVER event handler will set isIdle to false, to indicate that the stage has been hovered. This means that the next time that loopIfIdle is invoked, it will not loop (i.e. move back to frame 1) hence achieving the effect that you're after.

An even simpler solution would be to simply gotoAndPlay(26) in the MOUSE_OVER handler, and ignore the entire frame script and isIdle flag approach. This will not, however, guarantee a smooth transition from loop to frame 26 (imagine if the mouse enters the stage on frame 1, which will then jump straight to 26.) Depending on your requirements, this might still be a good alternative.

richardolsson
Hmm, if I'm wanting the second suggestions, using gotoAndPlay(26), how would I go about that. Please excuse my ignorance, this is my first time messing with actionscript.
kylex
Remove everything up to and including the line with addFrameScript() in it. Just leaving the MouseEvent handling code. Then in handleMouseOver, just do mc.gotoAndPlay(26).
richardolsson
And how does the script know to loop then?
kylex
If you're using the timeline, just put gotoAndPlay(1) on frame 25. Or you can use the addFrameScript() anyway if you want to (or do not have access to the timeline.) In that case, just skip the parts about isIdle.
richardolsson
Okay, it's looping, and I have the script set up like this:mc.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);// The mouse handler:function handleMouseOver(event : MouseEvent) : void{ MovieClip(thevideo).gotoAndPlay(107);}but the mouseover isn't working, there are no errors either.
kylex
Assuming that your instance names are right (mc and thevideo) I can only ask you to make sure that the MOUSE_OVER event is indeed firing, and getting caught. Try putting a trace() inside handleMouseOver to be sure that it does indeed get invoked. Also make sure that mc is not a Shape, Bitmap, Video or any other display object that is not interactive (doesn't inherit InteractiveObject.)
richardolsson
ah, mc is a video. So how can I work around that?
kylex
You will need to listen for the mouse event on it's container, or an object such as a MovieClip, that you draw on top of the video but which is transparent (has alpha=0.) Bottom line is that you just need to catch those mouse events. Exactly where it makes sense in your project is up to you. If the video takes up the entire stage anyway, maybe you could listen on stage: stage.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
richardolsson
Thanks! Your help brought me to the answer!
kylex