Character

Gamma provides gma.character, which is a convenience class for representing an entity controlled by the player. In most games there is only one such entity (multiple in multiplayer games) and it is called the character.

The gma.character class extends gma.moveable and provides some extra functions for changing the state of the entity – to control jumping and moving.

Note

See Moveable Entities for what else is provided for the character

Instantiating the character

The character is a moveable entity, which in turn is a rectangle. Therefore you should provide the dimensions and position when specifying a character.

The character is typically instantiated and provided to the manager. For example:

var manager = gma.manager(...);
manager.character = gma.character({
    left     : 0,
    bottom   : 0,
    width    : 3,
    height   : 6,
    depth    : 3
});

See the shapes topic for possible combinations of dimensions and position.

Note

The position properties of a character is ignored (spawn points are defined per level) but is still required.

Jumping

The jump method is used to tell the character to jump. It sets the character's ystate to JUMPING when the character isn't already jumping and sets it's velocity to what has been defined for it's jumpVelocity property.

The jump function is designed to be bound to a key-press:

// Jump when spacebar (keycode 32) is pressed
gma.keyHandler.register(32, manager.character.jump);

Note

If the character is dead (character's alive property isn't set to true) jump will have no effect.

Moving

To tell the character to move left or right, the move method is supplied. This function accepts one of the gma.constants to specify the direction we want to move in (either LEFT or RIGHT) and an event object.

It will then set xState on the character to this direction if the event object is a keydown, otherwise it will set the character's xState to STILL if the event is a keyup.

An example keybinding would be (with or without currying):

// Using currying
gma.keyHandler.register(39, manager.character.move.curry(gma.constants.RIGHT));
gma.keyHandler.register(37, manager.character.move.curry(gma.constants.LEFT));

// Not using currying
gma.keyHandler.register(39, function(e) {
    manager.character.move(gma.constants.RIGHT, e);
});
gma.keyHandler.register(37, function(e) {
    manager.character.move(gma.constants.LEFT, e);
});

Note

If the character is dead (character's alive property isn't set to true) move will have no effect.

Collision functionality

The gma.character class defines additional collision functionality. After doing the collision checks it inherits, the character check if it collided with a gma.collectable and calls pickup on the collectable and increment score on the character.

Note

This is implemented in the collided__pickupCollectable function. See the advanced topic on collision functionality to find out how to customize collision behaviours.

Fork me on GitHub