Methods
addAction(action)
Add an Action to this sprite: a special function that
will be automatically applied to the sprite over time until it is complete.
Most common actions can be created with the static methods in the ActionFactory class.
All actions added to this sprite are performed in parallel, unless enclosed by a Sequence action.
Most common actions can be created with the static methods in the ActionFactory class.
All actions added to this sprite are performed in parallel, unless enclosed by a Sequence action.
Parameters:
Name | Type | Description |
---|---|---|
action |
Action | an action to be applied to this object |
boundWithinRectangle(worldWidth, worldHeight)
Adjusts the position of this sprite
so that it remains completely contained within screen or world dimensions.
Called automatically by update if setBoundRectangle was previously called.
Called automatically by update if setBoundRectangle was previously called.
Parameters:
Name | Type | Description |
---|---|---|
worldWidth |
number | the width of the screen or world |
worldHeight |
number | the height of the screen or world |
destroy()
Remove this sprite from the group that contains it.
destroyOutsideRectangle(worldWidth, worldHeight)
Destroy this sprite if it moves completely beyond the edges of the screen or world.
Called automatically by update if setDestroyRectangle was previously called.
Called automatically by update if setDestroyRectangle was previously called.
Parameters:
Name | Type | Description |
---|---|---|
worldWidth |
number | the width of the screen or world |
worldHeight |
number | the height of the screen or world |
draw(context)
Draw the sprite on a canvas, centered at the sprite's position, in an area corresponding to the sprite's size.
Also take into account sprite's angle, whether the image should be flipped or mirrored, and the opacity of the image.
If visible is set to false, sprite will not be drawn.
Parameters:
Name | Type | Description |
---|---|---|
context |
the graphics context object associated to the game canvas |
getAngle() → {number}
Get the angle (in degrees) between this sprite and the positive x-axis.
(Angles increase in clockwise direction, since positive y-axis is down.)
(Angles increase in clockwise direction, since positive y-axis is down.)
Returns:
angle between this sprite and positive x-axis
- Type
- number
moveAtAngle(distance, angle)
Move this sprite by a given distance in a given direction.
Parameters:
Name | Type | Description |
---|---|---|
distance |
number | distance this sprite will move |
angle |
number | direction along which this sprite will move |
moveBy(dx, dy)
Change this sprite's position by the given amounts.
Parameters:
Name | Type | Description |
---|---|---|
dx |
number | value to add to the position x coordinate |
dy |
number | value to add to the position y coordinate |
moveForward(distance)
Move this sprite by a given distance along its current direction angle.
Parameters:
Name | Type | Description |
---|---|---|
distance |
number | distance this sprite will move |
overlaps(other) → {boolean}
Determine if this sprite overlaps another sprite (includes overlapping edges).
Parameters:
Name | Type | Description |
---|---|---|
other |
Sprite | sprite to check for overlap with |
Returns:
true if this sprite overlaps other sprite, false otherwise
- Type
- boolean
rotateBy(angleDegrees)
Rotate this sprite by the given amount.
Parameters:
Name | Type | Description |
---|---|---|
angleDegrees |
number | the angle (in degrees) to rotate this sprite by |
setAngle(angleDegrees)
Set the angle (in degrees) between this sprite and the positive x-axis.
(Angles increase in clockwise direction, since positive y-axis is down.)
(Angles increase in clockwise direction, since positive y-axis is down.)
Parameters:
Name | Type | Description |
---|---|---|
angleDegrees |
number | the new direction angle of this sprite |
setAnimation(animation)
Set the Animation used when drawing this sprite.
Also updates the size of the sprite to the size of an animation frame.
Animation object will be automatically updated and used when drawing sprite.
Also updates the size of the sprite to the size of an animation frame.
Animation object will be automatically updated and used when drawing sprite.
Parameters:
Name | Type | Description |
---|---|---|
animation |
Animation | the animation to use when drawing this sprite |
setBoundRectangle(width, height)
Set world dimensions (width and height) to be used to bound sprite position within the world.
Calling this function will cause boundWithinRectangle to be called automatically by the update function.
Calling this function will cause boundWithinRectangle to be called automatically by the update function.
Parameters:
Name | Type | Description |
---|---|---|
width |
number | the width of the screen or world |
height |
number | the height of the screen or world |
setDestroyRectangle(width, height)
Set world dimensions (width and height) to be used to destroy sprite if it moves beyond world edges.
Calling this function will cause destroyOutsideRectangle to be called automatically by the update function.
Calling this function will cause destroyOutsideRectangle to be called automatically by the update function.
Parameters:
Name | Type | Description |
---|---|---|
width |
number | the width of the screen or world |
height |
number | the height of the screen or world |
setOpacity(opacity)
Change the opacity when drawing,
enabling objects underneath to be partially visible
by blending their colors with the colors of this object.
0 = fully transparent (appears invisible); 1 = fully opaque (appears solid)
0 = fully transparent (appears invisible); 1 = fully opaque (appears solid)
Parameters:
Name | Type | Description |
---|---|---|
opacity |
number | opacity of this object |
setPhysics(accValue, maxSpeed, decValue)
Initialize Physics data for this sprite and link to position.
Physics object will be automatically updated and used to control position.
Physics object will be automatically updated and used to control position.
Parameters:
Name | Type | Description |
---|---|---|
accValue |
number | default magnitude of acceleration when using accelerateAtAngle |
maxSpeed |
number | maximum speed: if speed is ever above this amount, it will be reduced to this amount |
decValue |
number | when not accelerating, object will decelerate (decrease speed) by this amount |
setPosition(x, y)
Set the x and y coordinates of the center of this sprite.
Parameters:
Name | Type | Description |
---|---|---|
x |
number | the new x coordinate of the center of this sprite |
y |
number | the new y coordinate of the center of this sprite |
setSize(width, height)
Set the size (rectangle width and height) of the sprite;
used when drawing sprite and checking for overlap with other sprites.
Parameters:
Name | Type | Description |
---|---|---|
width |
number | the new width of this sprite |
height |
number | the new height of this sprite |
setTexture(texture)
Set the texture used when drawing this sprite.
Also updates the size of the sprite to the size of the texture.
Parameters:
Name | Type | Description |
---|---|---|
texture |
Texture | the texture to use when drawing this sprite |
setVisible(visible)
Set whether this sprite should be visible or not;
determines whether sprite will be drawn on canvas.
Parameters:
Name | Type | Description |
---|---|---|
visible |
boolean | determines if this sprite is visible or not |
setWrapRectangle(width, height)
Set world dimensions (width and height) to be used to wrap sprite around world when moving beyond screen edges.
Calling this function will cause wrapAroundRectangle to be called automatically by the update function.
Calling this function will cause wrapAroundRectangle to be called automatically by the update function.
Parameters:
Name | Type | Description |
---|---|---|
width |
number | the width of the screen or world |
height |
number | the height of the screen or world |
update(deltaTime)
Perform any internal actions that should be repeated every frame.
Parameters:
Name | Type | Description |
---|---|---|
deltaTime |
number | time elapsed since previous frame |
wrapAroundRectangle(worldWidth, worldHeight)
If this sprite moves completely beyond an edge of the screen or world,
adjust its position to the opposite side.
Called automatically by update if setWrapRectangle was previously called.
Called automatically by update if setWrapRectangle was previously called.
Parameters:
Name | Type | Description |
---|---|---|
worldWidth |
number | the width of the screen or world |
worldHeight |
number | the height of the screen or world |