Class AnimatedSprite
java.lang.Object
Sprite
AnimatedSprite
Animated Sprite class - useful to have Sprites move around
Designed to be used with Spritesheets & JSON Array files from TexturePacker software:
https://free-tex-packer.com/app/
Inspired by Daniel Shiffman's p5js Animated Sprite tutorial: https://youtu.be/3noMeuufLZY
-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionAnimatedSprite
(processing.core.PApplet p, String png, String json) Constructor #3 taking in images and json onlyAnimatedSprite
(processing.core.PApplet p, String png, String json, float x, float y) Constructor #2: animations + starting coordinatesAnimatedSprite
(processing.core.PApplet p, String png, String json, float x, float y, float aSpeed) Constructor #1 for AnimatedSprite with Spritesheet (Must use the TexturePacker to make the JSON) -
Method Summary
Modifier and TypeMethodDescriptionvoid
animate()
Cycles through the images of the animated spritevoid
animate
(float animationSpeed) Cycles through the images of the animated sprite & reset a new animation speedvoid
animateHorizontal
(float horizontalSpeed, float animationSpeed, boolean wraparound) Makes the AnimatedSprite move to the right-leftvoid
animateMove
(float hSpeed, float vSpeed, boolean wraparound) Makes animated sprite move in any straight linevoid
animateMove
(float hSpeed, float vSpeed, float animationSpeed, boolean wraparound) Makes animated sprite move in any straight line + sets animation speedvoid
animateToPlayer
(AnimatedSprite otherAS, float animationSpeed, boolean wraparound) Makes the AnimatedSprite move in the direction of another AnimatedSpritevoid
animateVertical
(float verticalSpeed, float animationSpeed, boolean wraparound) Makes the AnimatedSprite move up-downCopies an AnimatedSpritecopyTo
(float x, float y) Copies an AnimatedSprite to a specific locationcopyTo
(float x, float y, float aSpeed) Copies an AnimatedSprite to a specific location with a new speedAccessor methodvoid
resize
(int w, int h) Resizes the animated sprite images to different dimensionsvoid
setAnimationSpeed
(float animationSpeed) Mutator methodvoid
setSpeed
(float animationSpeed) Sets the speed of how fast the frames cyclevoid
show()
Overriden method Displays the correct frame of the Sprite image on the screenMethods inherited from class Sprite
copy, doesCollideBottom, equals, getAccelerationX, getAccelerationY, getCenterX, getCenterY, getH, getImage, getImagePath, getIsAnimated, getName, getSpeedX, getSpeedY, getW, getX, getY, isOverlapping, isSolid, isTouchingBottom, isTouchingBottom, isTouchingLeft, isTouchingLeft, isTouchingRight, isTouchingRight, isTouchingTop, isTouchingTop, jump, jump, jump, move, moveTo, rotate, setAccelerationX, setAccelerationY, setCenterX, setCenterY, setH, setImage, setIsAnimated, setName, setSolid, setSpeed, setSpeedX, setSpeedY, setW, startGravity, startGravity, stopGravity, toString, update, update
-
Constructor Details
-
AnimatedSprite
public AnimatedSprite(processing.core.PApplet p, String png, String json, float x, float y, float aSpeed) Constructor #1 for AnimatedSprite with Spritesheet (Must use the TexturePacker to make the JSON)- Parameters:
p
- Processing appletpng
- spritesheet file generated from TexturePacker (See instructions for how to generate)json
- json data file generated from TexturePacker (See instructions for how to generate)x
- sets the initial LEFT edge of the Spritey
- sets the initial TOP edge of the SpriteaSpeed
- how quickly the animations will cycle
-
AnimatedSprite
Constructor #2: animations + starting coordinates- Parameters:
p
- Processing appletpng
- spritesheet file generated from TexturePacker (See instructions for how to generate)json
- json data file generated from TexturePacker (See instructions for how to generate)x
- sets the initial LEFT edge of the Spritey
- sets the initial TOP edge of the Sprite
-
AnimatedSprite
Constructor #3 taking in images and json only- Parameters:
p
- Processing appletpng
- spritesheet file generated from TexturePacker (See instructions for how to generate)json
- json data file generated from TexturePacker (See instructions for how to generate)
-
-
Method Details
-
show
public void show()Overriden method Displays the correct frame of the Sprite image on the screen -
setSpeed
public void setSpeed(float animationSpeed) Sets the speed of how fast the frames cycle- Parameters:
animationSpeed
-
-
animate
public void animate(float animationSpeed) Cycles through the images of the animated sprite & reset a new animation speed- Parameters:
animationSpeed
-
-
animate
public void animate()Cycles through the images of the animated sprite -
animateMove
public void animateMove(float hSpeed, float vSpeed, float animationSpeed, boolean wraparound) Makes animated sprite move in any straight line + sets animation speed- Parameters:
hSpeed
- horizontal speed of the SpritevSpeed
- vertical speed of the SpriteanimationSpeed
- how quickly the animations will cyclewraparound
- true if animated sprite will appear on the other side of the screen after travelling off the screen
-
animateMove
public void animateMove(float hSpeed, float vSpeed, boolean wraparound) Makes animated sprite move in any straight line- Parameters:
hSpeed
- horizontal speed of the SpritevSpeed
- vertical speed of the Spritewraparound
- true if animated sprite will appear on the other side of the screen after travelling off the screen
-
animateHorizontal
public void animateHorizontal(float horizontalSpeed, float animationSpeed, boolean wraparound) Makes the AnimatedSprite move to the right-left- Parameters:
horizontalSpeed
- horizontal speed of the SpriteanimationSpeed
- how quickly the animations will cyclewraparound
- true if animated sprite will appear on the other side of the screen after travelling off the screen
-
animateVertical
public void animateVertical(float verticalSpeed, float animationSpeed, boolean wraparound) Makes the AnimatedSprite move up-down- Parameters:
verticalSpeed
- vertical speed of the SpriteanimationSpeed
- how quickly the animations will cyclewraparound
- true if animated sprite will appear on the other side of the screen after travelling off the screen
-
animateToPlayer
Makes the AnimatedSprite move in the direction of another AnimatedSprite- Parameters:
otherAS
- another AnimatedSpriteanimationSpeed
- how quickly the animations will cyclewraparound
- true if animated sprite will appear on the other side of the screen after travelling off the screen
-
getJsonFile
Accessor method- Returns:
- String the JSON file's path
-
setAnimationSpeed
public void setAnimationSpeed(float animationSpeed) Mutator method- Parameters:
animationSpeed
- how quickly the animations will cycle
-
resize
public void resize(int w, int h) Resizes the animated sprite images to different dimensions -
copySprite
Copies an AnimatedSprite- Returns:
- AnimatedSprite new copy of this AnimatedSprite
-
copyTo
Copies an AnimatedSprite to a specific location -
copyTo
Copies an AnimatedSprite to a specific location with a new speed- Parameters:
x
- the LEFT edge of the copied ASpritey
- the TOP edge of the copied ASpriteaSpeed
- how quickly the animations will cycle- Returns:
- AnimatedSprite new copy of this AnimatedSprite
-