Saberjs
The first byor [bring your own renderer] js game framework
Foreward
I developed this library to scratch my own itch. I needed a game framework that didn't care which renderer it used. Mostly because I wanted to use my own custom renderer (e2d)
and wasn't able to find anything worth using.
API
Game States
Game states are actually class-like. You define their prototype and this
refers to the game state instance.
var saber = ; var state = //prototype functions goes here { //runs before physics and runs only once } { //runs before render } { //runs after physics }; var instance = state; instanceparam = "value";
Asset Groups
Loading stuff and keeping track of what needs to be loaded can be tricky, so instead of worrying about asset counts, try using the functions returned by the AssetGroup
prototype.
Prototype
imageGroup.assetLoaded
This function returns a function that increments the loaded count and indexes the loaded asset onto imageGroup.index
with the specified id
.
It also takes an Object parameter (which will be indexed) or a function that accepts the parameters of the parent load event and returns the object to be indexed.
var imageGroup = ;var img = ;imgsrc = 'url'; //eitherimg; //or if you want to use a callback { //e is the event object from the img.onload event} img;
imageGroup.assetErrored
This function returns a function that increments the errored count and indexes the loaded asset onto imageGroup.erroredIndex
with the specified id
.
It also takes an Object parameter (which will be indexed) or a function that accepts the parameters of the parent load event and returns the object to be indexed.
Using the above example...
img; img;
Events
asset-loaded
Called when an asset is loaded.
imageGroup;
asset-errored
Called when an asset errors.
imageGroup;
loaded
Called after every asset is accounted for (errored or loaded.)
imageGroup;
Game
The main object that represents a game.
The folowing example is pixi.js
related bunny hello world. Basic.js example
var app = Game; //new Game(); is also finevar renderer = PIXI;documentbody;var stage = ; var helloWorld = { //this is the state instance thisbunnyTexture = PIXITexture; thisbunnySprite = thisbunnyTexture; thisbunnySpriteanchorx = 05; thisbunnySpriteanchory = 05; thisbunnySpritepositionx = 200; thisbunnySpritepositiony = 150; return stage; } { thisbunnySpriterotation += 01; } { return renderer; }; //add a state to the gameapp; //set the parameters for the game states and the tick functionapp; { ; return app;} ;