Skip to content

Modifying a sprites behaviour

Steve Butler edited this page Sep 17, 2026 · 1 revision

There are three ways in which a sprite's behaviour can be changed. All rely on the sprite being added to an Animator.

Dynamics

Sprites are normally stationary except for any animation that is applied. To make sprites move you need to set the sprite's dynamics property to a Dynamics instance. For example:

sprite.dynamics = new hcjeLib.sprites.Dynamics();

If you change the properties of the dynamics instance, the Animator will automatically apply any accelerations or velocities and update the sprite's positions accordingly.

Limiters

The Dynamics object can also be provided with a DynamicsLimiter instance when it is constructed. The limiter can then modify the result of any dynamics on each frame. An example of a limiter is shown below:

const GAME_DIMENSIONS = gameArea.designDims;
const limiter = {
  limit: (target, dynamics) => {
    let position = target.position;
    const bounds = target.bounds; 
    if (position.y + bounds.height > GAME_DIMENSIONS.height) {
      position.y = GAME_DIMENSIONS.height - bounds.height;
      dynamics.vy = 0;
      dynamics.ay = 0;
    }
  }
};
mySprite.dynamics = new hcjeLib.sprites.Dynamics(limiter);

This limiter merely stops the sprite if it reaches the bottom edge of the game. Note that limiters should normally only operate on the dynamics properties or the sprite's position data. Although a limiter has access to the sprite, manipulation of the additional properties are discouraged. More sophisticated manipulation should normally be done via an adjuster as described below.

Adjusters

The limiters described above belong to the Dynamics objects because they are intended primarily for limiting the dynamics of a sprite and its position. Adjusters, however, are all derived from the BaseSpriteAdjuster class and are expected to have multiple effects upon a sprite. They also have a lifetime which can call an onCompletion function when they have finished their task.

Adjusters are implemented by setting the sprite's adjuster property to an appropriate adjuster instance. For example:

sprite.position = {x: 0, y: 0, angle:0};
const adjuster = new hcjeLib.sprites.ReachTargetXY(sprite, 100, 100);
adjuster.onCompletion = () => {
  console.log('Move complete');
}
sprite.adjuster = adjuster;  

Some standard adjusters are provided as part of the HCJE:

Completion of adjuster's actions can either be determined by the adjuster itself during a call to its adjust method or by the main game calling the adjuster's markComplete method.

If you set the sprite's adjuster property, any existing adjuster that had been set is automatically marked as complete but its onCompletion callback is not called.

Clone this wiki locally