Skip to content

Thrunium / Reference

Utilities

The library version and small helpers that live directly on THRUNIUM.

General

version: string

The version of Thrunium that is loaded, such as "1.1.0".

JavaScript
console.log(THRUNIUM.version); // '1.1.0'

pageLoad()

Marks the page as loaded. Thrunium calls it itself, through ready(), as soon as the page has loaded, and the game's loading screen waits for it before switching to startState. You do not need to call it.

JavaScript
THRUNIUM.pageLoad(); // already done for you once the page has loaded

ready(fn)

  • fn () => void

    The function to run.

Runs fn once, as soon as the page's HTML has been loaded and parsed. If the page has already finished loading, fn runs right away. Use it to start your game only when the page is ready. Since 1.1.0 every call runs its function; 1.0.0 silently ignored any ready() call after the first (and since Thrunium calls ready(pageLoad) itself, your own calls were effectively ignored).

JavaScript
THRUNIUM.ready(function() {
	var game = new THRUNIUM.Game(400, 400);
	game.init();
});

throwError(txt)

  • txt string

    The error message.

Logs txt as an error in the console and stops the game loop after the current frame. Thrunium uses it for errors it cannot recover from, such as a collision check on a badly defined shape; you can use it the same way.

JavaScript
var levels = ['intro', 'forest'];
var level = levels[5];
if (level === undefined) {
	THRUNIUM.throwError('Level 5 does not exist');
}

mobileAndTabletcheck()

returnboolean

Returns true if the browser runs on a mobile phone or tablet, judged from its user agent (the patterns of detectmobilebrowsers.com). Use it to show touch controls, for example.

JavaScript
if (THRUNIUM.mobileAndTabletcheck()) {
	console.log('Tap to jump');
} else {
	console.log('Press space to jump');
}

iOScheck()

returnboolean

Returns true if the browser runs on an iPad, iPhone or iPod, judged from its user agent. iPads that report themselves as a Mac are not detected.

JavaScript
if (THRUNIUM.iOScheck()) {
	console.log('Running on iOS');
}

getRandom(min, max, float?)

  • min number

    The lowest possible result. Pass a whole number when float is false.

  • max number

    The highest possible result (for decimals, the limit it stays below). Pass a whole number when float is false.

  • float booleanoptional

    true for a decimal number. Default is false.

returns number

Returns a random number between min and max. By default it is a whole number, and both min and max can come up. With float set to true it is a decimal number from min up to, but not including, max. Since 1.1.0 decimal results never exceed max; 1.0.0 could return up to max + 1.

JavaScript
var dice = THRUNIUM.getRandom(1, 6); // 1, 2, 3, 4, 5 or 6
JavaScript
var speed = THRUNIUM.getRandom(0.5, 2, true); // e.g. 1.2734