gameify. Sprite

Creates a scene in the game. (Eg. a menu or level)

Constructor

new Sprite(x, y, image)

Parameters:
NameTypeDescription
xNumber

The x (horizontal) position of the sprite, left-to-right.

yNumber

The y (vertical) position of the sprite, top-to-bottom.

imagegameify.Image

The image the sprite should have.

Example
// ...

// Create a sprite with the image "player.png" in the top left corner
let mySprite = new gameify.Sprite(0, 0, "player.png");
// Add the sprite to the screen
myScreen.add(Sprite);

myScene.onUpdate(() => {
    // update the sprite
    mySprite.update();
});
myScene.onDraw(() => {
    myScene.clear(); // clear the screen
    mySprite.draw(); // draw the sprite
});

Members

animator :gameify.Animator

The animator for this sprite.

getSize

Get the size of the sprite

image

The Sprite's image / texture

parent

The parent screen (not used directly)

position :gameify.Vector2d

The position of the Sprite on the screen

rotation

The sprite's rotation, in degrees

scale

The scale of the sprite's texture

shape :shapes.Shape

The sprite's shape, for collision, etc.

Type:
  • shapes.Shape

shapeOffset :gameify.Vector2d

The sprite's shape offset (to align it properly)

velocity :gameify.Vector2d

The velocity of the Sprite

Methods

draw()

Draw the Sprite

faceTowards(point, offsetopt)

Have the sprite rotate to face towards a point

Parameters:
NameTypeAttributesDescription
pointgameify.Vector2d

The point to face towards

offsetNumber<optional>

Rotational offset, in degrees

fromJSON(data, ref) → {gameify.Sprite}

Creates a object from JSON data

Parameters:
NameTypeDescription
dataObject | Array

Serialized object data (from object.toJSON)

reffunction

A function that returns a name for other objects, so they can be restored later

Returns:
Type: 
gameify.Sprite

getParent() → {gameify.Screen}

Get the screen this sprite draws to

Returns:
Type: 
gameify.Screen

goTowards(pos, speedopt)

Have the sprite move towards a point.

Parameters:
NameTypeAttributesDescription
posgameify.Vector2d

The point to move towards

speedNumber<optional>

How quickly the sprite should move towards the point. If speed isn't specified, it keeps its current speed.

onDraw(callback)

Set the draw function for this sprite

Parameters:
NameTypeDescription
callbackfunction

The function to be called right before the sprite is drawn

onUpdate(callback)

Run a function when this sprite updates

Parameters:
NameTypeDescription
callbackfunction

The function to be run when the sprite updates. An optional argument can be included for a delta since the last update, and another for a reference to the sprite

(package) setContext()

Set the Canvas context to draw to. This should be called whenever a sprite is added to a Screen

setImage(newImage)

Change the Sprite's image / texture

Parameters:
NameTypeDescription
newImagegameify.Image

The image to change the sprite to

setShape(shape, offsetopt)

Set a shape for collisions. If no offset is provided, the shape's position is used.

Parameters:
NameTypeAttributesDescription
shapegameify.shapes.Shape

The shape (This shape is NOT copied, and it's position will be modified)

offsetgameify.Vector2d<optional>

The shape's offset (to align it properly)

setShape(shape, offsetxopt, offsetyopt)

Set a shape for collisions. If no offset is provided, the shape's position is used.

Parameters:
NameTypeAttributesDescription
shapegameify.shapes.Shape

The shape (This shape is NOT copied, and it's position will be modified)

offsetxNumber<optional>

The shape's offset x (to align it properly)

offsetyNumber<optional>

The shape's offset y

toJSON(keyopt, ref) → {Object}

Convert the object to JSON

Parameters:
NameTypeAttributesDescription
keystring<optional>

Key object is stored under (unused, here for consistency with e.g. Date.toJSON, etc.)

reffunction

A function that returns a name for other objects, so they can be restored later

Returns:
Type: 
Object

update(deltaopt, updateAnimatoropt)

Update the Sprite

Parameters:
NameTypeAttributesDescription
deltaNumber<optional>

The time, in miliseconds, since the last frame

updateAnimatorBoolean<optional>

Whether or not to update the animator