Core
Game
The global GAME
object is essentially the guts of JackyJS. It is responsible for handling the game loop, checking for events, and rendering output. It's also host to a bunch of constants, variables, events, and useful functions to make developing web games much easier.
TIP: Pay special attention to the Inits table, as that's where all your awesome code goes. :-)
Global Constants
The following are global constants accessed through GAME.cons
, which cannot (should not) be changed:
cons: {
BLEND: {
NORMAL: PIXI.blendModes.NORMAL,
ADD: PIXI.blendModes.ADD,
MULTIPLY: PIXI.blendModes.MULTIPLY,
SCREEN: PIXI.blendModes.SCREEN,
OVERLAY: PIXI.blendModes.OVERLAY,
DARKEN: PIXI.blendModes.DARKEN,
LIGHTEN: PIXI.blendModes.LIGHTEN,
COLOR_DODGE: PIXI.blendModes.COLOR_DODGE,
COLOR_BURN: PIXI.blendModes.COLOR_BURN,
HARD_LIGHT: PIXI.blendModes.HARD_LIGHT,
SOFT_LIGHT: PIXI.blendModes.SOFT_LIGHT,
DIFFERENCE: PIXI.blendModes.DIFFERENCE,
EXCLUSION: PIXI.blendModes.EXCLUSION,
HUE: PIXI.blendModes.HUE,
SATURATION: PIXI.blendModes.SATURATION,
COLOR: PIXI.blendModes.COLOR,
LUMINOSITY: PIXI.blendModes.LUMINOSITY
},
DIRECTION: {
RIGHT: 0,
UP: 90,
LEFT: 180,
DOWN: 270
}
}
Global Variables
The following are global variables accessed through GAME.vars
, which can be changed at any time:
vars: {
SCREEN_X: 0,
SCREEN_Y: 0,
GRAVITY: 0.5,
GRAVITY_DIRECTION: 270,
SPEED_MAX: 12,
FRICTION: 0.15,
PARTICLE_LIMIT: 100
}
Global Custom
The following is where your custom global constants, variables, and functions should be placed. It is accessed through GAME.custom
and initialized in the GAME.initCustom()
function. Placing constants, variables, and functions here allows you to access them from anywhere in your game code, without worrying about polluting the global window
object scope.
custom {
cons: {},
vars: {},
funcs: {}
}
For example:
GAME.initCustom(function(){
GAME.custom.cons.ENEMIES_MAX = 10;
GAME.custom.cons.BULLETS_MAX = 100;
GAME.custom.vars.ENEMY_COUNT = 0;
GAME.custom.vars.BULLET_COUNT = 0;
GAME.custom.funcs.checkEnemies = function(){
if (GAME.custom.vars.BULLET_COUNT > GAME.custom.cons.BULLETS_MAX){
GAME.find({instanceOf:'bullet'}).each(function(obj){
obj.destroy();
});
}
};
});
Global Instances
There is a special global variable called GAME.Instances
. It stores instantiated entities for quick access; that is, instead of having to use GAME.find
all the time. Basically, whatever unique name you provided for an entity during creation, is what gets registered in this global variable. It is for this reason, even though not mandatory, to always provide a name for your entities.
Global instances are available the moment the first room has started. They can be used at any time and in any entity event thereafter. What this means is that any entity can access any other entity's properties, methods, and events! A useful feature for sure. ;-)
NOTE: unless a particular entity is set as persistent, it will no longer be available in GAME.Instances
after a Room transition; ie, it doesn't exist anymore.
The naming convention is as follows:
- uses name if provided, else, asset name.
- removes underscores and hyphens.
- captializes all characters.
Consider the following example:
GAME.initRooms(function(){
var level1 = GAME.Room.create({name:'level1'});
level1.onCreate(function(obj){
obj.addSprite('carrot', {x:0, y:0});
obj.addSprite('carrot', {name:'carrot-2', x:16, y:16});
obj.addSprite('carrot', {x:32, y:32});
});
level1.onStart(function(obj){
// spins all carrots
GAME.find({instanceOf:'carrot'}).each(function(obj){
obj.spin();
});
// blinks only the third carrot
// global instance name overridden since unique name not provided
GAME.Instances.CARROT.blink();
// destroys only the second carrot
GAME.Instances.CARROT2.destroy();
});
});
TIP: in cases where you may want multiple instances without unique names, you'll have to use the GAME.find
function to precisely target and iterate over these kinds of objects.
Properties (read-only)
Name | Type | Description |
---|---|---|
width | Number | the actual pixel width of the game, before scaling. |
height | Number | the actual pixel height of the game, before scaling. |
orientation | String | the device orientation ('portrait' OR 'landscape'). |
scale | Boolean | whether the game can scale proportionately. (default: FALSE) |
mouseX | Number | the current mouse screen X value (top left is 0, 0). |
mouseY | Number | the current mouse screen Y value (top left is 0, 0). |
scrollX | Number | the horizontal page scroll amount. |
scrollY | Number | the vertical page scroll amount. |
hasAdBlock | Boolean | is TRUE if an ad blocker is detected. |
hasStorage | Boolean | is TRUE if localStorage is present (See the Classes > Data for more info). |
hasFocus | Boolean | is TRUE if game has focus. |
isFullScreen | Boolean | is TRUE if game is in full screen. |
isMobile | Boolean | is TRUE if game is running on a mobile device (iphone, ipad, ipod, android, windows phone). |
Methods
Events
When a JackyJS event occurs a corresponding global function is executed, that is, if custom code exists for it. Most of the following event functions simply require an anonymous function as its only parameter. Further, these global event functions are typically assigned in the GAME.initGlobal()
function.
Name | Parameter | Type | Description |
---|---|---|---|
onInit | callback | Function | after all initialization code is done. |
onExecute | callback | Function | after the execute function is run, after adblock is checked, but before preload. |
onStart | callback | Function | after the first room is started and all corresponding instances are instantiated. |
onUpdate | callback | Function | after every step of the game loop, after all entities updated. |
onOrientationChange | callback | Function | after device orientation changed. |
onFocus | callback | Function | when user clicks on game. |
onBlur | callback | Function | when user clicks somewhere else on page. |
onFreeze | callback | Function | when user navigates away from page. |
onUnFreeze | callback | Function | when user navigates back to page, if previously frozen. |
onMousePress | button | String | which button was pressed and released. ('LEFT' is the only available choice for now) |
callback | Function | function to execute when mouse button is pressed and released. | |
onMouseDown | button | String | which button was held down. ('LEFT' is the only available choice for now) |
callback | Function | function to execute while mouse button is held down. | |
onMouseUp | button | String | which button was released. ('LEFT' is the only available choice for now) |
callback | Function | function to execute when mouse button is released. | |
onKeyPress | key | String | which key was pressed and released. (see Input class for key codes) |
callback | Function | function to execute when key was pressed and released. | |
onKeyDown | key | String | which key was held down. (see Input class for key codes) |
callback | Function | function to execute while key is held down. |
Inits
This is where all your custom code goes. Basically, you are passing an anonymous function as the only parameter to all of these functions. All of this code should reside in your own Javascript file, which is to be included after the JackyJS min file.
Name | Parameter | Type | Description |
---|---|---|---|
initGlobal | callback | Function | where all your global event functions go. |
initCustom | callback | Function | where all your global custom constants, variables, and functions are defined. |
* initPreload | callback | Function | where all your assets are placed for preloading. |
* initAssets | callback | Function | where all your assets are initialized for later use in entities. |
* initEntities | callback | Function | where all your entities (Sprite, Background, Text, etc) are initialized and events assigned (onCreate, onUpdate, onCollision, etc). |
initEntitiesChildren | callback | Function | where all your entity children are initialized and events assigned. JackyJS supports inheritance of entity properties and events, as well as overriding parent behaviors. |
initInterface | callback | Function | where all your button, Dialog, and Text objects can go. This is entirely optional and is available only as an organizational construct; ie, you can place this code in the GAME.initEntities() function if you like, especially if your game has minimal code. |
initParticles | callback | Function | where all your Particle parts, emitters, and systems should go. |
initPaths | callback | Function | where all your Paths are initialized and events assigned. |
* initRooms | callback | Function | where all your Rooms are initialized and events assigned. |
initTimelines | callback | Function | where all your Timelines are initialized and events assigned. |
initTransitions | callback | Function | where all your Room Transitions are initialized and events assigned. |