The actor class is the most important class in gamvas. You use it for the player, ai opponents, physics objects, maybe even static objects in your gameworld. It forms a object that can hold animations, physical properties, states, logic and can react to events like keyboard or mouse input or collisions. Actors hold a default animation and a default state, so if you only need one of them, you can use these, see example below.
new gamvas.Actor(name, x, y);
name | a unique name in the game world or false to let the name autogenerate |
x/y | the position of the actors center in the world (optional) |
gamvas.ActorState gamvas.Class gamvas.Image
// extend gamvas.Actor to create a new instancable actor myActor = gamvas.Actor.extend({ // the create function is the constructor create: function(name, x, y) { // call our super class constructor to initialize the actor this._super(name, x, y); // get our current state to get access to the resource handler var st = gamvas.state.getCurrentState(); // load our animation with 64x64 pixels per frames, 10 frames // playing with 20 frames per second this.setFile(st.resource.getImage('anim.png'), 64, 64, 10, 20); // get default actor state var defState = this.myActor.getCurrentState(); // install brain, move 10 pixels per second to the right defState.update = function(t) { this.actor.move(10*t, 0); }; } });
gamvas. | The actor class is the most important class in gamvas. |
Variables | |
name | The name of the actor |
position | A gamvas.Vector2D object with the position information |
center | A gamvas.Vector2D object with the center offset of the actor |
currentAnimation | The name of the current animation |
rotation | The rotation of the actor |
scaleFactor | The scale factor of the object |
density | The physical property of density. |
friction | The physical property of friction Defines the friction of objects. |
resitution | The physical property of resitution (bounce). |
type | Allows you to set types to your physics objects to identify them. |
layer | Integer value used for z-sorting on automatic draw through gamvas.State.addActor |
usePhysics | Delivers true/false if this object has physics enabled. |
body | The Box2D b2Body, if physics is enabled |
fixture | The Box2D b2Fixture, if physics is enabled |
Functions | |
init | Description |
addState | Adds a actor state |
addAnimation | Adds a animation |
createBody | Create a Box2D b2Body object for the actor. |
bodyRect | make the actor a physics object with a rectangular body shape |
bodyCircle | make the actor a physics object with a circle as body shape |
bodyPolygon | make the actor a physics object with a non rectangular polygon shape If you just need a rectangle, there is a shortcut: <gamvas.actor.bodyRect> |
resetForces | Resets all physical forces on the actor |
setBullet | Defines if a actor is a physics ‘bullet’. |
setSensor | Defines if a actor is a physics sensor or not |
setAwake | Allows you to wake (or sleep) a actor in the physics simulation |
setFixedRotation | Object does not rotate, when under physics controll |
setName | Set the actor name |
setRotation | Set certain rotation of the actor in radians |
rotate | Rotate the actor |
setPosition | Set the position of a actor |
move | Move the actor |
setScale | Set a certain scale factor |
scale | Scale a object |
setLinearDamping | Sets the linear damping of the physics object. |
setAngularDamping | Sets the angular damping of the physics object. |
setCenter | Set the center for an actor. |
getCurrentAnimation | Get the current gamvas.Animation object that is playing |
getCurrentState | Get the current gamvas.ActorState the actor is in |
setFile | Sets a image file for the current animation |
preDraw | Gets called after screen clear and before camera handling. |
draw | Gets called when the actor is drawn. |
postDraw | Gets called after camera handling. |
isActive | Check if the actor is active |
setActive | Enable or disable the actor for automatic drawing (if in states actors list) |
setState | Switch the actor to a certain gamvas.ActorState |
setAnimation | Switch the actor to a certain gamvas.Animation |
setFPS | Sets the frames per seconds of the current animation |
setFrameList | Sets the list of frames of the current animation |
setGroupIndex | Set the Box2D filter group index |
setCategoryBits | Sets the Box2D category bits |
setMaskBits | Sets the Box2D mask bits |
addRevoluteJoint | Add a Box2D revolute joint between this actor and another |
addPrismaticJoint | Add a Box2D prismatic joint between this actor and another |
this.position
A gamvas.Vector2D object with the position information
<gamvas.actor.setPosition> <gamvas.actor.move>
this.friction
The physical property of friction Defines the friction of objects. A ice ground would have a very low friction, maybe 0.1 or less, while a rubber ground would have a high value, close to 1.0. So if you would push a ball over these grounds, on the low friction ice ground, the ball might slip over where as on the high friction rubber ground it would immediately start to roll.
0.5
The physical property of resitution (bounce). Defines how bouncy a object is. A value of 1.0 means, that if falling to the ground, the object would jump exactly as high up as it fell down, wile smaller values will give a more natural bouncyness where with every bounce, it would bounce less, until it stops bouncing.
0.3
this.layer
Integer value used for z-sorting on automatic draw through gamvas.State.addActor
Versions prior 0.8.3 used the layer value in the wrong direction, if you have used gamvas before this version, see gamvas.config.reverseLayerOrder
0
this.usePhysics
Delivers true/false if this object has physics enabled. This is meant to be read only, it will be set automatically when creating a physics body.
gamvas.Actor.createBody gamvas.Actor.bodyRect gamvas.Actor.bodyCircle gamvas.Actor.bodyPolygon
addState: function( state, activate )
Adds a actor state
state | a gamvas.ActorState object |
activate | if true, immediately switch to the new state (optional) |
myRunningState = gamvas.ActorState.extend({ update: function(t) { this.actor.move(100*t, 0); }, onKeyUp: function(keyCode) { if ( (keyCode == gamvas.key.RIGHT) || (keyCode = gamvas.key.LEFT) ) { this.actor.setState('walking'); } } }); myActor = gamvas.Actor.extend({ create: function(name, x, y) { this._super(name, x, y); this.addState(new myRunningState('running')); } });
addAnimation: function( anim )
Adds a animation
anim | a gamvas.Animation object |
myActor = gamvas.Actor.extend({ create: function(name, x, y) { this._super(name, x, ,y); this.addAnimation( new gamvas.Animation('running', this.resource.getImage('playerRun.png'), 64, 64, 12, 20) ); } });
createBody: function( type, shape )
Create a Box2D b2Body object for the actor. This is the most flexible way to create a body, but you have to do the Box2D stuff quite low level
This is the low level function to add physics to your object. Unless you are required to for some reason, you would normally not use this function directly, but use gamvas.Actor.bodyRect, gamvas.Actor.bodyCircle or gamvas.Actor.bodyPolygon
type | the Box2D body type (dynamic/static/...) |
shape | the Box2D shape for collision detection |
gamvas.physics.DYNAMIC | A body that moves, collides, etc (gamvas.physics.DYNAMIC) |
gamvas.physics.STATIC | A body that does not move, like ground, walls, etc (gamvas.physics.STATIC) |
gamvas.physics.KINEMATIC | A body that does move, but under player or AI controll (gamvas.physics.KINEMATIC) |
gamvas.Actor.bodyRect gamvas.Actor.bodyPolygon gamvas.Actor.bodyCircle
myPlayer = gamvas.Actor.extend({ create: function(name, x ,y) { this._super(name, x, y); // create a polygon shape var shape = new Box2D.Collision.Shapes.b2PolygonShape; // create a box shape with 200 by 100 pixels // NOTE: For demonstration only, use gamvas.Actor.bodyRect for this scenario shape.SetAsBox(gamvas.physics.toWorld(200), gamvas.physics.toWorld(100)); // now finally create the body, adds the actor to physics simulation automatically this.createBody(gamvas.physics.DYNAMIC, shape); } });
bodyRect: function( x, y, width, height, type )
make the actor a physics object with a rectangular body shape
x/y | the position of the body in the physics world |
width/height | the half dimension of the rectangle, in pixels |
type | the Box2D body type (See gamvas.Actor.createBody) (optional) |
gamvas.Actor.createBody gamvas.Actor.bodyPolygon gamvas.Actor.bodyCircle
bodyCircle: function( x, y, radius, type )
make the actor a physics object with a circle as body shape
x/y | the position of the body in the physics world |
radius | the radius of the circle in pixels |
type | the Box2D body type (See gamvas.Actor.createBody) (optional) |
gamvas.Actor.createBody gamvas.Actor.bodyRect gamvas.Actor.bodyPolygon
bodyPolygon: function( x, y, polys, cx, cy, type )
make the actor a physics object with a non rectangular polygon shape If you just need a rectangle, there is a shortcut: <gamvas.actor.bodyRect>
Although Box2D documentation requires a counter clock wise (CCW) polygon, gamvas does require you to secify a clock wise polygon, as in the gamvas worl, the y axis represents screen pixels and therefor runs down, whereas Box2D world represents a real life world, where positive values on the y axis run up.
Box2D does not collide concave polygons (as in curving inward, like a skateboard ramp), make sure your polygons are convex (as in curving outward, like a hexagon).
x/y | the position of the body in the physics world |
polys | a array holding arrays of the pixel coordinates of the vertices |
cx/cy | the center of the polygon object (optional) |
type | the Box2D body type (See gamvas.Actor.createBody) (optional) |
gamvas.Actor.createBody gamvas.Actor.bodyRect gamvas.Actor.bodyCircle
Create a triangle object with a image of the size 64 by 64 pixels with the center in the middle, at screen position 200, 50
this.bodyPolygon(200, 50, [ [32, 0], [64, 64], [0, 64] ], 32, 32);
resetForces: function( x, y, r )
Resets all physical forces on the actor
When called without parameter, it just stops rotation and movement. With the optional parameters, you can fully reset the actor to its starting position/rotation
x/y | The position to set the actor to (optional) |
r | The rotation to set the actor to (in radians) |
setBullet: function( b )
Defines if a actor is a physics ‘bullet’.
A bullet gets increased precision (but slower) collision calculation. Immagine a bullet flying towards a thin piece of metal. In real world, that bullet would hit the metal, no matter what, because the real world is not running in frames per second. The physics simulation on the other hand does, therefor, if the bullet moves very fast, the bullet actually skips through the air, because it traveled a certain amount since the last frame redraw. This can lead to very fast moving objects falling through walles that they would collide when they would move slower, because they skiped a distance larger then this colliding objects width.
By setting a actor to a bullet, you can ensure that it always collides, even if it skips that part because of the frame based simulation.
This is a expensive operation, only use it on important objects (like e.b. the players spaceship) and only if necessary because your objects falls through stuff that it should collide with
b | true/false |
false
setSensor: function( b )
Defines if a actor is a physics sensor or not
Sensors do recognize collisions, but are not influenced by the collision forces, so will continue moving as there would not be a collision, but there is one.
You would use sensors to trigger events, like open a door if the player stands right before it.
b | true/false |
false
rotate: function( r )
Rotate the actor
r | the amount to rotate the actor in radians |
gamvas.Actor.setRotation http://en.wikipedia.org/wiki/Radians
setCenter: function( x, y )
Set the center for an actor. If you have a round object for example with a size of 64 by 64 pixels and you want to rotate it around the center, you would use myObject.setCenter(32, 32);
x/y | the center, as seen of the upper left corner of the object |
getCurrentAnimation: function()
Get the current gamvas.Animation object that is playing
setFile: function( file, frameWidth, frameHeight, numberOfFrames, fps )
Sets a image file for the current animation
file | a JavaScript Image object holding the animation in tiles |
frameWidth | the width of a single frame tile of the animation |
frameHeight | the height of a single frame tile of the animation |
numberOfFrames | the number of frames in the animation |
fps | the speed of the animation in frames per second |
myActor = gamvas.Actor.extend({ create: function(name, x, y) { this._super(name, x,, y); var st = gamvas.state.getCurrentState(); this.setFile(st.resource.getImage('anim.png'), 64, 64, 10, 20); } });
draw: function( t )
Gets called when the actor is drawn. Usually you would not overwrite this function with your logic, use the gamvas.ActorState.update function of either the default state or a custom gamvas.ActorState
t | the time since last redraw |
setState: function( stateName )
Switch the actor to a certain gamvas.ActorState
stateName | the name of the gamvas.ActorState |
setAnimation: function( a )
Switch the actor to a certain gamvas.Animation
a | the name of the gamvas.Animation |
setFPS: function( fps )
Sets the frames per seconds of the current animation
gamvas.Animation.setFPS for more information
setFrameList: function( fl )
Sets the list of frames of the current animation
gamvas.Animation.setFrameList for more information
setGroupIndex: function( g )
Set the Box2D filter group index
All members of a negative group never collide, or of a positive group always collide
g | The group index, negative or positive values disable or enable collision |
Set 4 objects group index, so ncol1 and ncol2 never collide with each other but all other objects collide with everything
this.ncol1.setGroupIndex(-1); this.ncol2.setGroupIndex(-1); this.col1.setGroupIndex(1); this.col2.setGroupIndex(1);
setCategoryBits: function( b )
Sets the Box2D category bits
You can have up to 16 categories for your physics objects like for example player, monster, coins and together with <gamvas.actor.setMaskBits> you can specify which category can collide with which
b | a bitfield which category the current object belongs |
<gamvas.actor.setMaskBits>
setMaskBits: function( b )
Sets the Box2D mask bits
You can have up to 16 categories for your physics objects. With the mask bits you specify with which category to collide with, whereas 1 means collide, and 0 means ignore collision.
b | a bitfield which categories to collide with |
<gamvas.actor.setCategoryBits>
addRevoluteJoint: function( t, tp, params )
Add a Box2D revolute joint between this actor and another
Revolute joints are hinge like joints, you can limit their angle and use them as motors.
t | the target actor |
tp | the target position, either as b2Vec2D or gamvas.Vector2D (optional) |
params | a list of joint parameters, see below (optional) |
For detailed description see Box2D manual on http://box2d.org/manual.html
lowerAngle | angle in radians |
upperAngle | angle in radians |
enableLimit | true/false |
maxMotorTorque | maximum torque value |
motorSpeed | current motor speed |
enableMotor | true/false |
addPrismaticJoint: function( t, tp, tv, params )
Add a Box2D prismatic joint between this actor and another
Prismatic joints are joints movable along a axis, similar to springs
t | the target actor |
tp | the target position, either as b2Vec2D or gamvas.Vector2D (optional) |
tv | the target vector, aka the direction of the joint, either as b2Vec2D or gamvas.Vector2D (optional) |
params | a list of joint parameters, see below (optional) |
For detailed description see Box2D manual on http://box2d.org/manual.html
upperTranslation | the upper translation limit |
lowerTranslation | the lower translation limit |
enableLimit | true/false |
maxMotorForce | the maximum motor force |
motorSpeed | the current motor speed |
enableMotor | true/false |
upperTranslation and lowerTranslation should allow the value zero between them, because the joint will start with zero. For example: upperTranslation = -0.2 lowerTranslation = 0.3
The name of the actor
this.name
A gamvas.Vector2D object with the position information
this.position
A gamvas.Vector2D object with the center offset of the actor
this.center
The name of the current animation
this.currentAnimation
The rotation of the actor
this.rotation
The scale factor of the object
this.scaleFactor
The physical property of density.
this.density
The physical property of friction Defines the friction of objects.
this.friction
Allows you to set types to your physics objects to identify them.
this.type
Integer value used for z-sorting on automatic draw through gamvas.State.addActor
this.layer
Add a gamvas.Actor to the state.
addActor: function( act )
Delivers true/false if this object has physics enabled.
this.usePhysics
The Box2D b2Body, if physics is enabled
this.body
The Box2D b2Fixture, if physics is enabled
this.fixture
Description
init: function()
Adds a actor state
addState: function( state, activate )
Adds a animation
addAnimation: function( anim )
Create a Box2D b2Body object for the actor.
createBody: function( type, shape )
make the actor a physics object with a rectangular body shape
bodyRect: function( x, y, width, height, type )
make the actor a physics object with a circle as body shape
bodyCircle: function( x, y, radius, type )
make the actor a physics object with a non rectangular polygon shape If you just need a rectangle, there is a shortcut: gamvas.actor.bodyRect
bodyPolygon: function( x, y, polys, cx, cy, type )
Resets all physical forces on the actor
resetForces: function( x, y, r )
Defines if a actor is a physics ‘bullet’.
setBullet: function( b )
Defines if a actor is a physics sensor or not
setSensor: function( b )
Allows you to wake (or sleep) a actor in the physics simulation
setAwake: function( b )
Object does not rotate, when under physics controll
setFixedRotation: function( b )
Set the actor name
setName: function( name )
Set certain rotation of the actor in radians
setRotation: function( r )
Rotate the actor
rotate: function( r )
Set the position of a actor
setPosition: function( x, y )
Move the actor
move: function( x, y )
Set a certain scale factor
setScale: function( s )
Scale a object
scale: function( s )
Sets the linear damping of the physics object.
setLinearDamping: function( d )
Sets the angular damping of the physics object.
setAngularDamping: function( d )
Set the center for an actor.
setCenter: function( x, y )
Get the current gamvas.Animation object that is playing
getCurrentAnimation: function()
Get the current gamvas.ActorState the actor is in
getCurrentState: function()
Sets a image file for the current animation
setFile: function( file, frameWidth, frameHeight, numberOfFrames, fps )
Gets called after screen clear and before camera handling.
preDraw: function( t )
Gets called when the actor is drawn.
draw: function( t )
Gets called after camera handling.
postDraw: function( t )
Check if the actor is active
isActive: function()
Enable or disable the actor for automatic drawing (if in states actors list)
setActive: function( yesno )
Switch the actor to a certain gamvas.ActorState
setState: function( stateName )
Switch the actor to a certain gamvas.Animation
setAnimation: function( a )
Sets the frames per seconds of the current animation
setFPS: function( fps )
Sets the list of frames of the current animation
setFrameList: function( fl )
Set the Box2D filter group index
setGroupIndex: function( g )
Sets the Box2D category bits
setCategoryBits: function( b )
Sets the Box2D mask bits
setMaskBits: function( b )
Add a Box2D revolute joint between this actor and another
addRevoluteJoint: function( t, tp, params )
Add a Box2D prismatic joint between this actor and another
addPrismaticJoint: function( t, tp, tv, params )
Reverse layer sorting
reverseLayerOrder: false
Update the ai.
update: function( t )
Set the speed of the animation in frames per second
setFPS: function( fps )
Allows to set a list of frames that are considdered to be the animation
setFrameList: function( fl )