Class AnimatedSprite
java.lang.Object
com.codename1.gaming.Sprite
com.codename1.gaming.AnimatedSprite
- All Implemented Interfaces:
PhysicsLinkable
A Sprite that cycles through a sequence of frames over time.
The animation advances in #onUpdate(double), so adding it to a Scene whose
Scene#update(double) is called every frame (or calling #onUpdate(double)
directly) drives playback. By default it loops; set #setLooping(boolean) to
false to stop on the last frame.
-
Constructor Summary
ConstructorsConstructorDescriptionAnimatedSprite(SpriteSheet sheet, int[] frameIndices, double secondsPerFrame) Creates an animated sprite from frames pulled out of aSpriteSheet.AnimatedSprite(Image[] frames, double secondsPerFrame) Creates an animated sprite from an explicit array of frames. -
Method Summary
Modifier and TypeMethodDescriptionintThe index of the frame currently being shown.intdoublebooleanbooleanprotected voidonUpdate(double deltaSeconds) Per frame update hook.voidpause()Pauses playback, keeping the current frame.voidplay()Starts (or resumes) playback.voidsetCurrentFrame(int index) Jumps to the given frame index.voidsetFrameDuration(double secondsPerFrame) voidsetLooping(boolean looping) voidstop()Stops playback and rewinds to the first frame.Methods inherited from class Sprite
getAlpha, getAnchorX, getAnchorY, getBounds, getColor, getImage, getRenderHeight, getRenderWidth, getRotation, getScaleX, getScaleY, getUserData, getX, getY, getZOrder, intersects, isVisible, setAlpha, setAnchor, setColor, setImage, setPhysicsPosition, setPhysicsRotation, setPosition, setRotation, setScale, setScale, setSize, setUserData, setVisible, setX, setY, setZOrder
-
Constructor Details
-
AnimatedSprite
Creates an animated sprite from an explicit array of frames.
Parameters
-
frames: the frame images, played in order -
secondsPerFrame: how long each frame is shown, in seconds
-
-
AnimatedSprite
Creates an animated sprite from frames pulled out of a
SpriteSheet.Parameters
-
sheet: the source sprite sheet -
frameIndices: the linear frame indices to play, in order -
secondsPerFrame: how long each frame is shown, in seconds
-
-
-
Method Details
-
onUpdate
protected void onUpdate(double deltaSeconds) Description copied from class:SpritePer frame update hook. The default implementation does nothing; subclasses such asAnimatedSpriteoverride it to advance over time.Scene#update(double)invokes this for every sprite it contains. -
play
public void play()Starts (or resumes) playback. -
pause
public void pause()Pauses playback, keeping the current frame. -
stop
public void stop()Stops playback and rewinds to the first frame. -
isPlaying
public boolean isPlaying() -
isLooping
public boolean isLooping() -
setLooping
public void setLooping(boolean looping) -
getFrameDuration
public double getFrameDuration() -
setFrameDuration
public void setFrameDuration(double secondsPerFrame) -
getCurrentFrame
public int getCurrentFrame()The index of the frame currently being shown. -
setCurrentFrame
public void setCurrentFrame(int index) Jumps to the given frame index. -
getFrameCount
public int getFrameCount()
-