A class for actor states
ActorState is the brain of a actor. Every actor has a default ActorState, that you can use for your logic by using gamvas.Actor.getCurrentState or you could add own states for different situations, for example a soldier ai could have states like patrol, detect, follow, fight
new gamvas.ActorState(name)
name | a unique state name within the actor |
gamvas. | A class for actor states |
Variables | |
name | The name of the actor state |
actor | The actor the state is assigned to, or null if not assigned |
Functions | |
init | Gets called once on state initialization. |
enter | Gets called when the state is entered. |
leave | Gets called when the state is left. |
preDraw | Gets called after screen clear and before camera handling. |
draw | Gets called when the state should draw. |
postDraw | Gets called after camera handling. |
update | Update the ai. |
onKeyDown | A key on the keyboard was pressed |
onKeyUp | A key on the keyboard was released |
onMouseDown | A mouse button was pressed |
onMouseUp | A mouse button was released |
onMouseMove | The mouse was moved |
onCollision | The actor starts a collision with another actor |
onCollisionEnter | The actor starts a collision with another actor |
onCollisionLeave | The actor leaves a collision with another actor |
doCollide | This function is called to check of two objects should collide. |
onKeyDown: function( keyCode, character, ev )
A key on the keyboard was pressed
Requires the actor to be registered with gamvas.State.registerInputEvents
keyCode | The keycode of the key (e.g. gamvas.key.RETURN) |
character | The actual character (e.g. ‘a’) |
ev | The JavaScript event object |
gamvas.State.registerInputEvents gamvas.ActorState.onKeyUp
myActorState = gamvas.ActorState.extend({ onKeyDown: function(keyCode) { if (keyCode == gamvas.key.SPACE) { this.firePlayerGun(); } }, firePlayerGun() { console.log('BAZOING!'); } });
onCollision: function( a, ni, ti, c )
The actor starts a collision with another actor
a | the colliding actor |
ni | the normal impulse (aka how hard did we hit) |
ti | the tangent impulse (how much rotational force did we get out of the collision) |
c | a b2Contact object holding low level information about the contact |
<gamvas.actorstate.onCollisionEnter> <gamvas.actorstate.onCollisionLeave>
spaceShipFlying = gamvas.ActorState.extend({ onCollision: function(a, ni) { if ( (a.type == "asteroid") && (ni > 15) ) { console.log("here is your captain speaking, we got hit hard by a asteroid... abandon ship!"); } } });
onCollisionEnter: function( a, c )
The actor starts a collision with another actor
a | the colliding actor |
c | a b2Contact object holding low level information about the contact |
<gamvas.actorstate.onCollisionEnter>
myActorState = gamvas.ActorState.extend({ onCollisionEnter: function(a) { console.log("i got hit by "+a.name); } });
doCollide: function( a, c, om )
This function is called to check of two objects should collide. If one of the two objects returns false, the collision between the two objects will be ignored.
This is a important function for creating jump and run games. On collision with so called cloud objects - which are typical jump and run objects that you can for example jump through from below but not fall down when standing on them, you would check if you jump upwards, then disable the collision, or if you fall downwards and are positioned above the colliding object, you would enable the collision.
a | the colliding actor |
c | a b2Contact object holding low level information about the contact |
om | the old b2Manifold object holding information about the collision points |
<gamvas.actorstate.onCollisionEnter>
Do not collide with ghost objects
myActorState = gamvas.ActorState.extend({ doCollide: function(opponent) { if (opponent.type == "ghost") { return false; } return true; } });
The name of the actor state
this.name
The actor the state is assigned to, or null if not assigned
this.actor
Gets called once on state initialization.
init: function()
Gets called when the state is entered.
enter: function()
Gets called when the state is left.
leave: function()
Gets called after screen clear and before camera handling.
preDraw: function( t )
Gets called when the state should draw.
draw: function( t )
Gets called after camera handling.
postDraw: function( t )
Update the ai.
update: function( t )
A key on the keyboard was pressed
onKeyDown: function( keyCode, character, ev )
A key on the keyboard was released
onKeyUp: function( keyCode, character, ev )
A mouse button was pressed
onMouseDown: function( button, x, y, ev )
A mouse button was released
onMouseUp: function( button, x, y, ev )
The mouse was moved
onMouseMove: function( x, y, ev )
The actor starts a collision with another actor
onCollision: function( a, ni, ti, c )
The actor starts a collision with another actor
onCollisionEnter: function( a, c )
The actor leaves a collision with another actor
onCollisionLeave: function( a, c )
This function is called to check of two objects should collide.
doCollide: function( a, c, om )
Get the current gamvas.ActorState the actor is in
getCurrentState: function()
Send keyboard/mouse events to gamvas.Actor onKeyDown and similar functions
registerInputEvents: function( act )