Class Sprite

Direct Known Subclasses:
AnimatedSprite, Button, Platform

public class Sprite extends Object
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
    Modifier and Type
    Field
    Description
    processing.core.PApplet
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    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
    Sprite(processing.core.PApplet p, String spriteImgFile)
    Sprite Constructor #1: Only pass in the image file (Non-animated)
    Sprite(processing.core.PApplet p, String spriteImgFile, float scale)
    Sprite Constructor #2: Only pass in the image file that can be scaled (Non-animated)
    Sprite(processing.core.PApplet p, String spriteImgFile, float scale, float x, float y)
    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 name
    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
  • Method Summary

    Modifier and Type
    Method
    Description
    Copies a new Sprite to same location as this Sprite
    copyTo(float x, float y)
    Copies a Sprite to a specific location
    boolean
    doesCollideBottom(Sprite otherSprite, float cushion)
    Checks if collision occurs at the bottom of this Sprite
    boolean
    equals(Sprite otherSprite)
    Checks if this Sprite is same as otherSprite (based on name or image)
    float
     
    float
     
    float
     
    float
     
    float
     
    processing.core.PImage
    Accesses the Sprite's image
    Gets the image path of the Sprite
    boolean
    Checks if Sprite object is animated
    Compares 2 sprites by a name, will check the image file name if no name specified
    float
    Gets the speed of the Sprite in the X-direction
    float
    Gets the speed of the Sprite in the Y-direction
    float
     
    float
     
    float
     
    boolean
    isOverlapping(Sprite otherSprite)
    Checks if the rectangle of this Sprite overlaps in any way with the rectangle of another
    boolean
    Checks if a Sprite is solid
    boolean
    isTouchingBottom(Sprite otherSprite)
    Checks if sprite has bumped into the bottom of another sprite
    boolean
    isTouchingBottom(Sprite otherSprite, float cushion)
    Checks if sprite has bumped into the bottom of another sprite with a specified cushion on bottom
    boolean
    isTouchingLeft(Sprite otherSprite)
    Checks if sprite has bumped into the left side of another sprite
    boolean
    isTouchingLeft(Sprite otherSprite, float cushion)
    Checks if sprite has bumped into the left side of another sprite with a specified cushion on left
    boolean
    isTouchingRight(Sprite otherSprite)
    Checks if sprite has landed on top of another sprite
    boolean
    isTouchingRight(Sprite otherSprite, float cushion)
    Checks if sprite has bumped into the right side of another sprite with a specified cushion on right
    boolean
    isTouchingTop(Sprite otherSprite)
    Checks if sprite has landed on top of another sprite
    boolean
    isTouchingTop(Sprite otherSprite, float cushion)
    Checks if sprite has landed on top of another sprite with a specified cushion on top
    void
    Creates a jump with default jump speed
    void
    jump(float jumpSpeed)
    Creates a jump with a specific jumpSpeed at a pre-existing gravityStrength
    void
    jump(float jumpSpeed, float gravityStrength)
    Creates a jump with a specific jumpSpeed and also sets the Sprite's gravity strength
    void
    move(float changeX, float changeY)
    Moves Sprite image on the screen relative to current position
    void
    moveTo(float x, float y)
    Moves Sprite image on the screen to a specific coordinate
    void
    resize(int width, int height)
    Scales the Sprite's image size to a new width & height
    void
    rotate(float degrees)
    Rotates Sprite image on the screen
    void
    setAccelerationX(float accelX)
    Changes the acceleration of the Sprite in the X-direction
    void
    setAccelerationY(float accelY)
    Changes the acceleration of the Sprite in the Y-direction
    void
    setCenterX(float centerX)
     
    void
    setCenterY(float centerY)
     
    void
    setH(float h)
     
    void
    setImage(processing.core.PImage img)
    Changes the Sprite's image
    void
    setIsAnimated(boolean isAnimated)
    Changes if Sprite object is animated
    void
    Sets the Sprites name to be used for comparisons
    void
    setSolid(boolean isSolid)
    Changes if a Sprite is solid
    void
    setSpeed(float speedX, float speedY)
    Changes the speed of the Sprite
    void
    setSpeedX(float speedX)
     
    void
    setSpeedY(float speedY)
     
    void
    setW(float w)
     
    void
    Displays the Sprite image on the screen
    void
    Starts gravity acting on a Sprite at default rate or previously defined rate
    void
    startGravity(float gravityStrength)
    Starts gravity acting on sprite at a particular acceleration rate
    void
    Stops gravity acting on a Sprite
     
    void
    Automatically moves the Sprite based on its velocity
    void
    update(float deltaTime)
    Method called to update a Sprite's position based on its set speed and acceleration

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • p

      public processing.core.PApplet p
  • Constructor Details

    • Sprite

      public Sprite(processing.core.PApplet p, String spriteImgFile)
      Sprite Constructor #1: Only pass in the image file (Non-animated)
      Parameters:
      p - Processing applet
      spriteImgFile - filename for the non-animated sprite
    • Sprite

      public Sprite(processing.core.PApplet p, String spriteImgFile, float scale)
      Sprite Constructor #2: Only pass in the image file that can be scaled (Non-animated)
      Parameters:
      p - Processing applet
      spriteImgFile - filename for the non-animated sprite
      scale - float that multiplies the size of the image to display
    • Sprite

      public Sprite(processing.core.PApplet p, String spriteImgFile, float scale, float x, float y)
      Sprite Constructor #3: for Non-Animated Sprite (not working)
      Parameters:
      p - Processing applet
      spriteImgFile - filename for the non-animated sprite
      scale - float that multiplies the size of the image to display
      x - sets the initial LEFT edge of the Sprite
      y - 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 applet
      spriteImgFile - filename for the non-animated sprite
      scale - float that multiplies the size of the image to display
      x - sets the initial LEFT edge of the Sprite
      y - sets the initial TOP edge of the Sprite
      isAnimated - 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 applet
      spriteImg - pre-loaded PImage for Sprite
      scale - float that multiplies the size of the image to display
      x - sets the initial LEFT edge of the Sprite
      y - 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 applet
      color - PColor int for the color of the rectangle
      posXCenter - pixel x-value of center of rectangle
      posYTop - pixel y-value of top of rectangle
      rectWidth - number of pixels wide of rectangle
      rectHeight - 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 value
      y - 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 left
      changeY - 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

      public boolean isOverlapping(Sprite otherSprite)
      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

      public boolean isTouchingTop(Sprite otherSprite, float cushion)
      Checks if sprite has landed on top of another sprite with a specified cushion on top
      Parameters:
      otherSprite - second Sprite to compare this Sprite with
      cushion - 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

      public boolean isTouchingTop(Sprite otherSprite)
      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

      public boolean isTouchingBottom(Sprite otherSprite, float cushion)
      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 with
      cushion - 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

      public boolean isTouchingBottom(Sprite otherSprite)
      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

      public boolean isTouchingRight(Sprite otherSprite, float cushion)
      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 with
      cushion - 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

      public boolean isTouchingRight(Sprite otherSprite)
      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

      public boolean isTouchingLeft(Sprite otherSprite, float cushion)
      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 with
      cushion - 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

      public boolean isTouchingLeft(Sprite otherSprite)
      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

      public boolean doesCollideBottom(Sprite otherSprite, float cushion)
      Checks if collision occurs at the bottom of this Sprite
      Parameters:
      otherSprite - second Sprite to compare this Sprite with
      cushion - 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

      public String getImagePath()
      Gets the image path of the Sprite
      Returns:
      String
    • getName

      public String getName()
      Compares 2 sprites by a name, will check the image file name if no name specified
      Returns:
      String
    • setName

      public void setName(String name)
      Sets the Sprites name to be used for comparisons
      Parameters:
      name -
    • copyTo

      public Sprite copyTo(float x, float y)
      Copies a Sprite to a specific location
      Parameters:
      x -
      y -
      Returns:
      Sprite
    • copy

      public 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 to
      height - how many pixels high the Sprite will change to
    • equals

      public boolean equals(Sprite otherSprite)
      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

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      String includes Sprite's filename, centerX, centerY, left edge, top edge, speedX, speedY, w, h, isAnimated