gamvas.ActorState

Description

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

Constructor

new gamvas.ActorState(name)

Parameters

namea unique state name within the actor
Summary
gamvas.ActorStateA class for actor states
Variables
nameThe name of the actor state
actorThe actor the state is assigned to, or null if not assigned
Functions
initGets called once on state initialization.
enterGets called when the state is entered.
leaveGets called when the state is left.
preDrawGets called after screen clear and before camera handling.
drawGets called when the state should draw.
postDrawGets called after camera handling.
updateUpdate the ai.
onKeyDownA key on the keyboard was pressed
onKeyUpA key on the keyboard was released
onMouseDownA mouse button was pressed
onMouseUpA mouse button was released
onMouseMoveThe mouse was moved
onCollisionThe actor starts a collision with another actor
onCollisionEnterThe actor starts a collision with another actor
onCollisionLeaveThe actor leaves a collision with another actor
doCollideThis function is called to check of two objects should collide.

Variables

name

this.name

The name of the actor state

actor

this.actor

The actor the state is assigned to, or null if not assigned

Functions

init

init: function()

Description

Gets called once on state initialization.  Overwrite with code that needs to be done one, like resource loading.

enter

enter: function()

Description

Gets called when the state is entered.

leave

leave: function()

Description

Gets called when the state is left.

preDraw

preDraw: function(t)

Description

Gets called after screen clear and before camera handling.  Use this for example for static background elements that do not move with the camera.

Parameters

tthe time since last redraw

draw

draw: function(t)

Description

Gets called when the state should draw.  In most cases you would not overwrite this function.  If you put logic like ai in a actor, overwrite the update function.

Parameters

tthe time since last redraw

See

gamvas.ActorState.update

postDraw

postDraw: function(t)

Description

Gets called after camera handling.  Use this to render static things in the screen forground, like score or health.

Parameters

tthe time since last redraw

update

update: function(t)

Description

Update the ai.  Overwrite with things your actor should do on a per frame basis.

Parameters

tthe time since last redraw

onKeyDown

onKeyDown: function(keyCode,
character,
ev)

Description

A key on the keyboard was pressed

Note

Requires the actor to be registered with gamvas.State.registerInputEvents

Parameters

keyCodeThe keycode of the key (e.g. gamvas.key.RETURN)
characterThe actual character (e.g.  ‘a’)
evThe JavaScript event object

See

gamvas.State.registerInputEvents gamvas.ActorState.onKeyUp

Example

myActorState = gamvas.ActorState.extend({
    onKeyDown: function(keyCode) {
        if (keyCode == gamvas.key.SPACE) {
            this.firePlayerGun();
        }
    },
    firePlayerGun() {
        console.log('BAZOING!');
    }
});

onKeyUp

onKeyUp: function(keyCode,
character,
ev)

Description

A key on the keyboard was released

Parameters

keyCodeThe keycode of the key (e.g. gamvas.key.RETURN)
characterThe actual character (e.g.  ‘a’)
evThe JavaScript event object

See

gamvas.ActorState.onKeyDown

onMouseDown

onMouseDown: function(button,
x,
y,
ev)

Description

A mouse button was pressed

Parameters

buttonThe mouse button that was pressed (e.g. gamvas.mouse.LEFT)
x/yThe position on the screen the mousepointer was while pressed
evThe JavaScript event object

See

gamvas.mouse

onMouseUp

onMouseUp: function(button,
x,
y,
ev)

Description

A mouse button was released

Parameters

buttonThe mouse button that was released (e.g. gamvas.mouse.LEFT)
x/yThe position on the screen the mousepointer was while released
evThe JavaScript event object

See

gamvas.mouse

onMouseMove

onMouseMove: function(x,
y,
ev)

Description

The mouse was moved

Parameters

x/yThe position where the mousecursor was
evThe JavaScript event object

Example

myActorState = gamvas.ActorState.extend({
    onMouseMove: function(x, y) {
       this.followPosition(x, y);
    }
});

onCollision

onCollision: function(a,
ni,
ti,
c)

Description

The actor starts a collision with another actor

Parameters

athe colliding actor
nithe normal impulse (aka how hard did we hit)
tithe tangent impulse (how much rotational force did we get out of the collision)
ca b2Contact object holding low level information about the contact

See

<gamvas.actorstate.onCollisionEnter> <gamvas.actorstate.onCollisionLeave>

Example

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

onCollisionEnter: function(a,
c)

Description

The actor starts a collision with another actor

Parameters

athe colliding actor
ca b2Contact object holding low level information about the contact

See

<gamvas.actorstate.onCollisionEnter>

Example

myActorState = gamvas.ActorState.extend({
    onCollisionEnter: function(a) {
        console.log("i got hit by "+a.name);
    }
});

onCollisionLeave

onCollisionLeave: function(a,
c)

Description

The actor leaves a collision with another actor

Parameters

athe colliding actor
ca b2Contact object holding low level information about the contact

See

<gamvas.actorstate.onCollisionEnter>

doCollide

doCollide: function(a,
c,
om)

Description

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.

Parameters

athe colliding actor
ca b2Contact object holding low level information about the contact
omthe old b2Manifold object holding information about the collision points

See

<gamvas.actorstate.onCollisionEnter>

Example

Do not collide with ghost objects

myActorState = gamvas.ActorState.extend({
    doCollide: function(opponent) {
        if (opponent.type == "ghost") {
            return false;
        }
        return true;
    }
});
this.name
The name of the actor state
this.actor
The actor the state is assigned to, or null if not assigned
init: function()
Gets called once on state initialization.
enter: function()
Gets called when the state is entered.
leave: function()
Gets called when the state is left.
preDraw: function(t)
Gets called after screen clear and before camera handling.
draw: function(t)
Gets called when the state should draw.
postDraw: function(t)
Gets called after camera handling.
update: function(t)
Update the ai.
onKeyDown: function(keyCode,
character,
ev)
A key on the keyboard was pressed
onKeyUp: function(keyCode,
character,
ev)
A key on the keyboard was released
onMouseDown: function(button,
x,
y,
ev)
A mouse button was pressed
onMouseUp: function(button,
x,
y,
ev)
A mouse button was released
onMouseMove: function(x,
y,
ev)
The mouse was moved
onCollision: function(a,
ni,
ti,
c)
The actor starts a collision with another actor
onCollisionEnter: function(a,
c)
The actor starts a collision with another actor
onCollisionLeave: function(a,
c)
The actor leaves a collision with another actor
doCollide: function(a,
c,
om)
This function is called to check of two objects should collide.
getCurrentState: function()
Get the current gamvas.ActorState the actor is in
registerInputEvents: function(act)
Send keyboard/mouse events to gamvas.Actor onKeyDown and similar functions
Information and functions for the mouse.
Close