Basic avatar script
From Whirled
|
Basic Script
Put this script in the first frame of your avatar's Main scene in order for it to receive action data from the server. The top script is the bare bones of standing and walking. The second script set includes states for dancing or other triggerable, looping actions.
import com.whirled.AvatarControl; import com.whirled.ControlEvent; var _ctrl :AvatarControl = new AvatarControl(this); _ctrl.addEventListener(ControlEvent.APPEARANCE_CHANGED, updateLook); updateLook(); function updateLook (o :Object = null) :void { var isMoving :Boolean = _ctrl.isMoving(); var orient :Number = _ctrl.getOrientation(); var scene :String = (isMoving ? "walk" : "face") + "_" + (orient < 180 ? "right" : "left"); gotoAndPlay(1, scene); }
Basic Script + Dancing
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); }
Basic Script + Talking
import com.whirled.AvatarControl; import com.whirled.ControlEvent; var _ctrl :AvatarControl = new AvatarControl(this); _ctrl.addEventListener(ControlEvent.APPEARANCE_CHANGED, updateLook); _ctrl.addEventListener(ControlEvent.AVATAR_SPOKE, spoke); updateLook(); function updateLook (o :Object = null) :void { var isMoving :Boolean = _ctrl.isMoving(); var orient :Number = _ctrl.getOrientation(); var scene :String = (isMoving ? "walk" : "face") + "_" + (orient < 180 ? "right" : "left"); gotoAndPlay(1, scene); } function spoke (o :Object = null) :void { try { avatar.mouth.gotoAndPlay("speak"); } catch (e :Error) { trace("Speak failed: " + e); } }
Putting It All Together
This code incorporates multiple dance and walk states, multiple walk speeds, speaking, and a custom idle scene. Note that some states can have different scenes for moving and for standing still. A switch/case structure was chosen instead of if/then, to simplify handling more than two states.
import com.whirled.AvatarControl; import com.whirled.ControlEvent; // _ctrl becomes adds control to your avatar object var _ctrl :AvatarControl = new AvatarControl(this); // This means that whenever the Avatar's appearance changes, the "updateLook" function should be called _ctrl.addEventListener(ControlEvent.APPEARANCE_CHANGED, updateLook); // Whenever the avatar is supposed to speak, call the "spoke" function _ctrl.addEventListener(ControlEvent.AVATAR_SPOKE, spoke); // When you select a new state for the avatar, the look must be updated again via the "updateLook" function _ctrl.addEventListener(ControlEvent.STATE_CHANGED, updateLook); // Set this to the spot on the floor between your avatar's legs (0,0) is the top-left corner of the scene _ctrl.setHotSpot(162, 331); // This sets the avatar's move speed to 400 pixels per second _ctrl.setMoveSpeed(400); // Register the states so that they show up as selectable avatar menu options in the Whirled _ctrl.registerStates("Default", "Sashay", "Raise the roof", "The Sprinkler", "The Wave", "The Egyptian", "Step Dance", "Travolta", "Moonwalk", "Running Man", "Die"); // Show the avatar after loading it updateLook(); // This is supposed to select the appropriate animation to play, and play it =) function updateLook (o :Object = null) :void { // This one becomes true if the avatar is going somewhere var isMoving :Boolean = _ctrl.isMoving(); // The player is idle, or has typed /away var isSleeping :Boolean = _ctrl.isSleeping(); // The angle the avatar is facing, with 0-179 being right-facing, and 180-359 left-facing // 90-269 are facing forward/down, and 270-89 are facing back/up var orient :Number = _ctrl.getOrientation(); // Player-selected "state" of the avatar, i.e., dancing, walking var avstate :String = _ctrl.getState(); // This variable will hold the scene we have selected and will be playing var scene :String = null; if (isSleeping) { // Player is idle scene = "idle_" + (orient < 180 ? "right" : "left"); } else { // Player is awake if (isMoving) { // These are the walking states: switch (avstate) { case "Sashay" : _ctrl.setMoveSpeed(240); scene = "sashay_" + (orient < 180 ? "right" : "left"); break; case "The Egyptian" : _ctrl.setMoveSpeed(170); scene = "egyptian_walk_" + (orient < 180 ? "right" : "left"); break; case "Running Man" : _ctrl.setMoveSpeed(250); scene = "run_" + (orient < 180 ? "right" : "left"); break; case "Moonwalk" : _ctrl.setMoveSpeed(160); scene = "moon_" + (orient < 180 ? "right" : "left"); break; default : _ctrl.setMoveSpeed(400); scene = "walk_" + (orient < 180 ? "right" : "left"); } } else { // These are the "standing still" / dancing states: _ctrl.setMoveSpeed(400); // Refresh to default walk speed unless a specific move has its custom speed switch (avstate) { case "Sashay" : _ctrl.setMoveSpeed(240); scene = "sashay_face_" + (orient < 180 ? "right" : "left"); break; case "Raise the roof" : scene = "roof_" + (orient < 180 ? "right" : "left"); break; case "The Sprinkler" : scene = "sprinkle_" + (orient < 180 ? "right" : "left"); break; case "The Egyptian" : _ctrl.setMoveSpeed(170); scene = "egyptian_face_" + (orient < 180 ? "right" : "left"); break; case "The Wave" : scene = "wave_" + (orient < 180 ? "right" : "left"); break; case "Step Dance" : scene = "step_" + (orient < 180 ? "right" : "left"); break; case "Travolta" : scene = "travolta_" + (orient < 180 ? "right" : "left"); break; case "Running Man" : _ctrl.setMoveSpeed(250); scene = "run_" + (orient < 180 ? "right" : "left"); break; case "Moonwalk" : _ctrl.setMoveSpeed(160); scene = "moonstand_" + (orient < 180 ? "right" : "left"); break; case "Die" : scene = "idle_" + (orient < 180 ? "right" : "left"); break; default : scene = "face_" + (orient < 180 ? "right" : "left"); } } } // Finally, go ahead and play the selected animation gotoAndPlay(1, scene); } // See http://wiki.whirled.com/Advanced_avatar_(Flash_tutorial) function spoke (o :Object = null) :void { avatar.mouth.gotoAndPlay("speak"); }

