Packagecitrus.objects.platformer.box2d
Classpublic class Hero
InheritanceHero Inheritance Box2DPhysicsObject Inheritance APhysicsObject Inheritance CitrusObject Inheritance Object

This is a common, simple, yet solid implementation of a side-scrolling Hero. The hero can run, jump, get hurt, and kill enemies. It dispatches signals when significant events happen. The game state's logic should listen for those signals to perform game state updates (such as increment coin collections). Don't store data on the hero object that you will need between two or more levels (such as current coin count). The hero should be re-created each time a state is created or reset.



Public Properties
 PropertyDefined By
  acceleration : Number = 1
This is the rate at which the hero speeds up when you move him left and right.
Hero
 Inheritedanimation : String
Animations management works the same way than label whether it uses MovieClip, SpriteSheet or whatever.
APhysicsObject
 Inheritedart : ICitrusArt
[read-only]
APhysicsObject
 InheritedbeginContactCallEnabled : Boolean
This flag determines if the handleBeginContact method is called or not.
Box2DPhysicsObject
 Inheritedbody : b2Body
[read-only] A direct reference to the Box2D body associated with this object.
Box2DPhysicsObject
  canDuck : Boolean = true
Determines whether or not the hero's ducking ability is enabled.
Hero
  controlsEnabled : Boolean
Whether or not the player can move and jump with the hero.
Hero
 Inheriteddepth : Number
[read-only] No depth in a 2D Physics world.
Box2DPhysicsObject
 InheritedendContactCallEnabled : Boolean
This flag determines if the handleEndContact method is called or not.
Box2DPhysicsObject
  enemyClass : *
[write-only] The Hero uses the enemyClass parameter to know who he can kill (and who can kill him).
Hero
  enemySpringHeight : Number = 8
The y velocity that the hero will spring when he kills an enemy.
Hero
  enemySpringJumpHeight : Number = 9
The y velocity that the hero will spring when he kills an enemy while pressing the jump button.
Hero
  friction : Number
This is the amount of friction that the hero will have.
Hero
 Inheritedgroup : uint
The group is similar to a z-index sorting.
APhysicsObject
 Inheritedheight : Number
This can only be set in the constructor parameters.
Box2DPhysicsObject
 InheritedhideParamWarnings : Boolean = false
[static]
CitrusObject
  hurtDuration : Number = 1000
How long the hero is in hurt mode for.
Hero
  hurtVelocityX : Number = 6
The amount of kick-back that the hero jumps when he gets hurt.
Hero
  hurtVelocityY : Number = 10
The amount of kick-back that the hero jumps when he gets hurt.
Hero
 InheritedID : uint
[read-only]
CitrusObject
  inputChannel : uint = 0
Defines which input Channel to listen to.
Hero
 Inheritedinverted : Boolean
[read-only] Used to invert the view on the y-axis, number of animations friendly!
APhysicsObject
  jumpAcceleration : Number = 0.3
This is the amount of "float" that the hero has when the player holds the jump button while jumping.
Hero
  jumpHeight : Number = 11
This is the initial velocity that the hero will move at when he jumps.
Hero
 Inheritedkill : Boolean = false
Set it to true if you want to remove, clean and destroy the object.
CitrusObject
  killVelocity : Number = 3
This is the y velocity that the hero must be travelling in order to kill an Enemy.
Hero
  maxVelocity : Number = 8
This is the fastest speed that the hero can move left or right.
Hero
 Inheritedname : String
A name to identify easily an objet.
CitrusObject
 InheritedoffsetX : Number
offsetX allows to move graphics on x axis compared to their initial point.
APhysicsObject
 InheritedoffsetY : Number
offsetY allows to move graphics on y axis compared to their initial point.
APhysicsObject
  onAnimationChange : Signal
Dispatched whenever the hero's animation changes.
Hero
  onGiveDamage : Signal
Dispatched whenever the hero gives damage to an enemy.
Hero
  onGround : Boolean
[read-only] Returns true if the hero is on the ground and can jump.
Hero
  onJump : Signal
Dispatched whenever the hero jumps.
Hero
  onTakeDamage : Signal
Dispatched whenever the hero takes damage from an enemy.
Hero
 InheritedparallaxX : Number
APhysicsObject
 InheritedparallaxY : Number
APhysicsObject
 Inheritedpoints : Array
Used to define vertices' x and y points.
Box2DPhysicsObject
 InheritedpostContactCallEnabled : Boolean
This flag determines if the handlePostSolve method is called or not.
Box2DPhysicsObject
 InheritedpreContactCallEnabled : Boolean
This flag determines if the handlePreSolve method is called or not.
Box2DPhysicsObject
 Inheritedradius : Number
This can only be set in the constructor parameters.
Box2DPhysicsObject
 Inheritedregistration : String
Flash registration point is topLeft, whereas physics engine use mostly center.
APhysicsObject
 Inheritedrotation : Number
Box2DPhysicsObject
 Inheritedtouchable : Boolean
APhysicsObject
 Inheritedtype : String = classicObject
Added to the CE's render list via the State and the add method.
CitrusObject
 InheritedupdateCallEnabled : Boolean = false
This property prevent the update method to be called by the enter frame, it will save performances.
CitrusObject
 Inheritedvelocity : Array
Box2DPhysicsObject
 Inheritedview : *
The view can be a class, a string to a file, or a display object.
APhysicsObject
 Inheritedvisible : Boolean
You can easily change if an object is visible or not.
APhysicsObject
 Inheritedwidth : Number
This can only be set in the constructor parameters.
Box2DPhysicsObject
 Inheritedx : Number
Box2DPhysicsObject
 Inheritedy : Number
Box2DPhysicsObject
 Inheritedz : Number
[read-only]
Box2DPhysicsObject
Protected Properties
 PropertyDefined By
 Inherited_animation : String
APhysicsObject
 Inherited_art : ICitrusArt
APhysicsObject
 Inherited_beginContactCallEnabled : Boolean = false
Box2DPhysicsObject
 Inherited_body : b2Body
Box2DPhysicsObject
 Inherited_bodyDef : b2BodyDef
Box2DPhysicsObject
 Inherited_box2D : Box2D
Box2DPhysicsObject
 Inherited_ce : CitrusEngine
CitrusObject
  _combinedGroundAngle : Number = 0
Hero
  _controlsEnabled : Boolean = true
Hero
  _ducking : Boolean = false
Hero
 Inherited_endContactCallEnabled : Boolean = false
Box2DPhysicsObject
  _enemyClass : Class
Hero
 Inherited_fixture : b2Fixture
Box2DPhysicsObject
 Inherited_fixtureDef : b2FixtureDef
Box2DPhysicsObject
  _friction : Number = 0.75
Hero
  _groundContacts : Array
Hero
 Inherited_height : Number = 1
Box2DPhysicsObject
  _hurt : Boolean = false
Hero
  _hurtTimeoutID : uint
Hero
 Inherited_initialized : Boolean = false
CitrusObject
 Inherited_inverted : Boolean = false
APhysicsObject
  _onGround : Boolean = false
Hero
 Inherited_parallaxX : Number = 1
APhysicsObject
 Inherited_parallaxY : Number = 1
APhysicsObject
 Inherited_params : Object
CitrusObject
  _playerMovingHero : Boolean = false
Hero
 Inherited_postContactCallEnabled : Boolean = false
Box2DPhysicsObject
 Inherited_preContactCallEnabled : Boolean = false
Box2DPhysicsObject
 Inherited_radius : Number = 0
APhysicsObject
 Inherited_rotation : Number = 0
APhysicsObject
 Inherited_shape : b2Shape
Box2DPhysicsObject
  _springOffEnemy : Number = -1
Hero
 Inherited_timeDelta : Number
The time elasped between two update call.
CitrusObject
 Inherited_touchable : Boolean = false
APhysicsObject
 Inherited_vertices : Array
Box2DPhysicsObject
 Inherited_view : *
APhysicsObject
 Inherited_visible : Boolean = true
APhysicsObject
 Inherited_width : Number = 1
Box2DPhysicsObject
 Inherited_x : Number = 0
APhysicsObject
 Inherited_y : Number = 0
APhysicsObject
 Inherited_z : Number = 0
APhysicsObject
Public Methods
 MethodDefined By
  
Hero(name:String, params:Object = null)
Creates a new hero object.
Hero
 Inherited
addPhysics():void
[override] All your init physics code must be added in this method, no physics code into the constructor.
Box2DPhysicsObject
  
destroy():void
[override]
Hero
 Inherited
This method doesn't depend of your application enter frame.
APhysicsObject
 Inherited
[override]
Box2DPhysicsObject
  
Returns the absolute walking speed, taking moving platforms into account.
Hero
 Inherited
called when the art changes.
APhysicsObject
 Inherited
handleArtReady(citrusArt:ICitrusArt):void
called when the art is created (and loaded if loading is required)
APhysicsObject
  
handleBeginContact(contact:b2Contact):void
[override]
Hero
  
handleEndContact(contact:b2Contact):void
[override]
Hero
 Inherited
handlePostSolve(contact:b2Contact, impulse:b2ContactImpulse):void
Override this method if you want to perform some actions after the collision.
Box2DPhysicsObject
  
handlePreSolve(contact:b2Contact, oldManifold:b2Manifold):void
[override]
Hero
  
hurt():void
Hurts the hero, disables his controls for a little bit, and dispatches the onTakeDamage signal.
Hero
 Inherited
initialize(poolObjectParams:Object = null):void
Call in the constructor if the Object is added via the State and the add method.
CitrusObject
 Inherited
setParams(object:Object, params:Object):void
The initialize method usually calls this.
CitrusObject
 Inherited
toString():String
CitrusObject
  
update(timeDelta:Number):void
[override]
Hero
Protected Methods
 MethodDefined By
 Inherited
Box2DPhysicsObject
 Inherited
createBody():void
This method will often need to be overridden to customize the Box2D body object.
Box2DPhysicsObject
 Inherited
This method will often need to be overridden to customize the Box2D fixture object.
Box2DPhysicsObject
 Inherited
This method will often need to be overridden to customize the Box2D joint object.
Box2DPhysicsObject
  
[override]
Hero
  
defineBody():void
[override]
Hero
  
[override]
Hero
 Inherited
This method will often need to be overridden to provide additional definition to the Box2D joint object.
Box2DPhysicsObject
  
Hero
  
Hero
  
Hero
  
Hero
Property Detail
_combinedGroundAngleproperty
protected var _combinedGroundAngle:Number = 0

_controlsEnabledproperty 
protected var _controlsEnabled:Boolean = true

_duckingproperty 
protected var _ducking:Boolean = false

_enemyClassproperty 
protected var _enemyClass:Class

_frictionproperty 
protected var _friction:Number = 0.75

_groundContactsproperty 
protected var _groundContacts:Array

_hurtproperty 
protected var _hurt:Boolean = false

_hurtTimeoutIDproperty 
protected var _hurtTimeoutID:uint

_onGroundproperty 
protected var _onGround:Boolean = false

_playerMovingHeroproperty 
protected var _playerMovingHero:Boolean = false

_springOffEnemyproperty 
protected var _springOffEnemy:Number = -1

accelerationproperty 
public var acceleration:Number = 1

This is the rate at which the hero speeds up when you move him left and right.

canDuckproperty 
public var canDuck:Boolean = true

Determines whether or not the hero's ducking ability is enabled.

controlsEnabledproperty 
controlsEnabled:Boolean

Whether or not the player can move and jump with the hero.


Implementation
    public function get controlsEnabled():Boolean
    public function set controlsEnabled(value:Boolean):void
enemyClassproperty 
enemyClass:*  [write-only]

The Hero uses the enemyClass parameter to know who he can kill (and who can kill him). Use this setter to to pass in which base class the hero's enemy should be, in String form or Object notation. For example, if you want to set the "Enemy" class as your hero's enemy, pass "citrus.objects.platformer.Enemy", or Enemy (with no quotes). Only String form will work when creating objects via a level editor.


Implementation
    public function set enemyClass(value:any):void
enemySpringHeightproperty 
public var enemySpringHeight:Number = 8

The y velocity that the hero will spring when he kills an enemy.

enemySpringJumpHeightproperty 
public var enemySpringJumpHeight:Number = 9

The y velocity that the hero will spring when he kills an enemy while pressing the jump button.

frictionproperty 
friction:Number

This is the amount of friction that the hero will have. Its value is multiplied against the friction value of other physics objects.


Implementation
    public function get friction():Number
    public function set friction(value:Number):void
hurtDurationproperty 
public var hurtDuration:Number = 1000

How long the hero is in hurt mode for.

hurtVelocityXproperty 
public var hurtVelocityX:Number = 6

The amount of kick-back that the hero jumps when he gets hurt.

hurtVelocityYproperty 
public var hurtVelocityY:Number = 10

The amount of kick-back that the hero jumps when he gets hurt.

inputChannelproperty 
public var inputChannel:uint = 0

Defines which input Channel to listen to.

jumpAccelerationproperty 
public var jumpAcceleration:Number = 0.3

This is the amount of "float" that the hero has when the player holds the jump button while jumping.

jumpHeightproperty 
public var jumpHeight:Number = 11

This is the initial velocity that the hero will move at when he jumps.

killVelocityproperty 
public var killVelocity:Number = 3

This is the y velocity that the hero must be travelling in order to kill an Enemy.

maxVelocityproperty 
public var maxVelocity:Number = 8

This is the fastest speed that the hero can move left or right.

onAnimationChangeproperty 
public var onAnimationChange:Signal

Dispatched whenever the hero's animation changes.

onGiveDamageproperty 
public var onGiveDamage:Signal

Dispatched whenever the hero gives damage to an enemy.

onGroundproperty 
onGround:Boolean  [read-only]

Returns true if the hero is on the ground and can jump.


Implementation
    public function get onGround():Boolean
onJumpproperty 
public var onJump:Signal

Dispatched whenever the hero jumps.

onTakeDamageproperty 
public var onTakeDamage:Signal

Dispatched whenever the hero takes damage from an enemy.

Constructor Detail
Hero()Constructor
public function Hero(name:String, params:Object = null)

Creates a new hero object.

Parameters
name:String
 
params:Object (default = null)
Method Detail
createShape()method
override protected function createShape():void

defineBody()method 
override protected function defineBody():void

defineFixture()method 
override protected function defineFixture():void

destroy()method 
override public function destroy():void

endHurtState()method 
protected function endHurtState():void

getSlopeBasedMoveAngle()method 
protected function getSlopeBasedMoveAngle():b2Vec2

Returns
b2Vec2
getWalkingSpeed()method 
public function getWalkingSpeed():Number

Returns the absolute walking speed, taking moving platforms into account. Isn't super performance-light, so use sparingly.

Returns
Number
handleBeginContact()method 
override public function handleBeginContact(contact:b2Contact):void

Parameters

contact:b2Contact

handleEndContact()method 
override public function handleEndContact(contact:b2Contact):void

Parameters

contact:b2Contact

handlePreSolve()method 
override public function handlePreSolve(contact:b2Contact, oldManifold:b2Manifold):void

Parameters

contact:b2Contact
 
oldManifold:b2Manifold

hurt()method 
public function hurt():void

Hurts the hero, disables his controls for a little bit, and dispatches the onTakeDamage signal.

update()method 
override public function update(timeDelta:Number):void

Parameters

timeDelta:Number

updateAnimation()method 
protected function updateAnimation():void

updateCombinedGroundAngle()method 
protected function updateCombinedGroundAngle():void