Class: Sprite

gameobjects~Sprite(config)

Renders a spritesheet with multiple frames as animation.

Constructor

new Sprite(config)

Parameters:
Name Type Description
config object A config object that sets some basic properties.
Properties
Name Type Attributes Default Description
x number <optional>
0 The game object's mid x position.
y number <optional>
0 The game object's mid y position.
name string The name of the image asset to use.
frame number <optional>
0 Which frame to render at the start. Zero indexed.
width number <optional>
32 The width of a single frame.
height number <optional>
32 The height of a single frame.
Source:
Fires:

Extends

Members

animation :string

The name if the current aniamtion which is playing. To start an animation, you simple set this property to the name of the desired animation. To stop the current animation and freeze on the current frame, you set this property to `undefined`.
Type:
  • string
Source:

animations :object

A dictionary with all the animations that have been added to this sprite.
Type:
  • object
Source:

body :module:physics~Body

A physics body. Use addBody to attach a body to this game object.
Type:
Overrides:
Default Value:
  • undefined
Source:

fillStyle :string

A color to use as fill, when the game object requires a fill.
Type:
  • string
Overrides:
Default Value:
  • #000000
Source:

frame :number

The index of the frame to render. Zero indexed.
Type:
  • number
Source:

frames :Array.<object>

Internal data about the x and y positions of individual frames in the image asset.
Type:
  • Array.<object>
Source:

height :number

The game object's height.
Type:
  • number
Overrides:
Source:

lifespan :number

How long this game object will remain on the scene in millieseconds. Use this for things like particles to let the engine remove them automatically. When it is undefined the game object will remain on the scene until it is removed manually or until the scene shuts down.
Type:
  • number
Overrides:
Default Value:
  • undefined
Source:

listeners :object

A dictionary of the event callbacks attached to this object.
Type:
  • object
Overrides:
Source:

name :string

The name of the image asset to use. You can change this to another asset if it has the same layout of frames.
Type:
  • string
Source:

offsetX :number

How much to displace this game object's x position in relation to the body's x position.
Type:
  • number
Overrides:
Default Value:
  • 0
Source:

offsetY :number

How much to displace this game object's y position in relation to the body's y position.
Type:
  • number
Overrides:
Default Value:
  • 0
Source:

scene :module:core~Scene

A reference to the scene this game object is attached to.
Type:
Overrides:
Source:

scrollFactorX :number

A number between 0 and 1. How much the game object is influenced by camera movement along the x axis. 1 means the game object keeps it's position in the scene. 0 means it is static in the viewport.
Type:
  • number
Overrides:
Default Value:
  • 1
Source:

scrollFactorY :number

A number between 0 and 1. How much the game object is influenced by camera movement along the y axis. 1 means the game object keeps it's position in the scene. 0 means it is static in the viewport.
Type:
  • number
Overrides:
Default Value:
  • 1
Source:

visible :boolean

Wether or not this game object will be rendered.
Type:
  • boolean
Overrides:
Default Value:
  • true
Source:

width :number

The game object's width.
Type:
  • number
Overrides:
Source:

x :number

The game object's mid x position.
Type:
  • number
Overrides:
Source:

y :number

The game object's mid y position.
Type:
  • number
Overrides:
Source:

Methods

addAnimation(config)

Adds animation and frame data to this sprite.
Parameters:
Name Type Description
config object A config describing the animation data.
Properties
Name Type Attributes Default Description
name string A unique name that references the animation. So it can later be triggered.
frames Array.<number> <optional>
[0] An array with a sequence of frame numbers.
fps number <optional>
12 The framerate of the animation in frames per second.
loop number <optional>
-1 How many times to loop the animation. -1 is loop forever. 0 is don't loop (play it just once). 1 is loop it once (playing it twice), 2 is loop it twice (playing it 3 times) and so on...
Source:

addBody(body) → {module:gameobjects~GameObject}

Adds a physics body to this game object and sets the position, width and height of the physics body to this game object's position, width and height. And returns this game object.
Parameters:
Name Type Description
body module:physics~Body An instance of a physics body
Overrides:
Source:
Returns:
Type
module:gameobjects~GameObject

destroy()

Automatically called when the scene shuts down to clean up events and references. When you override this method, use `super.destroy()`
Overrides:
Source:

draw(context)

Calculates which frame to use and blits it onto the game canvas.
Parameters:
Name Type Description
context CanvasRenderingContext2D The translated game canvas context.
Overrides:
Source:

emit(type, eopt)

Emits the event of a certain type and executes all callbacks.
Parameters:
Name Type Attributes Description
type string The name of the event it emits.
e object <optional>
An optional object that will be passed to the callbacks first argument.
Overrides:
Source:

off(typeopt, callbackopt)

Removes listeners from this object.
Parameters:
Name Type Attributes Description
type string <optional>
The name of the event to remove callback(s) for. If left empty, it will remove every single callback from this object.
callback function <optional>
The callback to remove. If left empty, it will remove all callbacks for the given type or when the type is empty every single callback on this object.
Overrides:
Source:

on(type, callback)

Continually listen on this object for an event.
Parameters:
Name Type Description
type string The name of the event.
callback function The function to execute each time the event is emitted.
Overrides:
Source:

once(type, callback)

Listen on this object for an event, but execute the callback only once.
Parameters:
Name Type Description
type string The name of the event.
callback function The function to execute once.
Overrides:
Source:

render(context, offset)

Called automatically every frame. Translates the canvas 2d context to the position of this game object and calls this game object's draw method. And then restores the context.
Parameters:
Name Type Description
context CanvasRenderingContext2D The canvas render context.
offset object The scene's offset as manipulated by the camera.
Overrides:
Source:

tickAnimation()

Internal function that calculates the frame number for the draw method.
Source:

Events

animationstopped

Notifies when the current animation has stopped.
Source: