Class Sprite
java.lang.Object
Sprite
- Direct Known Subclasses:
AnimatedSprite
,Button
,Platform
Sprite class - to create objects that move around with their own properties
Inspired by Daniel Shiffman's p5js Animated Sprite tutorial
Note: Picture coordinate origina at top, left corner
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionSprite
(processing.core.PApplet p, int color, float posXCenter, float posYTop, float rectWidth, float rectHeight) Sprite Constructor #6: Rectangle of color Sprite, used for PlatformSprite Constructor #1: Only pass in the image file (Non-animated)Sprite Constructor #2: Only pass in the image file that can be scaled (Non-animated)Sprite Constructor #3: for Non-Animated Sprite (not working)Sprite
(processing.core.PApplet p, String spriteImgFile, float scale, float x, float y, boolean isAnimated) Sprite Constructor #4: for ANY Sprite from a file nameSprite
(processing.core.PApplet p, processing.core.PImage spriteImg, float scale, float x, float y) Sprite Constructor #5: Input a PImage directly, Used for moveable Sprites -
Method Summary
Modifier and TypeMethodDescriptioncopy()
Copies a new Sprite to same location as this SpritecopyTo
(float x, float y) Copies a Sprite to a specific locationboolean
doesCollideBottom
(Sprite otherSprite, float cushion) Checks if collision occurs at the bottom of this Spriteboolean
Checks if this Sprite is same as otherSprite (based on name or image)float
float
float
float
float
getH()
processing.core.PImage
getImage()
Accesses the Sprite's imageGets the image path of the Spriteboolean
Checks if Sprite object is animatedgetName()
Compares 2 sprites by a name, will check the image file name if no name specifiedfloat
Gets the speed of the Sprite in the X-directionfloat
Gets the speed of the Sprite in the Y-directionfloat
getW()
float
getX()
float
getY()
boolean
isOverlapping
(Sprite otherSprite) Checks if the rectangle of this Sprite overlaps in any way with the rectangle of anotherboolean
isSolid()
Checks if a Sprite is solidboolean
isTouchingBottom
(Sprite otherSprite) Checks if sprite has bumped into the bottom of another spriteboolean
isTouchingBottom
(Sprite otherSprite, float cushion) Checks if sprite has bumped into the bottom of another sprite with a specified cushion on bottomboolean
isTouchingLeft
(Sprite otherSprite) Checks if sprite has bumped into the left side of another spriteboolean
isTouchingLeft
(Sprite otherSprite, float cushion) Checks if sprite has bumped into the left side of another sprite with a specified cushion on leftboolean
isTouchingRight
(Sprite otherSprite) Checks if sprite has landed on top of another spriteboolean
isTouchingRight
(Sprite otherSprite, float cushion) Checks if sprite has bumped into the right side of another sprite with a specified cushion on rightboolean
isTouchingTop
(Sprite otherSprite) Checks if sprite has landed on top of another spriteboolean
isTouchingTop
(Sprite otherSprite, float cushion) Checks if sprite has landed on top of another sprite with a specified cushion on topvoid
jump()
Creates a jump with default jump speedvoid
jump
(float jumpSpeed) Creates a jump with a specific jumpSpeed at a pre-existing gravityStrengthvoid
jump
(float jumpSpeed, float gravityStrength) Creates a jump with a specific jumpSpeed and also sets the Sprite's gravity strengthvoid
move
(float changeX, float changeY) Moves Sprite image on the screen relative to current positionvoid
moveTo
(float x, float y) Moves Sprite image on the screen to a specific coordinatevoid
resize
(int width, int height) Scales the Sprite's image size to a new width & heightvoid
rotate
(float degrees) Rotates Sprite image on the screenvoid
setAccelerationX
(float accelX) Changes the acceleration of the Sprite in the X-directionvoid
setAccelerationY
(float accelY) Changes the acceleration of the Sprite in the Y-directionvoid
setCenterX
(float centerX) void
setCenterY
(float centerY) void
setH
(float h) void
setImage
(processing.core.PImage img) Changes the Sprite's imagevoid
setIsAnimated
(boolean isAnimated) Changes if Sprite object is animatedvoid
Sets the Sprites name to be used for comparisonsvoid
setSolid
(boolean isSolid) Changes if a Sprite is solidvoid
setSpeed
(float speedX, float speedY) Changes the speed of the Spritevoid
setSpeedX
(float speedX) void
setSpeedY
(float speedY) void
setW
(float w) void
show()
Displays the Sprite image on the screenvoid
Starts gravity acting on a Sprite at default rate or previously defined ratevoid
startGravity
(float gravityStrength) Starts gravity acting on sprite at a particular acceleration ratevoid
Stops gravity acting on a SpritetoString()
void
update()
Automatically moves the Sprite based on its velocityvoid
update
(float deltaTime) Method called to update a Sprite's position based on its set speed and acceleration
-
Field Details
-
p
public processing.core.PApplet p
-
-
Constructor Details
-
Sprite
Sprite Constructor #1: Only pass in the image file (Non-animated)- Parameters:
p
- Processing appletspriteImgFile
- filename for the non-animated sprite
-
Sprite
Sprite Constructor #2: Only pass in the image file that can be scaled (Non-animated)- Parameters:
p
- Processing appletspriteImgFile
- filename for the non-animated spritescale
- float that multiplies the size of the image to display
-
Sprite
Sprite Constructor #3: for Non-Animated Sprite (not working)- Parameters:
p
- Processing appletspriteImgFile
- filename for the non-animated spritescale
- float that multiplies the size of the image to displayx
- sets the initial LEFT edge of the Spritey
- sets the initial TOP edge of the Sprite
-
Sprite
public Sprite(processing.core.PApplet p, String spriteImgFile, float scale, float x, float y, boolean isAnimated) Sprite Constructor #4: for ANY Sprite from a file name- Parameters:
p
- Processing appletspriteImgFile
- filename for the non-animated spritescale
- float that multiplies the size of the image to displayx
- sets the initial LEFT edge of the Spritey
- sets the initial TOP edge of the SpriteisAnimated
- true if animated, false otherwise
-
Sprite
public Sprite(processing.core.PApplet p, processing.core.PImage spriteImg, float scale, float x, float y) Sprite Constructor #5: Input a PImage directly, Used for moveable Sprites- Parameters:
p
- Processing appletspriteImg
- pre-loaded PImage for Spritescale
- float that multiplies the size of the image to displayx
- sets the initial LEFT edge of the Spritey
- sets the initial TOP edge of the Sprite
-
Sprite
public Sprite(processing.core.PApplet p, int color, float posXCenter, float posYTop, float rectWidth, float rectHeight) Sprite Constructor #6: Rectangle of color Sprite, used for Platform- Parameters:
p
- Processing appletcolor
- PColor int for the color of the rectangleposXCenter
- pixel x-value of center of rectangleposYTop
- pixel y-value of top of rectanglerectWidth
- number of pixels wide of rectanglerectHeight
- number of pixels high of rectangle
-
-
Method Details
-
show
public void show()Displays the Sprite image on the screen -
moveTo
public void moveTo(float x, float y) Moves Sprite image on the screen to a specific coordinate- Parameters:
x
- moves left edge to this pixel valuey
- moves top edge to this pixel value
-
move
public void move(float changeX, float changeY) Moves Sprite image on the screen relative to current position- Parameters:
changeX
- positive moves right, negative moves leftchangeY
- positive moves down, negative moves up
-
setSpeedX
public void setSpeedX(float speedX) - Parameters:
speedX
-
-
setSpeedY
public void setSpeedY(float speedY) - Parameters:
speedY
-
-
setSpeed
public void setSpeed(float speedX, float speedY) Changes the speed of the Sprite- Parameters:
speedX
-speedY
-
-
getSpeedX
public float getSpeedX()Gets the speed of the Sprite in the X-direction- Returns:
- float
-
getSpeedY
public float getSpeedY()Gets the speed of the Sprite in the Y-direction- Returns:
- float
-
setAccelerationX
public void setAccelerationX(float accelX) Changes the acceleration of the Sprite in the X-direction- Parameters:
accelX
-
-
setAccelerationY
public void setAccelerationY(float accelY) Changes the acceleration of the Sprite in the Y-direction- Parameters:
accelY
-
-
getAccelerationY
public float getAccelerationY()- Returns:
- float acceleration of the Sprite in the Y-direction
-
getAccelerationX
public float getAccelerationX()- Returns:
- float acceleration of the Sprite in the X-direction
-
startGravity
public void startGravity()Starts gravity acting on a Sprite at default rate or previously defined rate -
startGravity
public void startGravity(float gravityStrength) Starts gravity acting on sprite at a particular acceleration rate- Parameters:
gravityStrength
- positive acceleration in Y-direction is downwards
-
stopGravity
public void stopGravity()Stops gravity acting on a Sprite -
jump
public void jump(float jumpSpeed) Creates a jump with a specific jumpSpeed at a pre-existing gravityStrength- Parameters:
jumpSpeed
-
-
jump
public void jump(float jumpSpeed, float gravityStrength) Creates a jump with a specific jumpSpeed and also sets the Sprite's gravity strength- Parameters:
jumpSpeed
-gravityStrength
-
-
jump
public void jump()Creates a jump with default jump speed -
rotate
public void rotate(float degrees) Rotates Sprite image on the screen- Parameters:
degrees
-
-
setSolid
public void setSolid(boolean isSolid) Changes if a Sprite is solid- Parameters:
isSolid
-
-
isSolid
public boolean isSolid()Checks if a Sprite is solid- Returns:
- boolean
-
isOverlapping
Checks if the rectangle of this Sprite overlaps in any way with the rectangle of another- Parameters:
otherSprite
- second Sprite to compare this Sprite with- Returns:
- boolean true if other Sprit overlaps with this, false otherwise
-
isTouchingTop
Checks if sprite has landed on top of another sprite with a specified cushion on top- Parameters:
otherSprite
- second Sprite to compare this Sprite withcushion
- number of pixels above the top before considering the top touched- Returns:
- boolean true if this Sprite is touching the top of the other Sprite, false otherwise
-
isTouchingTop
Checks if sprite has landed on top of another sprite- Parameters:
otherSprite
- second Sprite to compare this Sprite with- Returns:
- boolean true if this Sprite is touching the top of the other Sprite, false otherwise
-
isTouchingBottom
Checks if sprite has bumped into the bottom of another sprite with a specified cushion on bottom- Parameters:
otherSprite
- second Sprite to compare this Sprite withcushion
- number of pixels above the top before considering the top touched- Returns:
- boolean true if this Sprite is touching the bottom of the other Sprite, false otherwise
-
isTouchingBottom
Checks if sprite has bumped into the bottom of another sprite- Parameters:
otherSprite
- second Sprite to compare this Sprite with- Returns:
- boolean true if this Sprite is touching the bottom of the other Sprite, false otherwise
-
isTouchingRight
Checks if sprite has bumped into the right side of another sprite with a specified cushion on right- Parameters:
otherSprite
- second Sprite to compare this Sprite withcushion
- extra pixels to the right to consider the right touched- Returns:
- boolean true if this Sprite is touching the right side of the other Sprite, false otherwise
-
isTouchingRight
Checks if sprite has landed on top of another sprite- Parameters:
otherSprite
- second Sprite to compare this Sprite with- Returns:
- boolean true if this Sprite is touching the right side of the other Sprite, false otherwise
-
isTouchingLeft
Checks if sprite has bumped into the left side of another sprite with a specified cushion on left- Parameters:
otherSprite
- second Sprite to compare this Sprite withcushion
- extra pixels to the right to consider the right touched- Returns:
- boolean true if this Sprite is touching the left side of the other Sprite, false otherwise
-
isTouchingLeft
Checks if sprite has bumped into the left side of another sprite- Parameters:
otherSprite
- second Sprite to compare this Sprite with- Returns:
- boolean true if this Sprite is touching the left side of the other Sprite, false otherwise
-
doesCollideBottom
Checks if collision occurs at the bottom of this Sprite- Parameters:
otherSprite
- second Sprite to compare this Sprite withcushion
- how close do feet need to be to ground- Returns:
- boolean return true if feet are close to the ground
-
getW
public float getW()- Returns:
- float
-
getH
public float getH()- Returns:
- float
-
getCenterX
public float getCenterX()- Returns:
- float
-
getCenterY
public float getCenterY()- Returns:
- float
-
getX
public float getX()- Returns:
- float
-
getY
public float getY()- Returns:
- float
-
setW
public void setW(float w) - Parameters:
w
-
-
setH
public void setH(float h) - Parameters:
h
-
-
setCenterX
public void setCenterX(float centerX) - Parameters:
centerX
-
-
setCenterY
public void setCenterY(float centerY) - Parameters:
centerY
-
-
getImage
public processing.core.PImage getImage()Accesses the Sprite's image- Returns:
- PImage
-
setImage
public void setImage(processing.core.PImage img) Changes the Sprite's image- Parameters:
img
-
-
getIsAnimated
public boolean getIsAnimated()Checks if Sprite object is animated- Returns:
- boolean
-
setIsAnimated
public void setIsAnimated(boolean isAnimated) Changes if Sprite object is animated- Parameters:
isAnimated
-
-
getImagePath
Gets the image path of the Sprite- Returns:
- String
-
getName
Compares 2 sprites by a name, will check the image file name if no name specified- Returns:
- String
-
setName
Sets the Sprites name to be used for comparisons- Parameters:
name
-
-
copyTo
Copies a Sprite to a specific location- Parameters:
x
-y
-- Returns:
- Sprite
-
copy
Copies a new Sprite to same location as this Sprite- Returns:
- Sprite
-
resize
public void resize(int width, int height) Scales the Sprite's image size to a new width & height- Parameters:
width
- how many pixels wide the Sprite will change toheight
- how many pixels high the Sprite will change to
-
equals
Checks if this Sprite is same as otherSprite (based on name or image)- Parameters:
otherSprite
- second Sprite to compare this Sprite with- Returns:
- boolean true if both Sprites have the same name or image, false otherwise
-
update
public void update()Automatically moves the Sprite based on its velocity -
update
public void update(float deltaTime) Method called to update a Sprite's position based on its set speed and acceleration- Parameters:
deltaTime
-
-
toString
-