Constructor
new Sprite(x, y, image)
| Name | Type | Description |
|---|---|---|
x | Number | The x (horizontal) position of the sprite, left-to-right. |
y | Number | The y (vertical) position of the sprite, top-to-bottom. |
image | gameify. | The image the sprite should have. |
// ...
// 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.
- 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
| Name | Type | Attributes | Description |
|---|---|---|---|
point | gameify. | The point to face towards | |
offset | Number | <optional> | Rotational offset, in degrees |
fromJSON(data, ref) → {gameify.Sprite}
Creates a object from JSON data
| Name | Type | Description |
|---|---|---|
data | Object | | Serialized object data (from object.toJSON) |
ref | function | A function that returns a name for other objects, so they can be restored later |
- Type:
- gameify.
Sprite
getParent() → {gameify.Screen}
Get the screen this sprite draws to
- Type:
- gameify.
Screen
goTowards(pos, speedopt)
Have the sprite move towards a point.
| Name | Type | Attributes | Description |
|---|---|---|---|
pos | gameify. | The point to move towards | |
speed | Number | <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
| Name | Type | Description |
|---|---|---|
callback | function | The function to be called right before the sprite is drawn |
onUpdate(callback)
Run a function when this sprite updates
| Name | Type | Description |
|---|---|---|
callback | function | 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
| Name | Type | Description |
|---|---|---|
newImage | gameify. | 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.
| Name | Type | Attributes | Description |
|---|---|---|---|
shape | gameify. | The shape (This shape is NOT copied, and it's position will be modified) | |
offset | gameify. | <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.
| Name | Type | Attributes | Description |
|---|---|---|---|
shape | gameify. | The shape (This shape is NOT copied, and it's position will be modified) | |
offsetx | Number | <optional> | The shape's offset x (to align it properly) |
offsety | Number | <optional> | The shape's offset y |
toJSON(keyopt, ref) → {Object}
Convert the object to JSON
| Name | Type | Attributes | Description |
|---|---|---|---|
key | string | <optional> | Key object is stored under (unused, here for consistency with e.g. Date.toJSON, etc.) |
ref | function | A function that returns a name for other objects, so they can be restored later |
- Type:
- Object
update(deltaopt, updateAnimatoropt)
Update the Sprite
| Name | Type | Attributes | Description |
|---|---|---|---|
delta | Number | <optional> | The time, in miliseconds, since the last frame |
updateAnimator | Boolean | <optional> | Whether or not to update the animator |