Namespace

Sprite

Sprite

Methods

# addSprite(sprite, callback)

Dynamically adds a sprite to the current board, when the sprite has been added the callback function will be invoked, if any. Useful for spawning item pickups or enemies.
Parameters:
Name Type Description
sprite Object BoardSprite object to add.
callback Callback If defined, the function to invoke after the sprite has been added.

View Source rpgcode.js, line 260

Example
var sprite = {
 "name":"skeleton.enemy",
 "id":"test3",
 "thread":"AI/PassiveEnemy.js",
 "startingPosition":{
    "x":232,
    "y":120,
    "layer":1
 },
 "events":[
    {
      "program":"AI/BumpCharacter.js",
       "type":"overlap",
       "key":""
    }
 ]
};

rpgcode.addSprite(sprite, function() {
 rpgcode.log("Sprite added to board.");
});

# animateSprite(spriteId, animationId, callback)

Animates the sprite using the requested animation. The animationId must be available for the sprite.
Parameters:
Name Type Description
spriteId string The ID set for the sprite as it appears in the editor.
animationId string The requested animation to play for the sprite.
callback Callback If defined, the function to invoke at the end of the animation.

View Source rpgcode.js, line 282

Example
var spriteId = "mysprite";
var animationId = "DANCE";
rpgcode.animateSprite(spriteId, animationId, function() {
 rpgcode.log("Finished dancing");
});

# destroySprite(spriteId)

Destroys a particular sprite instance and removes it from the engine.
Parameters:
Name Type Description
spriteId string The ID set for the sprite as it appears in the editor.

View Source rpgcode.js, line 570

Example
rpgcode.destroySprite("evil-eye-1");

# getEnemy(spriteId) → {Asset.EnemyAsset|null}

Gets the enemy asset by its spriteId as set in the editor. Returns null if the spriteId cannot be found.
Parameters:
Name Type Description
spriteId string

View Source rpgcode.js, line 1446

Asset.EnemyAsset | null
Example
const enemy = rpgcode.getEnemy("enemy-1");
console.log(enemy.name);

# getNpc(spriteId) → {Asset.NpcAsset|null}

Gets the npc asset by its spriteId as set in the editor. Returns null if the spriteId cannot be found.
Parameters:
Name Type Description
spriteId string

View Source rpgcode.js, line 1568

Asset.NpcAsset | null
Example
const npc = rpgcode.getNpc("npc-1");
console.log(npc.name);

# getSprite(spriteId) → {Asset.Sprite}

Gets the sprite associated with the ID set in the Board editor.
Parameters:
Name Type Description
spriteId string

View Source rpgcode.js, line 1676

Asset.Sprite
Example
var sprite = rpgcode.getSprite("MySprite");

# getSpriteDirection(spriteId) → {"NORTH"|"SOUTH"|"EAST"|"WEST"|"NORTH_EAST"|"SOUTH_EAST"|"NORTH_WEST"|"SOUTH_WEST"}

Gets the sprites's current direction.
Parameters:
Name Type Description
spriteId string ID associated with the sprite.

View Source rpgcode.js, line 1692

"NORTH" | "SOUTH" | "EAST" | "WEST" | "NORTH_EAST" | "SOUTH_EAST" | "NORTH_WEST" | "SOUTH_WEST"
Example
var direction = rpgcode.getSpriteDirection();
rpgcode.log(direction);

# getSpriteLocation(spriteId, inTiles, includeOffset) → {Geometry.Location}

Gets the sprites's current location, optionally including the visual offset that happens when boards are smaller than the viewport dimensions.
Parameters:
Name Type Description
spriteId string ID associated with the sprite.
inTiles boolean Should the location be in tiles, otherwise pixels.
includeOffset boolean Should the location include the visual board offset.

View Source rpgcode.js, line 1623

An object containing the sprite's location.
Example
var location = rpgcode.getSpriteLocation("rat");
rpgcode.log(location);

# moveSprite(spriteId, direction, distance)

Moves the sprite by the requested number of pixels in the given direction.
Parameters:
Name Type Description
spriteId string The ID set for the sprite as it appears in the editor.
direction string The direction to push the sprite in e.g. NORTH, SOUTH, EAST, WEST.
distance number Number of pixels to move.

View Source rpgcode.js, line 2169

Example
// Move the sprite with id "mysprite" in the north direction by 50 pixels.
var spriteId = "mysprite";
var direction = "NORTH";
var distancePx = 50;
rpgcode.moveSprite(spriteId, direction, distancePx);

# moveSpriteTo(spriteId, x, y, duration, callback)

Moves the sprite to the (x, y) position, the sprite will travel for the supplied duration (milliseconds). A short duration will result in the sprite arriving quicker and vice versa.
Parameters:
Name Type Description
spriteId string The ID set for the sprite as it appears in the editor.
x number A pixel coordinate.
y number A pixel coordinate.
duration number Time taken for the movement to complete (milliseconds).
callback Callback Function to invoke when the sprite has finished moving.

View Source rpgcode.js, line 2293

Example
// Move towards (100, 150) for 50 milliseconds, this will animate the sprite.
var spriteId = "mysprite";
var x = 100;
var y = 150;
var delay = 50;
rpgcode.moveSpriteTo(spriteId, x, y, delay);

# pauseSprites(spriteIds)

Pauses animation and movement for all requested sprites.
Parameters:
Name Type Description
spriteIds string Sprites to pause.

View Source rpgcode.js, line 2102

Example
// Pauses every sprite
rpgcode.pauseSprites();

// Pauses just one sprite
rpgcode.pauseSprites(["sprite-1"]);

// Pauses multiple sprites
rpgcode.pauseSprites(["sprite-1", "sprite-2", "sprite-3"]);

# resumeSprites(spriteIds)

Resumes animation and movement for all requested sprites.
Parameters:
Name Type Description
spriteIds string Sprites to resume.

View Source rpgcode.js, line 2746

Example
// Resumes every sprite
rpgcode.resumeSprites();

// Resumes just one sprite
rpgcode.resumeSprites(["sprite-1"]);

// Resumes multiple sprites
rpgcode.resumeSprites(["sprite-1", "sprite-2", "sprite-3"]);

# setSpriteLocation(spriteId, x, y, layer, inTiles)

Sets the location of the sprite.
Parameters:
Name Type Description
spriteId string The ID set for the sprite as it appears in the editor.
x number In pixels by default.
y number In pixels by default.
layer number Target layer to put the sprite on.
inTiles boolean Is (x, y) in tile coordinates, defaults to pixels.

View Source rpgcode.js, line 3149

Example
// Place a sprite at the tile coordinates (10, 10, 1).
var spriteId = "mysprite";
var x = 10;
var y = 10;
var layer = 1;
var inTiles = true;
rpgcode.setSpriteLocation(spriteId, x, y, layer, inTiles);

# setSpriteStance(spriteId, stanceId)

Sets the sprite's current stance, uses the first frame in the animation.
Parameters:
Name Type Description
spriteId string The ID set for the sprite as it appears in the editor.
stanceId string The stanceId (animationId) to use.

View Source rpgcode.js, line 3179

Example
// Set the sprite with ID "mysprite" to its idle stance.
var spriteId = "mysprite";
var stanceId = "IDLE";
rpgcode.setSpriteStance(spriteId, stanceId);