Handy Actionscripts for Animators

From Whirled

Jump to: navigation, search

Contents

Stop an animation from looping:

   stop();


Randomly trigger an effect:

For this example we'll make randomly blinking eyes:

1. Make the eyes a movieclip symbol, and animate a blink (which may well be one frame).

2. Add about five frames between the first frame and the blink animation.

image:blink_frames.png

3. Put this script in a keyframe at the last frame before the blink:

       var blink:Number= Math.random()*100; // creates a random number, 'blink', from 1 to 100
       if(blink > 5){ // if 'blink' is greater than 5, or, with the above line, 'there is a 95% chance to...'
       gotoAndPlay(1); // ...loop back to the first frame
       }

This creates a cycle over the first five frames (where the eyes stay open). At the end of each cycle, it has a 95% chance of continuing the cycle (keeping the eyes open). We chose 'blink > 5' rather than 'blink < 95' because what you really want to say is that it has a 5% chance of blinking (not a 95% chance of not blinking). So, in the 5% chance the 'gotoAndPlay' event doesn't run, the playhead moves along the rest of the movieclip, where we have the blinking animation. At the end of the movieclip (as with any movieclip) it automatically loops back to the first frame, which conveniently drops it into the cycle again.

Notes:

  • You can adjust the probability of your random event three ways:
    • Add or remove frames before the code. The event can only be triggered on the code frame, so adding frames before it makes it test for the event less frequently.
    • Change the 'if' statement's number. Assigning 'blink' from 1 to 100 means the 'if (blink >' number is a handy percentage. Change it to 'if (blink > 20)' for a 20% chance of blinking, or any number you like.
    • Change your variable's upper limit. 'Blink' could be a random number from 1 to 5, or 1 to 10,000. More often than not, however, you're better off using the previous method.
  • You can name your variable any words that Flash isn't already using for code. Flash's scripting panel will highlight words you type if they have a code meaning already.
  • If you want multiple random events in the same scene, you have to use different names for each random variable.



To create a cycle:

1. Locate the first frame of your cycle and put a keyframe on the actions layer.

2. Label it something like "dance01cycle". Now locate the last frame of the cycle and put a keyframe there. In the actions palette, type:

   gotoAndPlay("dance01cycle");

Script for Publishing Your Avatar

When your avatar is ready to be published, create a single keyframe in your "main" scene. Add this ActionScript:

    import com.whirled.AvatarControl;
    import com.whirled.ControlEvent;
    var _ctrl :AvatarControl = new AvatarControl(this);
    _ctrl.addEventListener(ControlEvent.APPEARANCE_CHANGED, updateLook);
    _ctrl.addEventListener(ControlEvent.STATE_CHANGED, updateLook);
    _ctrl.setHotSpot(150, 320); // Set these as the X, Y coordinates of the centerpoint of the "floor" between your avatar's feet
    _ctrl.registerStates("Default", "Dance 1", "Dance 2"); // Assuming you have dances, or other states to swap in
    updateLook();
    function updateLook (o :Object = null) :void
    {
        var isMoving :Boolean = _ctrl.isMoving();
        var orient :Number = _ctrl.getOrientation();
        var scene :String = null;
        if (!isMoving) {
            var state :String = _ctrl.getState();
            if (state == "Dance 1") {
                 scene = "dance_01" + "_" + (orient < 180 ? "right" : "left");
            } else if (state == "Dance 2") {
                scene = "dance_02" + "_" + (orient < 180 ? "right" : "left");
            }
         }
        if (scene == null) {  // if none of the above (not dancing, or is walking)
               scene = (isMoving ? "walk" : "face") + "_" + (orient < 180 ? "right" : "left");
        }
        gotoAndPlay(1, scene);
    }


This is a very important script. It lets Whirled know exactly what your character is capable of doing. If you have any actions other than "default," "walk" or "Dance" you will want to add them here in the "_ctrl.registerStates" line, and then add another "else if" statement further down for that specific action.

Set Your Hotspot

Don't forget to change your hotspot coordinates. Do this by placing your cursor at the bottom of the screen, at your character's center of gravity. (Somewhere between its legs...if it has legs.) Now look at the "Info" tab and write down the X and Y coordinates listed. Pop these into the right place in the script above and you are good to go. (Remember that avatars max out at 450 px high, so neither value should be above that.)

See Also

If you don't have a .swf file of you avatar, just open the .fla file and press Ctrl-Enter to publish a .swf file.

Personal tools