gameify. Animation

Creates an animation

Constructor

new Animation(frames, options)

Parameters:
NameTypeDescription
framesArray.<AnimationFrame>

The frames of the animation

optionsAnimationOptions

animation options

Example
// ...

// Create a sprite with the image "player.png" in the top left corner
const frames = [{
    image: { type: 'Image', value: new gameify.Image("player_idle1.png") },
    position: { type: 'Vector2d', value: new gameify.Vector2d(0, 2) },
}, // ... more frames
];
let myAnimation = new gameify.Animation(frames, { duration: 200, loop: true });

mySprite.animator.set('idle', myAnimation);

// ...

myScene.onUpdate(() => {
    if (mySprite.velocity.getMagnitude() < .1) {
        mySprite.animator.play('idle');
    } else {
        mySprite.animator.play('walking');
    }
});

Members

frames :Array.<Object>

The frames of the animation

Type:
  • Array.<Object>

options :AnimationOptions

The animation's options

(static) propertyTypes

Animation types. Most types replace the value (absolute)

Properties
NameTypeDescription
simple

Apply the property by overwriting the old value each frame

object

Apply the property using Object.apply() each frame

number

A number

string

A string

boolean

A boolean

Image

A gameify.Image (on a sprite, or other object with a setImage method)

Vector2d

A gameify.Vector2d

Methods

applyFrameTo(object, frame)

Apply an animation frame to an object

Parameters:
NameTypeDescription
objectObject

The object to apply the frame to

frameObject

The frame to apply

applyTo(object, time)

Apply an animation frame to an object

Parameters:
NameTypeDescription
objectObject

The object to apply the frame to

timeNumber

The time (in milliseconds) to of the frame

framesFromJSON(data, ref) → {Array.<AnimationFrame>}

Creates frames from their JSON representation

Parameters:
NameTypeDescription
dataArray

Serialized frame data (from object.framesToJSON)

reffunction

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

Returns:
Type: 
Array.<AnimationFrame>

framesToJSON(keyopt, ref) → {Object}

Converts this animation's frames 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

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

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.Animation

getFrameAt(time) → {Object}

Get the frame at the given time

Parameters:
NameTypeDescription
timeNumber

The time (in milliseconds) to get the frame at

Returns:
Type: 
Object

getFrameNumberAt(time) → {Number}

Get the frame number at the given time

Parameters:
NameTypeDescription
timeNumber

The time (in milliseconds) to get the frame at

Returns:
Type: 
Number

isAfterCompletion(time)

Check if a the animation is completed after a specific time If options.loop is true, always returns false

Parameters:
NameTypeDescription
timeNumber

The time (in milliseconds) to check if the animation is completed at

setFrames(frames)

Update the animation's frames

Parameters:
NameTypeDescription
framesArray.<AnimationFrame>

The new frames

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