Thatisdifficult Thatisdifficult - 23 days ago 13
ActionScript Question

Making a button that goes to a random frame (from an array) on the main timeline. How to do from a movie clip?

I saw plenty of questions that asked how to make a button in a movie clip that leads to a frame in the main timeline, and took a look at them. But is it different if you want to go to a random frame in a specific set of frames? I've never really used AS3 aside from simple things like stop(); or gotoAndPlay.

Here's what my main timeline looks like at the moment:
Figure 1

Here's the code I've got so far by using Google:

var frameB:Array=[1,28,45,56,71,91,106,126];
blue_circle1.addEventListener(MouseEvent.CLICK, choose);

function choose1(event:MouseEvent):void {
var randomFrame:Number = frameB[Math.floor(Math.random() * frameB.length)];
trace(randomFrame);
gotoAndPlay(randomFrame);
}


The code works fine when I use it on the rotation buttons in the main timeline. But it doesn't work when I put it in a button in a movie clip. I need to change it so it works. If there is a better way of doing this, I'm willing to try it.

Edit: I should clarify things more. I got rectangle on wheels that moves left to right. It does this in a movie clip. I want the button to move with it. But when I place the button into said movie clip, the code on the button stops working. I hope I didn't end up making things more confusing.

Answer Source

When you place the button and/or code inside the movieClip it changes the movieClip that gotoAndPlay() is referring to. You need to specify which movieClip you are calling gotoAndPlay() on. For the main timeline gotoAndPlay() works, but inside a movieClip you must use this:

parent.gotoAndPlay(randomFrame);

Or you may need to set parent's type to a MovieClip like this:

MovieClip(parent).gotoAndPlay(randomFrame);

However, it's best to use external .as files as it gives you the most control over your code.

  • Save the code below in a file called 'MyFlashAnimation.as'
  • Create a folder called 'mycodefolder' and put it in the same directory/folder as your .fla.
  • Use the 'MyFlashAnimation.as' as your document class. In the Flash/Animate IDE find the Properties panel, then the Publish section
  • In the Publish section where it says 'Class' enter: mycodefolder.MyFlashAnimation (do not add the .as)

CODE:

package mycodefolder {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class MyFlashAnimation {

        private var animationClip:MovieClip;
        private var blueCircle1:Button;

        private var frameB:Array = [1,28,45,56,71,91,106,126];

        // constructor
        public function MyFlashAnimation() {

            // this your main robot/car animation
            // this assumes animation_clip is on the main stage
            animationClip = this.animation_clip;

            // this is your button. this assumes blue_circle1 is a child of
            // your animation_clip. update the path if necessary.
            // for example, it might be: animationClip.robot_body.blue_circle1
            blueCircle1 = animationClip.blue_circle1;

            // add listener
            blue_circle1.addEventListener(MouseEvent.CLICK, choose);
        }

        function choose1(event:MouseEvent):void {
            var randomFrame:Number = frameB[Math.floor(Math.random() * frameB.length)];
            trace(randomFrame);

            // tell animation clip to gotoAndPlay
            animationClip.gotoAndPlay(randomFrame);
        }

    }
}

That should work. If you have all your movieClip paths correct.