Packagecom.gskinner.motion
Classpublic class Wander

Wander ©2009 Grant Skinner, gskinner.com. Visit www.gskinner.com/blog/ for documentation, updates and more free code. Licensed under the MIT license - see the source file header for more information.
Applies wander motion to a target display object, rotating and moving it according to a wide set of properties.



Public Properties
 PropertyDefined by
  autoUpdate : Boolean
Specifies whether the wander updates each frame automatically.
Wander
  count : uint
As the internal counter approaches count, it adds a exponentially derived value to strength, such that when the internal counter equals count, the trend strength will be 1.
Wander
  data : Object
Allows you to associate arbitrary data with this wander instance.
Wander
  delayCount : uint
Allows you to delay the effect of the count property by the specified number of frames.
Wander
  innerRadius : Number
The radius (from targetX and/or targetY) within which the target does not trend towards the target position.
Wander
  oldRotation : Number
Returns the previous rotation of the target.
Wander
  oldX : Number
Returns the previous x position of the target.
Wander
  oldY : Number
Returns the previous y position of the target.
Wander
  onChange : Function
This callback is called when count .
Wander
  onComplete : Function
This callback is called each frame while the wander is active.
Wander
  outerRadius : Number
The maximum distance (from targetX and/or targetY) the target can wander from the target position.
Wander
  rotationLimit : Number
Limits the maximum rotation change that the wander can make per update.
Wander
  speed : Number
The distance the target will travel each frame.
Wander
  strength : Number
The constant 0-1 strength applied to all active trends (based on targetX, targetY, or targetRotation).
Wander
  target : DisplayObject
The display object to move.
Wander
  targetObject : DisplayObject
A display object whose x/y will be used as targetX / targetY (ie.
Wander
  targetRotation : Number
The rotation in degrees the target will trend towards or NaN to disable.
Wander
  targetX : Number
The x position the target will trend towards or NaN to disable.
Wander
  targetY : Number
The y position the target will trend towards or NaN to disable.
Wander
  varyRotation : Number
The amount that the target will vary rotation randomly (ie.
Wander
  varySpeed : Number
The amount that the speed will vary randomly each frame.
Wander
Public Methods
 MethodDefined by
  
Wander(target:DisplayObject, props:Object = null)
Creates a new wander instance.
Wander
  
getShortRotation(rot:Number):Number
[static] Helper function that returns the shortest equivalent rotation value.
Wander
  
reset(props:Object = null):void
Resets all of the instance's default properties except target, and applies the specified props.
Wander
  
update():void
Runs the next "frame" of the wander - varying the rotation according to the instance properties and moving forwards.
Wander
Protected Methods
 MethodDefined by
  
handleTick(evt:Event):void
Wander
Property detail
autoUpdateproperty
autoUpdate:Boolean  [read-write]

Specifies whether the wander updates each frame automatically. True by default. This can be used to pause / resume the motion. It can also be set to false and you can call .update() directly - for example, in a game with a central "tick" dispatcher.

Implementation
    public function get autoUpdate():Boolean
    public function set autoUpdate(value:Boolean):void
countproperty 
public var count:uint

As the internal counter approaches count, it adds a exponentially derived value to strength, such that when the internal counter equals count, the trend strength will be 1.

dataproperty 
public var data:Object

Allows you to associate arbitrary data with this wander instance.

delayCountproperty 
public var delayCount:uint

Allows you to delay the effect of the count property by the specified number of frames.

innerRadiusproperty 
public var innerRadius:Number

The radius (from targetX and/or targetY) within which the target does not trend towards the target position.

oldRotationproperty 
public var oldRotation:Number

Returns the previous rotation of the target.

oldXproperty 
public var oldX:Number

Returns the previous x position of the target.

oldYproperty 
public var oldY:Number

Returns the previous y position of the target.

onChangeproperty 
public var onChange:Function

This callback is called when count .

onCompleteproperty 
public var onComplete:Function

This callback is called each frame while the wander is active.

outerRadiusproperty 
public var outerRadius:Number

The maximum distance (from targetX and/or targetY) the target can wander from the target position. The strength of this trend increases exponentially as the target wanders away from the innerRadius and approaches the outerRadius.

rotationLimitproperty 
public var rotationLimit:Number

Limits the maximum rotation change that the wander can make per update. Set to NaN to disable.

speedproperty 
public var speed:Number

The distance the target will travel each frame.

strengthproperty 
public var strength:Number

The constant 0-1 strength applied to all active trends (based on targetX, targetY, or targetRotation). This is additive with strengths calculated from outerRadius and count.

targetproperty 
public var target:DisplayObject

The display object to move.

targetObjectproperty 
public var targetObject:DisplayObject

A display object whose x/y will be used as targetX / targetY (ie. the wander will follow the targetObject). Set to null to disable.

targetRotationproperty 
public var targetRotation:Number

The rotation in degrees the target will trend towards or NaN to disable.

targetXproperty 
public var targetX:Number

The x position the target will trend towards or NaN to disable.

targetYproperty 
public var targetY:Number

The y position the target will trend towards or NaN to disable.

varyRotationproperty 
public var varyRotation:Number

The amount that the target will vary rotation randomly (ie. wander). For example, a value of 0.5 would let the rotation vary by +/- 45 degrees each frame.

varySpeedproperty 
public var varySpeed:Number

The amount that the speed will vary randomly each frame. For example, with a speed of 4 and varySpeed of 0.5, the speed will vary from 2 to 4.

Constructor detail
Wander()constructor
public function Wander(target:DisplayObject, props:Object = null)

Creates a new wander instance.

Parameters
target:DisplayObject — Specifies the DisplayObject to move.
 
props:Object (default = null) — Shortcut for setting properties on the new instance. All properties on this object are copied to the new instance. ex. {targetRotation:0, strength:0.1}
Method detail
getShortRotation()method
public static function getShortRotation(rot:Number):Number

Helper function that returns the shortest equivalent rotation value. For example, -90deg is a shorter equivalent of 270deg, and 15deg is equivalent to 375deg.

Parameters
rot:Number

Returns
Number
handleTick()method 
protected function handleTick(evt:Event):voidParameters
evt:Event
reset()method 
public function reset(props:Object = null):void

Resets all of the instance's default properties except target, and applies the specified props.

Parameters
props:Object (default = null)
update()method 
public function update():void

Runs the next "frame" of the wander - varying the rotation according to the instance properties and moving forwards. This is called every frame when autoUpdate is true, or can be called directly.