Graphic.AnimatedSprite Class
The AnimatedSprite allows you to create an object which can be animated using a SpriteSheet.
When you instanciate a new AnimatedSprite instance, you have to pass it the SpriteSheet which it will have to use.
The spritesheet represent the configuration of all animations and how to play them.
An AnimatedSprite
instance is an object which can be displayed, and can play animations.
several AnimatedSprite with the same animation should use the same shared SpriteSheet.
An animation can be started with play()
and stopped with stop()
. For managing animations,
you can assign a callback when an animation is ended:
sprite.play("running", false, function(loop, anim, sprite, status) {
if (status == "END:STOP") {
console.log("Stop running, i'll walk.");
sprite.play("walking", true);
}
});
Constructor
Graphic.AnimatedSprite
-
params
Parameters:
-
params
Objectparams is given to GraphicObject constructor.
-
spriteSheet
SpriteSheetit is a SpriteSheet object which contains one or severals animation which can be used by the current AnimatedSprite object.
-
Item Index
Methods
Properties
Methods
_setCenterPointByHotPoint
-
currentAnim
This method is private and associate to the animated sprite the hotpoint
Parameters:
-
currentAnim
Objectcurrent animation of the Animated Sprite.
draw
-
context
This method allow you to draw an animated sprite on a context.
Parameters:
-
context
Objectthis parameter must be a valid canvas context, otherwise the behavior of the draw method is unspecified.
Returns:
getCurrentAnim
()
Object
The getCurrentAnim returns the current animation which is currently played.
Returns:
isPlaying
()
Boolean
The isPlaying method allows you to test if the current AnimatedSprite object is playing or not an animation.
Returns:
notifyParentChange
()
protected
This method notify the parent that a change has been done, and that it should clear his cache.
pause
()
The pause method allows you to pause the current animation until the resume method is called.
play
-
name
-
loop
-
callback
the play method allows you to start an animation (note that this animation must be defined inside of the SpriteSheet object currently used by the AnimatedSprite).
Parameters:
-
name
Stringstring parameter which represents the name of the animation to start
-
loop
Booleanboolean parameter which set looping the animation if set to true. Otherwise, looping is disabled.
-
callback
Functionfunction which is called each time the animation reach it's end. Callback take one parameter which is an object which contains the following parameters :
-
loop
Booleana boolean which represent the loop status of the animation, if loop true then the animation will loop when its reach its end.
-
anim
Stringa string which represent the name of the animation which reach end.
-
sprite
AnimatedSpritea reference to the animated sprite which called the callback
-
status
Stringa string which represent the status of the callback, status can have the following values :
"END:LOOP"
the AnimatedSprite will loop"END:STOP"
the AnimatedSprite is now stopped,"PAUSE"
the AnimatedSprite is now paused"RESUME"
the animated sprite is now resumed
-
resume
()
The resume method allows you to resume the current animation if it has been pause before.
rotate
-
angle
This method allow you to rotate the Graphical object around the center point of the GraphicalObject.
Parameters:
-
angle
Numberrepresent the angle of rotation, it's expressed in degree.
scale
-
x
-
y
this method allow you to do a scale on the GraphicObject.
Parameters:
-
x
Numberthis is the x scale factor
-
y
Numberthis is the y scale factor
setAttr
-
attrs
Setter availlable for updating attibuts and correctly clear the caches. You can set all attributes supported by this instance (see the GraphicObject constructor for common available properties)
Parameters:
-
attrs
ObjectGraphicObject attributs. See the constructor for more details.
Example:
object.setAttr({
width: 20,
height: 20
});
object.setAttr({
pos: {
x: 0,
y: 0
}
});
skew
-
a
-
b
This method allow you to do a skew transform on the GraphicObject.
Parameters:
-
a
Numberthe factor of skew on the y axis
-
b
Numberthe factor of skew on the x axis
stop
()
The stop method allows you to stop and then rewind the current animation.
translate
-
x
-
y
This method allow you to translate the GraphicalObject, Internally this method modify the GraphicObject's matrix.
Parameters:
-
x
Numberthis is the translation scalar of the x axis.
-
y
Numberthis is the translation scalar of the y axis.
update
()
Boolean
The update method is called each frame by the gameloop.
Returns:
Properties
alpha
Number
value of opacity, between 0.0 (fully transparent) and 1.0 (opaque)
centerPoint
Object
default center of all matrix transformations.
values are exprimed depending on the size of the object. 0;0 is the up-left corner.
Sub-properties:
-
x
Object -
y
Object
height
Number
matrix
Matrix
matrix applied to this object before draw.
parent
Layer
parent containing this object.
spriteSheet
SpriteSheet
a SpriteSheet object which contains one or severals animation which can be used by the current AnimatedSprite object.
width
Number
x
Number
position on X axis
y
Number
position on Y axis
zIndex
Number
zIndex, used to determine the drawing order. Hight zIndex are drawn first.