Retro.js

A library for making 2D space shooters

Retro.js Game Sprite


Retro.js

Retro.js contains one function called init.

Type Name Description Return
Property GAME This becomes the game board from which everything will be stored. N/A
Function init Call this to create a game board and an instance of Retro.js A game instance

Game

Type Name Description Return
Property xSize The tables size on the x-axis N/A
Property ySize The tables size on the y-axis N/A
Property board The canvas instance on which everything will be placed on. N/A
Property player The player sprite. There can only be one player so far N/A
Property context A html canvas's context
Property refreshFrequency How often the game board will refresh. Increase for more fluid game play. N/A
Property SPRITES A hash table containing all sprite instance on the screen. The unique id is the key and the sprite object is the value N/A
Property scoreText What will appear on the top right hand corner of the screen N/A
Property positions A hash table containing the locations of all the sprite instances on the screen. The unique id is the key and an array that cointains [xPos, yPos] of the sprite N/A
Property isActive Refreshing will not occur while false. Should not be played with by the user. Will cause bugs N/A
Property LEFT An integer that represents the key that will be used for the left player control button N/A
Property RIGHT An integer that represents the key that will be used for the right player control button N/A
Property UP An integer that represents the key that will be used for the up player control button N/A
Property DOWN An integer that represents the key that will be used for the down player control button N/A
Property SHOOT An integer that represents the key that will be used for the shooting control button N/A
Function start Sets the game board up and acts as the initializer. This is done automatically when creating a Retro.js instance and needs not be called by the user Void
Function update Calls reDraw several times a second. Frequency is determined by refreshFrequency. Allows the game to be dynamic Void
Function(styleName, styleValue) setStyle Given a style property name and a value, this function will change the game board. Ex. setStyle("backgroundColor", "blue") Void
Function(x, y) setSize Given an x and y as arguments, setSize will set the game board to those coordinates. Ex. setSize(1000,1000) Void
Function reDraw Renders every sprite in the game once, while discovering any collisions that may have occurred. Only useful if called frequently as is done in update Void
Function(path, xPos, yPos, width, height) createPlayer Calls addSprite, with special parameters to streamline player creation. Can only be called as long as a player is not already in use Player Sprite instance
Function(path, id, xPos, yPos, width, height) addSprite Creates a sprite, given a image path, pair of starting coordinates and dimensions, will create a new sprite on the screen Sprite instance
Function(sprite) detectCollision Determines if a sprite has collided with another. If so and a collisionBehavior is defined, then it will execute it. This should be called several times a second to be effective Void

Sprite

All visuals with exception of score are a Sprite object including characters and projectiles

Type Name Description Return
Property sprite Becomes equal to an Image() instance on objects intialization. Changing this after will change the sprite. Access directly to create animations and physical charater changes N/A
Property xPos The x-axis position of the sprite. To be manipulated by the move function N/A
Property yPos The y-axis position of the sprite. To be manipulated by the move function N/A
Property isStopped If true, this sprite cannot be moved N/A
Property speed The speed of the sprite is to be determined by this property. Change after initializing to implement things like speed power ups N/A
Property collider If not null, will contain the id of the last sprite to touch it N/A
Property projectilePath A url containing the location of the projectile that this sprite can launch. Change after initializing to create projectile sprite changes from things like power ups N/A
Property firedTotal This is the total number of projectiles fired by the sprite instance. Used to generate sprite ids and possibly useful metrics N/A
Property creator Applied to all sprites and indicates which sprite created it. Typically used by projectiles N/A
Property projectileSpeed How fast this sprite instance projectile travels. Modifies the speed property for the future projectile sprites that will be created N/A
Property projectileDirection Allows 4 directions to be used. Setting this to "up", "down", "left" or "right" will determine how a sprites projectiles will travel N/A
Property collisionDistance How close another object has to be to collide with this sprite N/A
Property game The game instance that this sprite lives in. Do not directly change this property N/A
Function (game, spritePath, xPos, yPos, width, height) start This is a constructor for this sprites instance. Needs not be called directly as it is automatically done when addSprite/createPlayer is called in game A new Sprite
Function(styleName, modifier) setStyle Sets the style of the sprite. Operates similarly to Game's version Void
Function(board, direction) move If isStopped is false, this function will move this sprite in the 4 cardinal directions Void
Function(speed, direction) shoot Creates a projectile sprite. Works only if a projectilePath is specified in the shooters properties Void
Function(projectile, direction) handleProjectile Reports if a Projectile has hit another sprite and will execute any collisionBehavior function specified in the other sprite. Will do nothing if one is not specified Void
Function destroy Deletes all object references to this sprite effectively destroying it Void
Function(intruderID) collisionBehavior This function is deliberately empty to allow user to customize their own collisionBehavior. To set your own function simply create a new one and set spriteVariable.collisionBehavior = yourFunction. It is important to accept an intruderID as an arguments or intruder.collisionBehavior cannot execute Void (recommended)