Class AnimatedSprite
java.lang.Object
org.openpatch.scratch.Sprite
org.openpatch.scratch.extensions.animation.AnimatedSprite
The AnimatedSprite class represents a sprite that can play animations. It
extends the Sprite
class and provides methods to add animations, play animations, set the
interval between animation
frames, and reset the animation.
Example usage:
AnimatedSprite sprite = new AnimatedSprite();
sprite.addCostume("idle", "assets/idle.png");
sprite.addAnimation("walk", "assets/walk_%d.png", 4);
sprite.playAnimation("walk");
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class Sprite
Sprite.RunHandler, Sprite.WhenAddedToStageHandler, Sprite.WhenBackdropSwitchesHandler, Sprite.WhenClickedHandler, Sprite.WhenIReceiveHandler, Sprite.WhenKeyPressedHandler, Sprite.WhenKeyReleasedHandler, Sprite.WhenMouseClickedHandler, Sprite.WhenMouseMovedHandler, Sprite.WhenRemovedFromStageHandler -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAnimation(String name, String pattern, int frames) Adds an animation to the sprite.voidaddAnimation(String name, String path, int frames, int width, int height) Adds an animation to the sprite.voidaddAnimation(String name, String path, int frames, int width, int height, int row) Adds an animation to the sprite.voidaddAnimation(String name, String path, int frames, int width, int height, int column, boolean useColumns) Adds an animation to the sprite.voidaddAnimation(String name, Function<Integer, String> builder, int frame) Adds an animation to the sprite.clone()Creates a clone of the current sprite.intGets the current animation frame.intGets the interval between animation frames.booleanChecks if the animation is played.voidplayAnimation(String name) Plays the animation with the specified name.voidplayAnimation(String name, boolean once) Plays the animation with the specified name.voidResets the animation.voidsetAnimationFrame(int frame) Sets the current animation frame.voidsetAnimationInterval(int interval) Sets the interval between animation frames.toString()Methods inherited from class Sprite
addCostume, addCostume, addCostumes, addedToStage, addShader, addSound, addTimer, broadcast, broadcast, changeHeight, changePosition, changeSize, changeTint, changeTransparency, changeWidth, changeX, changeY, disableHitbox, disableNineSlice, distanceToMousePointer, distanceToSprite, draw, drawDebug, enableHitbox, getCurrentCostumeIndex, getCurrentCostumeName, getCurrentDay, getCurrentDayOfWeek, getCurrentHour, getCurrentMillisecond, getCurrentMinute, getCurrentMonth, getCurrentSecond, getCurrentShader, getCurrentShaderIndex, getCurrentShaderName, getCurrentYear, getDaysSince2000, getDeltaTime, getDirection, getHeight, getHitbox, getMouse, getMouseX, getMouseY, getPen, getPosition, getShader, getSize, getStage, getText, getTimer, getTimer, getTint, getTouchingSprite, getTouchingSprites, getTransparency, getWidth, getWindow, getX, getY, goLayersBackwards, goLayersForwards, goToBackLayer, goToFrontLayer, goToMousePointer, goToRandomPosition, goToSprite, hide, ifOnEdgeBounce, isKeyPressed, isMouseDown, isSoundPlaying, isTouchingEdge, isTouchingMousePointer, isTouchingSprite, isTouchingSprite, isUI, isUI, isVisible, keyEvent, mouseEvent, move, move, nextCostume, nextShader, pickRandom, playSound, pointInDirection, pointInDirection, pointTowardsMousePointer, pointTowardsSprite, previousCostume, remove, removedFromStage, removeSound, removeTimer, resetShader, run, say, say, setDirection, setDirection, setHeight, setHitbox, setHitbox, setHitbox, setHitbox, setNineSlice, setOnEdgeBounce, setPosition, setPosition, setRotationStyle, setRun, setSize, setTint, setTint, setTint, setTransparency, setWhenAddedToStageHandler, setWhenBackdropSwitches, setWhenClicked, setWhenIReceive, setWhenKeyPressed, setWhenKeyReleased, setWhenMouseClicked, setWhenMouseMoved, setWhenRemovedFromStageHandler, setWidth, setX, setY, show, stamp, stampToBackground, stampToForeground, stampToUI, stopAllSounds, stopSound, switchCostume, switchCostume, switchShader, switchShader, think, think, turnLeft, turnRight, whenAddedToStage, whenAddedToStage, whenBackdropSwitches, whenClicked, whenIReceive, whenIReceive, whenKeyPressed, whenKeyReleased, whenMouseClicked, whenMouseMoved, whenRemovedFromStage, whenRemovedFromStage
-
Constructor Details
-
AnimatedSprite
public AnimatedSprite() -
AnimatedSprite
-
-
Method Details
-
addAnimation
-
addAnimation
Adds an animation to the sprite. To add an animation with file names "walk_1.png", "walk_2.png", "walk_3.png", you can use the following code:sprite.addAnimation("walk", i -> "walk_" + i + ".png", 3);- Parameters:
name- the name of the animationbuilder- a function that builds the file names for the animation framesframe- the number of frames in the animation. The frame starts from 1.
-
addAnimation
Adds an animation to the sprite. For an animation sprite sheet, where each frame is of equal width and height, you can use this method to add an animation by specifying the path to the sprite sheet, the number of frames, and the dimensions of each frame. For example, for a four-frame animation arranged in a single row, each frame being 64x64 pixels, you can add the animation using the following code:sprite.addAnimation("walk", "assets/walk.png", 4, 64, 64);- Parameters:
name- the name of the animationpath- the path to the animation framesframes- the number of frames in the animationwidth- the width of each frameheight- the height of each frame
-
addAnimation
Adds an animation to the sprite. If the animation frames are arranged in rows in a sprite sheet, you can use this method to specify which row to use for the animation. For example, for a four-frame animation arranged in a single row, you want the second, each frame being 64x64 pixels, you can add the animation using the following code:sprite.addAnimation("walk", "assets/walk.png", 4, 64, 64, 2);- Parameters:
name- the name of the animationpath- the path to the animation framesframes- the number of frames in the animationwidth- the width of each frameheight- the height of each framerow- the row of the animation frames
-
addAnimation
public void addAnimation(String name, String path, int frames, int width, int height, int column, boolean useColumns) Adds an animation to the sprite. If the animation frames are arranged in either rows or columns in a sprite sheet, you can use this method to specify which column or row to use for the animation. Take four frame animation arranged in a single column. Each frame is 64x64 pixels. You can add the animation using the following code:sprite.addAnimation("walk", "assets/walk.png", 4, 64, 64, 1, true); // use column 1- Parameters:
name- the name of the animationpath- the path to the animation framesframes- the number of frames in the animationwidth- the width of each frameheight- the height of each framecolumn- the column of the animation framesuseColumns- whether to use columns or rows
-
playAnimation
Plays the animation with the specified name.- Parameters:
name- the name of the animation to play
-
playAnimation
Plays the animation with the specified name.- Parameters:
name- the name of the animation to playonce- whether to play the animation once
-
resetAnimation
public void resetAnimation()Resets the animation. The animation will start from the first frame. -
setAnimationInterval
public void setAnimationInterval(int interval) Sets the interval between animation frames.- Parameters:
interval- the interval between animation frames
-
getAnimationInterval
public int getAnimationInterval()Gets the interval between animation frames.- Returns:
- the interval between animation frames
-
getAnimationFrame
public int getAnimationFrame()Gets the current animation frame.- Returns:
- the current animation frame
-
setAnimationFrame
public void setAnimationFrame(int frame) Sets the current animation frame.- Parameters:
frame- the current animation frame
-
isAnimationPlayed
public boolean isAnimationPlayed()Checks if the animation is played.- Returns:
- true if the animation is played, false otherwise
-
toString
-
clone
Description copied from class:SpriteCreates a clone of the current sprite. The cloned sprite will have the same properties as the original sprite, including its costumes, position, direction, and pen.
-