Simple avatar full ActionScript listing
From Whirled
For use with simple avatar (ActionScript tutorial):
//
// $Id$
//
// chicken - an avatar for Whirled
package {
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.getTimer; // function import
import com.whirled.AvatarControl;
import com.whirled.ControlEvent;
[SWF(width="161", height="170")] // the size of our image + BOUNCE pixels in the y direction.
public class chicken extends Sprite
{
public function chicken ()
{
_control = new AvatarControl(this);
// create and add the image that represents us
_image = (new IMAGE() as DisplayObject);
_image.y = BOUNCE;
addChild(_image);
// Uncomment this to be notified when your avatar changes orientation
_control.addEventListener(ControlEvent.APPEARANCE_CHANGED, appearanceChanged);
// listen for an unload event
_control.addEventListener(Event.UNLOAD, handleUnload);
// Uncomment this to be notified when the player speaks
// _control.addEventListener(ControlEvent.AVATAR_SPOKE, avatarSpoke);
// Uncomment this to export custom avatar actions
// _control.addEventListener(ControlEvent.ACTION_TRIGGERED, handleAction);
// _control.registerActions("Test action");
appearanceChanged();
}
/**
* This is called when your avatar's orientation changes or when it transitions from not
* walking to walking and vice versa.
*/
protected function appearanceChanged (... ignored) :void
{
var orient :Number = _control.getOrientation();
var isMoving :Boolean = _control.isMoving();
// make sure we're oriented correctly
// (We discard nearly all the orientation information and only care if we're
// facing left or right.)
if (orient < 180) {
_image.x = _image.width;
_image.scaleX = -1;
} else {
_image.x = 0;
_image.scaleX = 1;
}
// if we're moving, make us bounce.
if (_bouncing != isMoving) {
_bouncing = isMoving;
if (_bouncing) {
_bounceBase = getTimer(); // note that time at which we start bouncing
addEventListener(Event.ENTER_FRAME, handleEnterFrame);
} else {
removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
// stop bouncing: put us back on the ground
_image.y = BOUNCE;
}
}
}
protected function handleEnterFrame (... ignored) :void
{
trace ("Entered frame ");
var now :Number = getTimer();
var elapsed :Number = now - _bounceBase;
while (elapsed > BOUNCE_FREQUENCY) {
elapsed -= BOUNCE_FREQUENCY;
_bounceBase += BOUNCE_FREQUENCY; // give us less math to do next time..
}
var val :Number = elapsed * Math.PI / BOUNCE_FREQUENCY;
_image.y = BOUNCE - (Math.sin(val) * BOUNCE);
}
/**
* This is called when your avatar speaks.
*/
protected function avatarSpoke (event :Object = null) :void
{
}
/**
* This is called when the user selects a custom action exported on your avatar or when any
* other trigger event is received.
*/
protected function handleAction (event :ControlEvent) :void
{
}
/**
* This is called when your avatar is unloaded.
*/
protected function handleUnload (event :Event) :void
{
// stop any sounds, clean up any resources that need it. This specifically includes
// unregistering listeners to any events - especially Event.ENTER_FRAME
removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
}
/** How we communicate with whirled. */
protected var _control :AvatarControl;
/** The image we're flippin' and bouncin'. */
protected var _image :DisplayObject;
/** Are we currently bouncing? */
protected var _bouncing :Boolean = false;
/** The time at which the current bounce started. */
protected var _bounceBase :Number;
//-------------------------------------------------------
/** The height of our bounces. */
protected static const BOUNCE :int = 20;
/** The time to complete one bounce. */
protected static const BOUNCE_FREQUENCY :int = 400;
/** The image resource. */
//[Embed(source="rooster.jpg")]
[Embed(source="rooster.png")]
protected static const IMAGE :Class;
}
}

