code on 2008-12-18
...
@author naoto koshikawa
package
{
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLRequest;
/**
* ...
* @author naoto koshikawa
*/
public class PlaneMotion1 extends MovieClip
{
/** friction target to speed */
public static const FRICTION_TARGET_SPEED:String = "frictionTargetSpeed";
/** friction target to distance */
public static const FRICTION_TARGET_DISTANCE:String = "frictionTargetDistance";
/** minimum speed */
private static const MINIMUM_SPEED:Number = 0.1;
/** character uri */
private static const CHARACTER_IMG_URI:String = "http://koshikawa.net/swf/character001.swf";
/** target object */
private var _target:*;
/** motion speed */
private var _speed:Number;
/** motion speed acceleration */
private var _acceleration:Number;
/** motion friction */
private var _friction:Number;
/** motion friction target */
private var _frictionTarget:String;
/** motion destination point */
private var _destination:Point;
/** motion point destination subtract current */
private var _subPoint:Point;
/** motion apply the law of inertia */
private var _inertia:Boolean;
/** permissible destination range */
private var _permissibleRange:Number;
/** motion current point */
private var _current:Point;
/** distance */
private var _distance:Number;
/** motion angle in degree */
private var _degree:Number;
/** motion angle in radian */
private var _radian:Number;
/** motion boundary */
private var _boundary:Rectangle;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="24")]
/**
* constructor
*/
public function PlaneMotion1()
{
loadCharacter();
}
/**
* load character image
*/
private function loadCharacter():void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeLoadListener);
loader.load(new URLRequest(CHARACTER_IMG_URI));
}
/**
*
* @param event
*/
private function configureCharacter():void
{
addChild(_target);
_boundary = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
_current = new Point(_target.x, _target.y);
_destination = new Point(100, 100);
initCharacter();
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
}
/**
*
*/
private function initCharacter():void
{
_speed = 1;
_acceleration = 0.01;
_friction = 0.05;
_frictionTarget = PlaneMotion1.FRICTION_TARGET_DISTANCE;
_inertia = false;
_permissibleRange = 5;
_subPoint = _destination.subtract(_current);
_target.addEventListener(Event.ENTER_FRAME, enterFrameListener);
}
/**
* stop motion
*/
public function stopMotion():void
{
_target.removeEventListener(Event.ENTER_FRAME, enterFrameListener);
}
// _____________________________________________________ Listener
/**
* load character complete event listener
* @param event
*/
private function completeLoadListener(event:Event):void
{
var loaderInfo:LoaderInfo = event.target as LoaderInfo;
loaderInfo.removeEventListener(Event.COMPLETE, completeLoadListener);
_target = loaderInfo.loader;
configureCharacter();
}
/**
*
*/
private function enterFrameListener(event:Event):void
{
// update distinaction substract current
if (!_inertia) _subPoint = _destination.subtract(_current);
_distance = _subPoint.length;
_radian = Math.atan2(_subPoint.y, _subPoint.x);
if (_distance != 0) _degree = _radian * 180 / Math.PI;
// update speed
switch (_frictionTarget)
{
case PlaneMotion1.FRICTION_TARGET_SPEED:
_speed *= _friction;
_speed += _acceleration;
break;
case PlaneMotion1.FRICTION_TARGET_DISTANCE:
_speed = _distance * _friction;
_friction += _acceleration;
break;
}
_speed += _acceleration;
if (_speed < 0) _speed = MINIMUM_SPEED;
if (_speed > _distance) _speed = _distance;
// update current position
_current.x += Math.cos(_radian) * _speed;
_current.y += Math.sin(_radian) * _speed;
if (_boundary)
{
if (!_boundary.containsRect(_target.getRect(_target.parent)))
{
//trace("out of boundary");
}
}
if (!_inertia && _distance < _permissibleRange) stopMotion();
_target.x = _current.x;
_target.y = _current.y;
}
/**
* mouse down listener
* @param event
*/
private function mouseDownListener(event:MouseEvent):void
{
stopMotion();
_destination = new Point(event.stageX, event.stageY);
initCharacter();
}
}
}