Skip to content

Thrunium / Reference

Sprite

An image to draw on the game canvas, rotated around its centre. It can also be a sprite map: one image holding several equally sized frames, laid out left to right and wrapping to the next row, which draw() picks from and drawAnim() plays as an animation. Sprites draw on the current THRUNIUM.Game, so create the game first.

The easiest way to get one is to load the image as a sprite-map asset: an assets entry with a spriteMap of { width, height } is replaced by a ready Sprite once it has loaded. You can also construct one yourself from a loaded image.

Constructor

new THRUNIUM.Sprite(image)

  • image HTMLImageElement

    A loaded image to use as the sprite or sprite map.

Creates a sprite from an image. The image must have finished loading, because its size is read here; until you call setSpriteMap() the whole image is one frame. Must be called with new, and a THRUNIUM.Game must exist before the sprite draws.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var image = new Image();
image.onload = function() {
	var sprite = new THRUNIUM.Sprite(image);
	sprite.draw(0, 200, 200);
};
image.src = 'images/hero.png';

General

image: HTMLImageElement

The image the sprite draws from, as passed to the constructor.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	game.assets['hero'].image; // the loaded <img> element
};
game.init();
game.start();

angle: number

Rotation in degrees, clockwise, around the centre of the drawn frame. Default is 0. Set it with setAngle().

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	game.assets['hero'].angle; // 0
};
game.init();
game.start();

fullWidth: number

Width of the whole image in pixels, read from the image when the sprite is created.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	game.assets['hero'].fullWidth; // e.g. 128 for a 128 x 64 image
};
game.init();
game.start();

fullHeight: number

Height of the whole image in pixels, read from the image when the sprite is created. Since 1.1.0 this is the image's height; 1.0.0 used its width.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	game.assets['hero'].fullHeight; // e.g. 64 for a 128 x 64 image
};
game.init();
game.start();

spriteWidth: number

Width of one frame in pixels. It starts as the full image width (one frame) and is set by setSpriteMap().

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	game.assets['hero'].spriteWidth; // 32
};
game.init();
game.start();

spriteHeight: number

Height of one frame in pixels. It starts as the full image height (one frame) and is set by setSpriteMap().

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	game.assets['hero'].spriteHeight; // 32
};
game.init();
game.start();

currentSprite: number | null

The frame drawAnim() is showing, or null when no animation is running. drawAnim() updates it; set it to null to restart an animation from its first frame.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	var hero = game.assets['hero'];
	hero.setLoop(true);
	hero.drawAnim(0, 3, 200, 200);
	hero.currentSprite; // 0, 1, 2 or 3
};
game.init();
game.start();

animTimeDur: number

How long one run of an animation takes, in milliseconds, split evenly over its frames. Default is 100. Set it with setAnimTime().

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	game.assets['hero'].animTimeDur; // 100
};
game.init();
game.start();

frameTimeLast: number

Unix timestamp in milliseconds of when drawAnim() last moved to another frame. Starts at 0.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	var hero = game.assets['hero'];
	hero.drawAnim(0, 3, 200, 200);
	hero.frameTimeLast; // e.g. 1767225600000
};
game.init();
game.start();

animLoop: boolean

Whether drawAnim() starts over after the last frame. Default is false. Set it with setLoop().

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	game.assets['hero'].animLoop; // false
};
game.init();
game.start();

animCallback()

Function called when a non-looping animation finishes. Default does nothing. Set it with setAnimCallback().

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	game.assets['hero'].animCallback(); // does nothing until you set one
};
game.init();
game.start();

setAngle(angle)

  • angle number

    The rotation in degrees.

Sets the rotation, in degrees clockwise, that the sprite is drawn with from now on.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	var hero = game.assets['hero'];
	hero.setAngle(hero.angle + 90 * game.delta); // a quarter turn per second
	hero.draw(0, 200, 200);
};
game.init();
game.start();

setLoop(loop)

  • loop boolean

    true to loop the animation.

Sets whether drawAnim() starts over after the last frame. Without a loop, the animation calls the setAnimCallback() function after its last frame, and the next drawAnim() call starts it again. Since 1.1.0 drawAnim() honours this setting; 1.0.0 ignored it.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	var hero = game.assets['hero'];
	hero.setLoop(true);
	hero.drawAnim(0, 3, 200, 200);
};
game.init();
game.start();

setAnimCallback(fun)

  • fun () => void

    The function to call; it gets no arguments.

Sets the function called each time a non-looping animation finishes.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/explosion.png', index: 'boom', spriteMap: { width: 64, height: 64 } }];
game.startState = 'game';
game.states['game'] = function() {
	var boom = game.assets['boom'];
	boom.setAnimCallback(function() {
		game.state = 'gameover';
	});
	boom.drawAnim(0, 7, 200, 200);
};
game.states['gameover'] = function() {
	game.drawText('Game over', 200, 200);
};
game.init();
game.start();

setAnimTime(time)

  • time number

    Duration of the whole animation in milliseconds.

Sets how long one run of an animation takes, in milliseconds. The time is split evenly over the frames drawAnim() plays. Since 1.1.0 this works; in 1.0.0 it overwrote itself and had no effect.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	var hero = game.assets['hero'];
	hero.setAnimTime(400); // 4 frames, 100 ms each
	hero.setLoop(true);
	hero.drawAnim(0, 3, 200, 200);
};
game.init();
game.start();

setSpriteMap(width, height)

  • width number

    Width of one frame in pixels.

  • height number

    Height of one frame in pixels.

Turns the image into a sprite map of frames of width x height pixels. Frames are numbered from 0, left to right, then row by row. A sprite-map asset has this set already.

JavaScript
var game = new THRUNIUM.Game(400, 400);
var image = new Image();
image.onload = function() {
	var tiles = new THRUNIUM.Sprite(image);
	tiles.setSpriteMap(40, 40);
	tiles.draw(2, 200, 200); // the third 40 x 40 frame
};
image.src = 'images/tiles.png';

draw(imgNum?, x, y, width?, height?)

  • imgNum numberoptional

    Number of the frame to draw, counting from 0 left to right, then row by row. Default is 0.

  • x number

    X coordinate of the centre of the drawn frame.

  • y number

    Y coordinate of the centre of the drawn frame.

  • width numberoptional

    Width to draw the frame at. Default is the frame width.

  • height numberoptional

    Height to draw the frame at. Default is the frame height.

Draws one frame centred on (x, y), rotated by angle. The frame is drawn at its own size unless you pass width and height.

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/tiles.png', index: 'tiles', spriteMap: { width: 40, height: 40 } }];
game.startState = 'game';
game.states['game'] = function() {
	game.assets['tiles'].draw(0, 50, 50); // frame 0 at its own size
	game.assets['tiles'].draw(1, 150, 50, 80, 80); // frame 1 at double size
};
game.init();
game.start();

drawAnim(imgFrom, imgTo, x, y, width?, height?)

  • imgFrom number

    Number of the first frame of the animation.

  • imgTo number

    Number of the last frame of the animation.

  • x number

    X coordinate of the centre of the drawn frame.

  • y number

    Y coordinate of the centre of the drawn frame.

  • width numberoptional

    Width to draw the frame at. Default is the frame width.

  • height numberoptional

    Height to draw the frame at. Default is the frame height.

Plays an animation over the frames imgFrom to imgTo: call it every frame of your game state and it draws the current frame, moving to the next one when its share of animTimeDur has passed. After imgTo it starts over when setLoop(true) is set; otherwise it calls the setAnimCallback() function, and the next call starts again from imgFrom. Drawing works as in draw().

JavaScript
var game = new THRUNIUM.Game(400, 400);
game.assets = [{ src: 'images/hero.png', index: 'hero', spriteMap: { width: 32, height: 32 } }];
game.startState = 'game';
game.states['game'] = function() {
	// frames 0 to 3, drawn at double size
	game.assets['hero'].drawAnim(0, 3, 200, 200, 64, 64);
};
game.init();
game.start();