Class Sprite

java.lang.Object
org.openpatch.scratch.Sprite
Direct Known Subclasses:
AnimatedSprite, UISprite

public class Sprite extends Object
The `Sprite` class represents a graphical object that can be displayed on a stage. It supports various functionalities such as costumes, sounds, movement, rotation, collision detection, and interaction with the mouse and keyboard.

Key features include:

  • Adding and switching costumes
  • Adding and playing sounds
  • Movement and rotation
  • Collision detection with other sprites and the stage edges
  • Interaction with the mouse and keyboard
  • Displaying text and thought bubbles
  • Broadcasting and receiving messages

Usage example:

 
 Sprite sprite = new Sprite();
 sprite.addCostume("costume1", "path/to/image.png");
 sprite.addSound("sound1", "path/to/sound.wav");
 sprite.setPosition(100, 200);
 sprite.move(10);
 sprite.turnRight(90);
 sprite.say("Hello, world!");
 
 

Note: This class is designed to be used within a stage, and many methods require the sprite to be added to a stage to function correctly.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Direction constant pointing down (180 degrees).
    static final double
    Direction constant pointing left (270 degrees).
    static final double
    Direction constant pointing right (90 degrees).
    static final double
    Direction constant pointing up (0 degrees).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new Sprite object with default settings.
    Sprite(String name, String imagePath)
    Constructs a new Sprite object with a specified costume.
    Copies a Sprite object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Add one of the costumes that ship with Scratch for Java to the sprite.
    void
    addCostume(String name, String imagePath)
    Add a costume to the sprite.
    void
    addCostume(String name, String spriteSheetPath, int x, int y, int width, int height)
    Adds a new costume to the sprite if a costume with the same name does not already exist.
    void
    addCostumes(String prefix, String spriteSheet, int tileWidth, int tileHeight)
    Adds all tiles from a spritesheet as costumes.
    protected void
     
    void
    Add one of the sounds that ship with Scratch for Java to the sprite.
    void
    addSound(String name, String soundPath)
    Add a sound to the sprite.
    void
    ask(String question)
    Asks a question and waits for an answer to be typed in.
    void
    broadcast(String message)
    Broadcasts a message to all sprites in the stage except the current sprite.
    void
    changePosition(double x, double y)
    Changes the position of the sprite by the given amounts.
    void
    Changes the position of the sprite by a given vector.
    void
    changeSize(double amount)
    Changes the size of the sprite by a given percentage.
    void
    changeTint(double step)
    Changes the tint for the sprite by a step.
    void
    changeTransparency(double step)
    Changes how see-through the sprite is by a step.
    void
    changeVolume(double step)
    Makes every sound of this sprite louder or quieter.
    void
    changeX(double x)
    Changes x by a certain amount
    void
    changeY(double y)
    Changes y by a certain amount
    Creates a clone of the current sprite.
    void
    debug(Object... values)
    Prints a debug message to stdout when debug mode is enabled.
    void
    Disables the hitbox for the sprite.
    protected void
    Disables the nine-slice feature for all costumes of the sprite.
    double
    Calculates the distance from the current sprite to the mouse pointer.
    double
    Calculates the Euclidean distance between this sprite and another sprite.
    protected void
    draw(processing.core.PGraphics buffer)
    Draws the sprite if it is not hidden.
    protected void
    drawDebug(processing.core.PGraphics buffer)
    Draws debug information for the sprite.
    void
    Enables the hitbox for the sprite.
    Returns the last answer that was typed in.
    int
    Returns the current costume index
    Returns the current costume name
    double
    Gets the seconds passed since the last frame.
    double
    Returns the direction of the sprite.
    int
    Return the height of the current costume or the pen size, when no costume is available.
    Returns the hitbox of the sprite based on its current costume, position, and rotation.
    Retrieves the current position of the mouse cursor.
    double
    Returns the current x-position of the mouse cursor
    double
    Returns the current y-position of the mouse cursor
    Returns the pen of the sprite.
    Retrieves the current position of the sprite.
    Returns the shaders of this sprite.
    double
    Returns the size of the sprite.
    Retrieves the current stage associated with this sprite.
    Retrieves the text associated with this sprite.
    Return the default timer
    Return a timer by name
    Retrieves the tint value of the current costume.
    <T extends Sprite>
    T
    Returns the first sprite of the specified type that is currently touching this sprite.
    <T extends Sprite>
    List<T>
    Returns a list of sprites of the specified type that are currently touching this sprite.
    double
    Gets the transparency of the current costume.
    double
    Returns how loud the sounds of this sprite play.
    int
    Return the width of the current costume or the pen size, when no costume is available.
    double
    Returns the x coordinate of the sprite
    double
    Returns the y coordinate of the sprite
    void
    glide(double seconds, double x, double y)
    Slides the sprite to a place over the given time, instead of jumping there.
    void
    goLayersBackwards(int number)
    Moves the sprite backwards by a specified number of layers in the stage.
    void
    goLayersForwards(int number)
    Moves the sprite forward by a specified number of layers within its stage.
    void
    Moves the sprite to the back layer of the stage.
    void
    Moves this sprite to the front layer of the stage.
    void
    Moves the sprite to the current position of the mouse pointer.
    void
    Moves the sprite to a random position within the boundaries of the stage.
    void
    Moves this sprite to the position of the specified sprite.
    void
    Hides the sprite.
    void
    Checks if the sprite is on the edge of the stage and bounces it back if it is.
    boolean
    Checks whether a question is on screen and still waiting for an answer.
    boolean
    Checks whether the sprite is still on its way to a place it was told to glide to.
    boolean
    Returns true if the key is pressed
    boolean
    Returns true is the mouse button is down
    boolean
    Returns true if the sound if playing
    boolean
    Returns true if the rectangle which contains the image is outside of the stage
    boolean
    Returns true is the mouse pointer is touching the hitbox of the sprite.
    boolean
    Checks if this sprite is touching any sprite of the specified class type.
    boolean
    Checks if this sprite is touching another sprite.
    protected boolean
    Checks if the sprite is part of the user interface.
    boolean
    Returns if the sprite is visible
    void
    move(double steps)
    Moves the sprite towards the current rotation by the received steps.
    void
    Moves the sprite in the direction of the given vector.
    void
    Switch to the next costume.
    int
    pickRandom(int from, int to)
    Returns a random integer between the specified range.
    void
    Plays a sound.
    void
    pointInDirection(double degrees)
    Points the sprite in the specified direction.
    void
    Points the sprite in the direction of the given vector.
    void
    Points the sprite towards the current position of the mouse pointer.
    void
    Points the current sprite towards the specified sprite.
    void
    Switch to the previous costume.
    void
    Removes this sprite from its current stage.
    protected void
     
    void
    run()
    This method is intended to be overridden by subclasses to define the behavior of the sprite when it is run.
    void
    say(String text)
    Makes the sprite display a speech bubble with the specified text.
    void
    say(String text, int millis)
    Displays a text message for a specified duration.
    void
    setDirection(double degrees)
    Sets the direction of the sprite to a given degrees.
    void
    Sets the direction of the sprite to the direction of a given vector.
    protected void
    setHeight(double height)
    * Sets the height of the sprite.
    void
    setHitbox(double... points)
    Sets the hitbox for the sprite using the provided points.
    void
    Sets the hitbox for the sprite using the specified shape.
    protected void
    setNineSlice(int top, int right, int bottom, int left)
    Set the nine-slice (also known as nine-patch) parameters for the sprite's costumes.
    void
    setPosition(double x, double y)
    Sets the position of the sprite
    void
    Sets the position of the sprite based on the coordinates of a given vector.
    void
    Sets the rotation style for the sprite.
    void
    setSize(double percentage)
    Sets the size of the sprite.
    void
    setTint(double h)
    Sets the tint for the sprite with a hue.
    void
    setTint(double r, double g, double b)
    Sets the tint for the sprite with rgb.
    void
    Sets the tint for the sprite with an color object.
    void
    setTransparency(double transparency)
    Sets how see-through the sprite is, as Scratch's ghost effect does: 0 is the solid sprite you start with and 100 is invisible.
    protected void
    setUI(boolean isUI)
    Sets the UI status of the sprite.
    void
    setVolume(double percent)
    Sets how loud every sound of this sprite plays.
    protected void
    setWidth(double width)
    * Sets the width of the sprite.
    void
    setX(double x)
    Sets the x coordinate
    void
    setY(double y)
    Sets the y coordinate
    void
    Shows the sprite.
    void
    Stamps the current sprite to the background.
    void
    stamp(Layer layer)
    Stamps the sprite onto one of the stage's layers.
    void
    Stops the playing of all sounds of the sprite.
    void
    Stops the playing of the sound with the given name
    void
    switchCostume(double index)
    Switches the current costume of the sprite to the costume at the specified index.
    void
    Switch to a costume by name.
    void
    think(String text)
    Displays a thought bubble with the specified text.
    void
    think(String text, int millis)
    Displays a thought bubble with the specified text for a given duration.
    void
    turnLeft(double degrees)
    Rotates the sprite by a certain degrees to the left.
    void
    turnRight(double degrees)
    Rotates the sprite by a certain degrees to the right.
    void
    This method is called when the sprite is added to the stage.
    void
    This method is called when the backdrop switches to the specified name.
    void
    This method is called when the sprite is clicked.
    void
    This method is called when a message is received.
    void
    This method is called when a key is pressed.
    void
    This method is called when a key is released.
    void
    This method is called when a mouse click event occurs.
    void
    whenMouseMoved(double x, double y)
    This method is called when the mouse is moved.
    void
    This method is called when the sprite is removed from the stage.

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • Sprite

      public Sprite()
      Constructs a new Sprite object with default settings.
    • Sprite

      public Sprite(String name, String imagePath)
      Constructs a new Sprite object with a specified costume.
      Parameters:
      name - a unique name for the costume
      imagePath - a path to an image file, or the name of a built-in sprite such as "slimeGreen"
    • Sprite

      public Sprite(Sprite s)
      Copies a Sprite object.
      Parameters:
      s - a Sprite object to copy
  • Method Details

    • getShaders

      public Shaders getShaders()
      Returns the shaders of this sprite. Shader handling lives behind this one method so that it does not crowd the everyday API.

      Example usage:

      
       this.getShaders().add("blur", "blur.frag", null);
       this.getShaders().switchTo("blur");
       
      Returns:
      the shaders
    • whenAddedToStage

      public void whenAddedToStage()
      This method is called when the sprite is added to the stage. Override this method to define custom behavior when the sprite is added to the stage.
    • whenRemovedFromStage

      public void whenRemovedFromStage()
      This method is called when the sprite is removed from the stage. Override this method to define custom behavior when the sprite is removed.
    • remove

      public void remove()
      Removes this sprite from its current stage.
      Scratch Block:
      delete this clone
    • debug

      public void debug(Object... values)
      Prints a debug message to stdout when debug mode is enabled. The message is prefixed with the sprite's class name so you can tell which sprite it came from.

      Example:

      
       this.debug("x =", getX(), "y =", getY());
       // prints: [CatSprite] x = 100.0 y = 200.0
       
      Parameters:
      values - one or more values to print
    • getStage

      public Stage getStage()
      Retrieves the current stage associated with this sprite.
      Returns:
      the stage associated with this sprite
    • addCostume

      public void addCostume(String name)
      Add one of the costumes that ship with Scratch for Java to the sprite. The costume gets the same name as the built-in sprite. If a costume with that name already exists do nothing.

      Example usage:

      
       this.addCostume("bunny1_jump");
       
      Parameters:
      name - the name of a built-in sprite, for example "bunny1_jump". Add the sheet in front of the name, for example "platformer/grass", if the same name exists on several sheets.
    • addCostume

      public void addCostume(String name, String imagePath)
      Add a costume to the sprite. If a costume with the received name already exists do nothing.
      Parameters:
      name - a unique name
      imagePath - a image path, or the name of a built-in sprite such as "bunny1_jump"
    • addCostume

      public void addCostume(String name, String spriteSheetPath, int x, int y, int width, int height)
      Adds a new costume to the sprite if a costume with the same name does not already exist.
      Parameters:
      name - The name of the costume.
      spriteSheetPath - The path to the sprite sheet image file.
      x - The x-coordinate of the top-left corner of the costume in the sprite sheet.
      y - The y-coordinate of the top-left corner of the costume in the sprite sheet.
      width - The width of the costume in the sprite sheet.
      height - The height of the costume in the sprite sheet.
    • addCostumes

      public void addCostumes(String prefix, String spriteSheet, int tileWidth, int tileHeight)
      Adds all tiles from a spritesheet as costumes. The costumes will be name by the prefix and the index in the spritesheet. For example if the prefix is "tile" and the spritesheet contains 4 tiles, the costumes will be named "tile0", "tile1", "tile2", and "tile3".
      Parameters:
      prefix - a prefix for all generated costumes
      spriteSheet - a path to a sprite sheet, or the name of a built-in sprite
      tileWidth - the width of a single tile
      tileHeight - the height of a single tile
    • setNineSlice

      protected void setNineSlice(int top, int right, int bottom, int left)
      Set the nine-slice (also known as nine-patch) parameters for the sprite's costumes.
      Parameters:
      top - the size of the top slice in pixels
      right - the size of the right slice in pixels
      bottom - the size of the bottom slice in pixels
      left - the size of the left slice in pixels
    • disableNineSlice

      protected void disableNineSlice()
      Disables the nine-slice feature for all costumes of the sprite.
    • switchCostume

      public void switchCostume(String name)
      Switch to a costume by name.
      Parameters:
      name - the name of a costume
      Scratch Block:
      switch costume to [name v]
    • switchCostume

      public void switchCostume(double index)
      Switches the current costume of the sprite to the costume at the specified index.
      Parameters:
      index - The index of the costume to switch to.
      Scratch Block:
      switch costume to (index)
    • nextCostume

      public void nextCostume()
      Switch to the next costume.
      Scratch Block:
      next costume
    • previousCostume

      public void previousCostume()
      Switch to the previous costume.
      Scratch Block:
      switch costume to (previous costume)
    • getCurrentCostumeName

      public String getCurrentCostumeName()
      Returns the current costume name
      Returns:
      a costume name
      Scratch Block:
      (costume [name v])
    • getCurrentCostumeIndex

      public int getCurrentCostumeIndex()
      Returns the current costume index
      Returns:
      a costume index
      Scratch Block:
      (costume [number v])
    • addSound

      public void addSound(String name, String soundPath)
      Add a sound to the sprite. If a sound with the received name already exists do nothing.
      Parameters:
      name - a unique name
      soundPath - a sound path, or the name of a built-in sound such as "footstep_carpet_000"
    • addSound

      public void addSound(String name)
      Add one of the sounds that ship with Scratch for Java to the sprite. The sound gets the same name as the built-in sound. If a sound with that name already exists do nothing.

      Example usage:

      
       this.addSound("footstep_carpet_000");
       
      Parameters:
      name - the name of a built-in sound, for example "footstep_carpet_000"
    • playSound

      public void playSound(String name)
      Plays a sound.

      Every sprite plays its own copy, so ten sprites playing the same sound are heard ten times at once. One sprite only plays a sound once at a time: playing it again while it is still going does nothing.

      Parameters:
      name - the sound name
      Scratch Block:
      start sound [name v]
    • stopAllSounds

      public void stopAllSounds()
      Stops the playing of all sounds of the sprite.
      Scratch Block:
      stop all sounds
    • stopSound

      public void stopSound(String name)
      Stops the playing of the sound with the given name
      Parameters:
      name - Name of the sound
    • isSoundPlaying

      public boolean isSoundPlaying(String name)
      Returns true if the sound if playing
      Parameters:
      name - Name of the sound
      Returns:
      playing
    • setTint

      public void setTint(Color c)
      Sets the tint for the sprite with an color object.
      Parameters:
      c - a color object
      See Also:
    • setTint

      public void setTint(double r, double g, double b)
      Sets the tint for the sprite with rgb.
      Parameters:
      r - a red value [0...255]
      g - a green value [0...255]
      b - a blue value [0...255]
      See Also:
    • setTint

      public void setTint(double h)
      Sets the tint for the sprite with a hue.
      Parameters:
      h - a hue value [0...255]
      See Also:
      Scratch Block:
      set [color v] effect to (h)
    • changeTint

      public void changeTint(double step)
      Changes the tint for the sprite by a step.
      Parameters:
      step - a step value
      Scratch Block:
      change [color v] effect by (step)
    • getTint

      public Color getTint()
      Retrieves the tint value of the current costume.
      Returns:
      the tint value of the current costume, or null if there are no costumes.
    • setTransparency

      public void setTransparency(double transparency)
      Sets how see-through the sprite is, as Scratch's ghost effect does: 0 is the solid sprite you start with and 100 is invisible. Anything outside that is pinned to it.
      Parameters:
      transparency - [0...100], 0 solid and 100 invisible
      See Also:
      Scratch Block:
      set [ghost v] effect to (transparency)
    • changeTransparency

      public void changeTransparency(double step)
      Changes how see-through the sprite is by a step. A positive step fades it out, a negative one brings it back.
      Parameters:
      step - a step value
      See Also:
      Scratch Block:
      change [ghost v] effect by (step)
    • getTransparency

      public double getTransparency()
      Gets the transparency of the current costume.
      Returns:
      how see-through the sprite is, 0 solid and 100 invisible
      See Also:
    • hide

      public void hide()
      Hides the sprite. The pen is not effected.
      Scratch Block:
      hide
    • show

      public void show()
      Shows the sprite.
      Scratch Block:
      show
    • isVisible

      public boolean isVisible()
      Returns if the sprite is visible
      Returns:
      is visible
    • getSize

      public double getSize()
      Returns the size of the sprite.
      Returns:
      size in percentage
      Scratch Block:
      (size)
    • setSize

      public void setSize(double percentage)
      Sets the size of the sprite.
      Parameters:
      percentage - a percentage [0...100]
      Scratch Block:
      set size to (percentage) %
    • setHeight

      protected void setHeight(double height)
      * Sets the height of the sprite.
      Parameters:
      height - a height in pixels
    • setWidth

      protected void setWidth(double width)
      * Sets the width of the sprite.
      Parameters:
      width - a width in pixels
    • changeSize

      public void changeSize(double amount)
      Changes the size of the sprite by a given percentage.
      Parameters:
      amount - a percentage [0...100]
      Scratch Block:
      change size by (amount)
    • ifOnEdgeBounce

      public void ifOnEdgeBounce()
      Checks if the sprite is on the edge of the stage and bounces it back if it is. The sprite's direction is reversed when it hits the left or right border, and it is reversed and rotated by 180 degrees when it hits the top or bottom border. The method does nothing if the hitbox is disabled or if the sprite is a UI element. It also does nothing if the sprite has not been added to a stage yet.
      Scratch Block:
      if on edge, bounce
    • setRotationStyle

      public void setRotationStyle(RotationStyle style)
      Sets the rotation style for the sprite.
      Parameters:
      style - the rotation style to be set
      See Also:
      Scratch Block:
      set rotation style [left-right v]
    • setPosition

      public void setPosition(double x, double y)
      Sets the position of the sprite
      Parameters:
      x - a x coordinate
      y - a y coordinate
      Scratch Block:
      go to x: (x) y: (y)
    • setPosition

      public void setPosition(Vector2 v)
      Sets the position of the sprite based on the coordinates of a given vector.
      Parameters:
      v - a vector
    • changePosition

      public void changePosition(Vector2 v)
      Changes the position of the sprite by a given vector.
      Parameters:
      v - a vector representing the change in position
    • changePosition

      public void changePosition(double x, double y)
      Changes the position of the sprite by the given amounts.
      Parameters:
      x - how far to move along the x-axis
      y - how far to move along the y-axis
    • getPosition

      public Vector2 getPosition()
      Retrieves the current position of the sprite.
      Returns:
      A Vector2 object representing the x and y coordinates of the sprite.
    • turnLeft

      public void turnLeft(double degrees)
      Rotates the sprite by a certain degrees to the left.
      Parameters:
      degrees - between 0 and 360
      Scratch Block:
      turn left (degrees) degrees
    • turnRight

      public void turnRight(double degrees)
      Rotates the sprite by a certain degrees to the right.
      Parameters:
      degrees - between 0 and 360
      Scratch Block:
      turn right (degrees) degrees
    • setDirection

      public void setDirection(double degrees)
      Sets the direction of the sprite to a given degrees. When this value is 0 the sprite moves up, when it is 180 it moves down.
      Parameters:
      degrees - between 0 and 360
      Scratch Block:
      point in direction (degrees)
    • setDirection

      public void setDirection(Vector2 v)
      Sets the direction of the sprite to the direction of a given vector.
      Parameters:
      v - a vector
    • pointInDirection

      public void pointInDirection(double degrees)
      Points the sprite in the specified direction.
      Parameters:
      degrees - The direction in degrees to point the sprite. 0 degrees is up, 90 degrees is to the right, 180 degrees is down, and 270 degrees is to the left.
    • pointInDirection

      public void pointInDirection(Vector2 v)
      Points the sprite in the direction of the given vector.
      Parameters:
      v - the target vector to point towards
    • pointTowardsMousePointer

      public void pointTowardsMousePointer()
      Points the sprite towards the current position of the mouse pointer. This method calculates the angle between the sprite's current position and the mouse pointer's position, then sets the sprite's direction accordingly.
      Scratch Block:
      point towards [mouse-pointer v]
    • pointTowardsSprite

      public void pointTowardsSprite(Sprite s)
      Points the current sprite towards the specified sprite.
      Parameters:
      s - the sprite to point towards
      Scratch Block:
      point towards [sprite v]
    • getDirection

      public double getDirection()
      Returns the direction of the sprite.
      Returns:
      the direction [0...360]
      Scratch Block:
      (direction)
    • getPen

      public Pen getPen()
      Returns the pen of the sprite.
      Returns:
    • move

      public void move(double steps)
      Moves the sprite towards the current rotation by the received steps.
      Parameters:
      steps - a number of pixels
      Scratch Block:
      move (steps) steps
    • move

      public void move(Vector2 v)
      Moves the sprite in the direction of the given vector. The length of the vector determines how move the sprite will move in this direction.
      Parameters:
      v - a vector
    • getX

      public double getX()
      Returns the x coordinate of the sprite
      Returns:
      a x coordinate
      Scratch Block:
      (x position)
    • setX

      public void setX(double x)
      Sets the x coordinate
      Parameters:
      x - a x coordinate
      Scratch Block:
      set x to (x)
    • changeX

      public void changeX(double x)
      Changes x by a certain amount
      Parameters:
      x - number in pixels
      Scratch Block:
      change x by (x)
    • getY

      public double getY()
      Returns the y coordinate of the sprite
      Returns:
      a y coordinate
      Scratch Block:
      (y position)
    • setY

      public void setY(double y)
      Sets the y coordinate
      Parameters:
      y - a y coordinate
      Scratch Block:
      set y to (y)
    • changeY

      public void changeY(double y)
      Changes y by a certain amount
      Parameters:
      y - number in pixels
      Scratch Block:
      change y by (y)
    • getWidth

      public int getWidth()
      Return the width of the current costume or the pen size, when no costume is available.
      Returns:
      the width of the sprite
    • getHeight

      public int getHeight()
      Return the height of the current costume or the pen size, when no costume is available.
      Returns:
      the height of the sprite
    • getTimer

      public Timer getTimer()
      Return the default timer
      Returns:
      the default timer
      Scratch Block:
      (timer)
    • getTimer

      public Timer getTimer(String name)
      Return a timer by name
      Returns:
      a timer
    • isTouchingMousePointer

      public boolean isTouchingMousePointer()
      Returns true is the mouse pointer is touching the hitbox of the sprite.
      Returns:
      true if touching
      Scratch Block:
      <touching [mouse-pointer v]?>
    • isTouchingEdge

      public boolean isTouchingEdge()
      Returns true if the rectangle which contains the image is outside of the stage
      Returns:
      true if outside
      Scratch Block:
      <touching [edge v]?>
    • distanceToMousePointer

      public double distanceToMousePointer()
      Calculates the distance from the current sprite to the mouse pointer.
      Returns:
      the distance to the mouse pointer as a double.
      Scratch Block:
      (distance to [mouse-pointer v])
    • distanceToSprite

      public double distanceToSprite(Sprite sprite)
      Calculates the Euclidean distance between this sprite and another sprite.
      Parameters:
      sprite - the other sprite to which the distance is calculated
      Returns:
      the distance between this sprite and the specified sprite
      Scratch Block:
      (distance to [sprite v])
    • setHitbox

      public void setHitbox(double... points)
      Sets the hitbox for the sprite using the provided points. The points should be provided in pairs representing the x and y coordinates.
      Parameters:
      points - an array of integers representing the x and y coordinates of the hitbox vertices. The length of the array should be even, with each pair of integers representing a point (x, y).
    • setHitbox

      public void setHitbox(Shape shape)
      Sets the hitbox for the sprite using the specified shape.
      Parameters:
      shape - the shape to be used for the hitbox
    • disableHitbox

      public void disableHitbox()
      Disables the hitbox for the sprite. Once the hitbox is disabled, the sprite will no longer detect collisions with other objects.
    • enableHitbox

      public void enableHitbox()
      Enables the hitbox for the sprite. This method sets the hitboxDisabled flag to false, allowing the sprite to interact with other objects.
    • getHitbox

      public Hitbox getHitbox()
      Returns the hitbox of the sprite based on its current costume, position, and rotation. If the sprite has a hitbox already defined, it updates and returns it. Otherwise, it calculates a new hitbox based on the sprite's dimensions and rotation.
      Returns:
      the hitbox of the sprite
    • isTouchingSprite

      public boolean isTouchingSprite(Sprite sprite)
      Checks if this sprite is touching another sprite.
      Parameters:
      sprite - The sprite to check for collision with.
      Returns:
      true if this sprite is touching the specified sprite, false otherwise. Returns false if the specified sprite is the same as this sprite, if the stage is null, if the specified sprite is null, not shown, or has its hitbox disabled.
      Scratch Block:
      <touching [sprite v]?>
    • isTouchingSprite

      public boolean isTouchingSprite(Class<? extends Sprite> c)
      Checks if this sprite is touching any sprite of the specified class type.
      Parameters:
      c - the class type of the sprite to check for collision
      Returns:
      true if this sprite is touching any sprite of the specified class type, false otherwise
    • getTouchingSprite

      public <T extends Sprite> T getTouchingSprite(Class<T> c)
      Returns the first sprite of the specified type that is currently touching this sprite.
      Type Parameters:
      T - the type of the sprite to check for
      Parameters:
      c - the class object of the type of sprite to check for
      Returns:
      the first sprite of the specified type that is touching this sprite, or null if no such sprite is found
    • getTouchingSprites

      public <T extends Sprite> List<T> getTouchingSprites(Class<T> c)
      Returns a list of sprites of the specified type that are currently touching this sprite.
      Type Parameters:
      T - the type of sprites to return
      Parameters:
      c - the class of the type of sprites to return
      Returns:
      a list of sprites of the specified type that are touching this sprite, or null if the stage is not set
    • getMouseX

      public double getMouseX()
      Returns the current x-position of the mouse cursor
      Returns:
      x-position
      Scratch Block:
      (mouse x)
    • getMouseY

      public double getMouseY()
      Returns the current y-position of the mouse cursor
      Returns:
      y-position
      Scratch Block:
      (mouse y)
    • getMouse

      public Vector2 getMouse()
      Retrieves the current position of the mouse cursor.
      Returns:
      a Vector2 object representing the current mouse cursor position, with the x-coordinate obtained from getMouseX() and the y-coordinate obtained from getMouseY().
      See Also:
    • isMouseDown

      public boolean isMouseDown()
      Returns true is the mouse button is down
      Returns:
      mouse button down
      Scratch Block:
      <mouse down?>
    • isKeyPressed

      public boolean isKeyPressed(KeyCode keyCode)
      Returns true if the key is pressed
      Parameters:
      keyCode - a key
      Returns:
      key pressed
      Scratch Block:
      <key [space v] pressed?>
    • getDeltaTime

      public double getDeltaTime()
      Gets the seconds passed since the last frame.
      Returns:
      seconds since last frame
    • whenKeyPressed

      public void whenKeyPressed(KeyCode keyCode)
      This method is called when a key is pressed. Override this method to define custom behavior.
      Parameters:
      keyCode - the key that was pressed
      See Also:
      Scratch Block:
      when [space v] key pressed
    • whenKeyReleased

      public void whenKeyReleased(KeyCode keyCode)
      This method is called when a key is released. Override this method to define custom behavior.
      Parameters:
      keyCode - the key that was released
      See Also:
    • whenMouseMoved

      public void whenMouseMoved(double x, double y)
      This method is called when the mouse is moved. Override this method to define custom behavior.
      Parameters:
      x - The x-coordinate of the mouse pointer.
      y - The y-coordinate of the mouse pointer.
    • whenMouseClicked

      public void whenMouseClicked(MouseCode mouseCode)
      This method is called when a mouse click event occurs. Override this method to define custom behavior.
      Parameters:
      mouseCode - The code representing the mouse button that was clicked.
    • whenClicked

      public void whenClicked()
      This method is called when the sprite is clicked. Override this method to define custom behavior for the sprite when it is clicked.
      Scratch Block:
      when this sprite clicked
    • goToRandomPosition

      public void goToRandomPosition()
      Moves the sprite to a random position within the boundaries of the stage. The new position is determined by generating random coordinates within the width and height of the stage.
      Scratch Block:
      go to [random position v]
    • goToMousePointer

      public void goToMousePointer()
      Moves the sprite to the current position of the mouse pointer. This method updates the sprite's position to the coordinates of the mouse cursor.
      Scratch Block:
      go to [mouse-pointer v]
    • goToSprite

      public void goToSprite(Sprite sprite)
      Moves this sprite to the position of the specified sprite.
      Parameters:
      sprite - the sprite to move to
      Scratch Block:
      go to [sprite v]
    • goToFrontLayer

      public void goToFrontLayer()
      Moves this sprite to the front layer of the stage. If the sprite is not part of any stage, the method does nothing.
      Scratch Block:
      go to [front v] layer
    • goToBackLayer

      public void goToBackLayer()
      Moves the sprite to the back layer of the stage. If the sprite is not associated with any stage, the method returns without performing any action.
      Scratch Block:
      go to [back v] layer
    • goLayersForwards

      public void goLayersForwards(int number)
      Moves the sprite forward by a specified number of layers within its stage.
      Parameters:
      number - the number of layers to move the sprite forward
      Scratch Block:
      go [forward v] (number) layers
    • goLayersBackwards

      public void goLayersBackwards(int number)
      Moves the sprite backwards by a specified number of layers in the stage. If the sprite is not part of a stage, the method does nothing.
      Parameters:
      number - the number of layers to move the sprite backwards
      Scratch Block:
      go [backward v] (number) layers
    • whenBackdropSwitches

      public void whenBackdropSwitches(String name)
      This method is called when the backdrop switches to the specified name. Override this method to define custom behavior.
      Parameters:
      name - the name of the backdrop to switch to
      Scratch Block:
      when backdrop switches to [name v]
    • pickRandom

      public int pickRandom(int from, int to)
      Returns a random integer between the specified range.
      Parameters:
      from - the lower bound of the range (inclusive)
      to - the upper bound of the range (exclusive)
      Returns:
      a random integer between the specified range
      Scratch Block:
      (pick random (from) to (to))
    • getText

      public Text getText()
      Retrieves the text associated with this sprite.
      Returns:
      the text associated with this sprite
    • think

      public void think(String text)
      Displays a thought bubble with the specified text.
      Parameters:
      text - The text to be displayed in the thought bubble.
      Scratch Block:
      think [text]
    • think

      public void think(String text, int millis)
      Displays a thought bubble with the specified text for a given duration.
      Parameters:
      text - The text to be displayed in the thought bubble.
      millis - The duration in milliseconds for which the thought bubble will be displayed.
      Scratch Block:
      think [text] for (millis) seconds
    • say

      public void say(String text)
      Makes the sprite display a speech bubble with the specified text.
      Parameters:
      text - The text to be displayed in the speech bubble.
      Scratch Block:
      say [text]
    • say

      public void say(String text, int millis)
      Displays a text message for a specified duration.
      Parameters:
      text - The message to be displayed.
      millis - The duration in milliseconds for which the message will be displayed.
      Scratch Block:
      say [text] for (millis) seconds
    • setVolume

      public void setVolume(double percent)
      Sets how loud every sound of this sprite plays.
      Parameters:
      percent - 0 for silent, 100 for full volume
      Scratch Block:
      set volume to (percent) %
    • changeVolume

      public void changeVolume(double step)
      Makes every sound of this sprite louder or quieter.
      Parameters:
      step - how much to add to the volume, in percent
      Scratch Block:
      change volume by (step)
    • getVolume

      public double getVolume()
      Returns how loud the sounds of this sprite play.
      Returns:
      the volume, from 0 to 100
      Scratch Block:
      (volume)
    • glide

      public void glide(double seconds, double x, double y)
      Slides the sprite to a place over the given time, instead of jumping there.

      Unlike Scratch this does not hold up the sprite: `run()` keeps being called while the sprite is on its way. Use isGliding() to tell whether it has arrived.

      Parameters:
      seconds - how long the trip should take
      x - where to end up
      y - where to end up
      Scratch Block:
      glide (seconds) secs to x: (x) y: (y)
    • isGliding

      public boolean isGliding()
      Checks whether the sprite is still on its way to a place it was told to glide to.
      Returns:
      true while it is still moving
    • ask

      public void ask(String question)
      Asks a question and waits for an answer to be typed in. The question appears at the top of the stage.

      Unlike Scratch, this does not pause the sprite. `run()` keeps being called while the question is on screen, so check isAsking() or wait for getAnswer() to change.

      Parameters:
      question - the question to show
      Scratch Block:
      ask [question] and wait
    • getAnswer

      public String getAnswer()
      Returns the last answer that was typed in.
      Returns:
      the answer, or an empty string if nothing has been answered yet
      Scratch Block:
      (answer)
    • isAsking

      public boolean isAsking()
      Checks whether a question is on screen and still waiting for an answer.
      Returns:
      true while a question is waiting
    • broadcast

      public void broadcast(String message)
      Broadcasts a message to all sprites in the stage except the current sprite. If the stage is not set, the method returns immediately.
      Parameters:
      message - The message to broadcast to other sprites.
      Scratch Block:
      broadcast [message v]
    • whenIReceive

      public void whenIReceive(String message)
      This method is called when a message is received. Override this method to define custom behavior.
      Parameters:
      message - The message that is received.
      See Also:
      Scratch Block:
      when I receive [message v]
    • stamp

      public void stamp()
      Stamps the current sprite to the background. A stamp is a non interactive version of the sprite.
      Scratch Block:
      stamp
    • stamp

      public void stamp(Layer layer)
      Stamps the sprite onto one of the stage's layers. A stamp is a picture of the sprite that stays where it is put, and is not interactive.

      Example usage:

      
       this.stamp(Layer.UI);
       
      Parameters:
      layer - which layer to stamp onto
    • setUI

      protected void setUI(boolean isUI)
      Sets the UI status of the sprite.
      Parameters:
      isUI - A boolean value indicating whether the sprite is part of the UI.
    • isUI

      protected boolean isUI()
      Checks if the sprite is part of the user interface.
      Returns:
      true if the sprite is part of the user interface, false otherwise.
    • run

      public void run()
      This method is intended to be overridden by subclasses to define the behavior of the sprite when it is run. By default, this method does nothing.

      It is called every frame.

    • addedToStage

      protected void addedToStage(Stage stage)
    • removedFromStage

      protected void removedFromStage(Stage stage)
    • draw

      protected void draw(processing.core.PGraphics buffer)
      Draws the sprite if it is not hidden.
    • drawDebug

      protected void drawDebug(processing.core.PGraphics buffer)
      Draws debug information for the sprite. This includes the hitbox and the current costume. The hitbox is drawn if it is not disabled and the sprite is not a UI element. The current costume is drawn if there are costumes available and the sprite is set to be shown.
    • clone

      public Sprite clone()
      Creates 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.
      Overrides:
      clone in class Object
      Returns:
      a new Sprite object that is a clone of the current sprite
      Scratch Block:
      create clone of [myself v]