Class Sprite

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

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:
  • Constructor Details

    • Sprite

      public Sprite()
    • Sprite

      public Sprite(String name, String imagePath)
    • Sprite

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

    • 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.
    • whenAddedToStage

      public void whenAddedToStage(Stage stage)
      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.
      Parameters:
      stage - The stage to which the sprite is added.
    • setWhenAddedToStageHandler

      public void setWhenAddedToStageHandler(Sprite.WhenAddedToStageHandler handler)
      Sets the handler for the whenAddedToStage event. This handler will be called when the sprite is added to a stage.
      Parameters:
      handler - an AddedToStageHandler that takes a Stage as an argument.
    • 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.
    • whenRemovedFromStage

      public void whenRemovedFromStage(Stage stage)
      This method is called when the sprite is removed from the stage. Override this method to define custom behavior when the sprite is removed.
      Parameters:
      stage - The stage from which the sprite is removed from.
    • setWhenRemovedFromStageHandler

      public void setWhenRemovedFromStageHandler(Sprite.WhenRemovedFromStageHandler handler)
      Sets the handler for the whenRemovedFromStage event. This handler will be called when the sprite is removed from a stage.
      Parameters:
      handler - an RemovedFromStageHandler that takes a Stage as an
    • remove

      public void remove()
      Removes this sprite from its current stage.
    • getStage

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

      public Shader addShader(String name, String fragmentShaderPath, String vertexShaderPath)
      Adds a new shader to the sprite. If a shader with the received name already exists, this method does nothing.
      Parameters:
      name -
      fragmentShaderPath - the path to the fragment shader file
      vertexShaderPath - the path to the vertex shader file
      Returns:
      the shader
    • switchShader

      public void switchShader(String name)
      Switch to a shader by name.
      Parameters:
      name - the name of a shader
    • switchShader

      public void switchShader(double index)
      Switch to a shader by index.
      Parameters:
      index - the index of a shader
    • resetShader

      public void resetShader()
      Resets the current shader to -1, which means no shader is currently active. This method can be used to disable the shader effect on the sprite.
    • getShader

      public Shader getShader(String name)
      Retrieves a shader by name.
      Parameters:
      name - the name of a shader
      Returns:
      the shader with the specified name, or null if no shader with that name exists
    • nextShader

      public void nextShader()
      Sets the next shader as the current shader.
    • getCurrentShaderName

      public String getCurrentShaderName()
      Retrieves the name of the current shader.
      Returns:
      the name of the current shader, or null if no shaders exist
    • getCurrentShaderIndex

      public int getCurrentShaderIndex()
      Retrieves the index of the current shader.
      Returns:
      the index of the current shader
    • getCurrentShader

      public Shader getCurrentShader()
      Retrieves the current shader.
      Returns:
      the current shader, or null if no shaders exist
    • 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
    • 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
      tileWidth - the width of a single tile
      tileHeight - the height of a single tile
    • setNineSlice

      public 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

      public 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
    • 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.
    • nextCostume

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

      public void previousCostume()
      Switch to the next costume.
    • getCurrentCostumeName

      public String getCurrentCostumeName()
      Returns the current costume name
      Returns:
      a costume name
    • getCurrentCostumeIndex

      public int getCurrentCostumeIndex()
      Returns the current costume index
      Returns:
      a costume index
    • 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
    • removeSound

      public void removeSound(String name)
      Remove a sound from the sprite.
      Parameters:
      name - the sound name
    • playSound

      public void playSound(String name)
      Plays a sound.
      Parameters:
      name - the sound name
    • stopAllSounds

      public void stopAllSounds()
      Stops the playing of all sounds of the sprite.
    • 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:
    • changeTint

      public void changeTint(double step)
      Changes the tint for the sprite by a step.
      Parameters:
      step - a step value
    • 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 the transparency of the sprite.
      Parameters:
      transparency - 0 full transparency, 255 no transparency
      See Also:
    • changeTransparency

      public void changeTransparency(double step)
      Changes the transparency for the sprite.
      Parameters:
      step - a step value
      See Also:
    • getTransparency

      public double getTransparency()
      Gets the transparency of the current costume.
      Returns:
      the transparency of the current costume, or 0 if there are no
      See Also:
    • hide

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

      public void show()
      Shows the sprite.
    • 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
    • setSize

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

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

      public void changeHeight(double amount)
      Changes the height of the sprite by a given amount.
      Parameters:
      amount - a height in pixels
    • setWidth

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

      public void changeWidth(double amount)
      Returns the height of the sprite.
      Parameters:
      amount - a amount in pixels
    • changeSize

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

      public void setOnEdgeBounce(boolean b)
      Sets if the sprite should bounce when hitting the edge of the screen. This method is for making is attribute perment.
      Parameters:
      b - true if the sprite should bounce
    • 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.
    • setRotationStyle

      public void setRotationStyle(RotationStyle style)
      Sets the rotation style for the sprite.
      Parameters:
      style - the rotation style to be set
      See Also:
    • setPosition

      public void setPosition(double x, double y)
      Sets the position of the sprite
      Parameters:
      x - a x coordinate
      y - a y coordinate
    • 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
    • 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
    • turnRight

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

      public void setDirection(double degrees)
      Sets the direction of the sprite to a given degrees. When this value is 0 the sprite move right, when it is 180 is moves to the left.
      Parameters:
      degrees - between 0 and 360
    • 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 to the right, 90 degrees is up, 180 degrees is to the left, and 270 degrees is down.
    • 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.
    • pointTowardsSprite

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

      public double getDirection()
      Returns the direction of the sprite.
      Returns:
      the direction [0...360]
    • 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
    • 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
    • setX

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

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

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

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

      public void changeY(double y)
      Changes y by a certain amount
      Parameters:
      y - number in pixels
    • 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
    • getTimer

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

      public void addTimer(String name)
      Add a new timer by name. Overwriting default is not permitted.
      Parameters:
      name - the name of the timer
    • removeTimer

      public void removeTimer(String name)
      Remove a timer by name. Removing of default is not permitted.
      Parameters:
      name - the name of the timer
    • isTouchingMousePointer

      public boolean isTouchingMousePointer()
      Returns true is the mouse pointer is touching the hitbox of the sprite.
      Returns:
      true if touching
    • isTouchingEdge

      public boolean isTouchingEdge()
      Returns true if the rectangle which contains the image is outside of the stage
      Returns:
      true if outside
    • 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.
    • 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
    • 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(double[] xPoints, double[] yPoints)
      Sets the hitbox for the sprite using the provided x and y coordinates.
      Parameters:
      xPoints - an array of x coordinates for the hitbox
      yPoints - an array of y coordinar the hitbox
    • setHitbox

      public void setHitbox(Hitbox hitbox)
      Sets the hitbox for the sprite using the provided Hitbox object.
      Parameters:
      hitbox - the Hitbox object to set
    • 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.
    • 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
    • getMouseY

      public double getMouseY()
      Returns the current y-position of the mouse cursor
      Returns:
      y-position
    • 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
    • isKeyPressed

      public boolean isKeyPressed(int keyCode)
      Returns true if the key is pressed
      Parameters:
      keyCode - a key code
      Returns:
      key pressed
    • getDeltaTime

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

      public int getCurrentYear()
      Returns the current year
      Returns:
      current year
    • getCurrentMonth

      public int getCurrentMonth()
      Returns the current month
      Returns:
      current month
    • getCurrentDay

      public int getCurrentDay()
      Returns the current day of the month
      Returns:
      current day of the month
    • getCurrentDayOfWeek

      public int getCurrentDayOfWeek()
      Returns the current day of the week
      Returns:
      current day of the week
    • getCurrentHour

      public int getCurrentHour()
      Returns the current hour
      Returns:
      current hour
    • getCurrentMinute

      public int getCurrentMinute()
      Returns the current minute
      Returns:
      current minute
    • getCurrentSecond

      public int getCurrentSecond()
      Returns the current second
      Returns:
      current second
    • getCurrentMillisecond

      public int getCurrentMillisecond()
      Returns the current millisecond
      Returns:
      current millisecond
    • getDaysSince2000

      public int getDaysSince2000()
      Returns the days since 2010/01/01
      Returns:
      days since 2010/01/01
    • keyEvent

      public void keyEvent(processing.event.KeyEvent e)
    • whenKeyPressed

      public void whenKeyPressed(int keyCode)
      This method is called when a key is pressed. Override this method to define custom behavior.
      Parameters:
      keyCode - the code of the key that was pressed
      See Also:
    • setWhenKeyPressed

      public void setWhenKeyPressed(Sprite.WhenKeyPressedHandler whenKeyPressed)
      Sets the handler for when a key is pressed. This allows you to define custom behavior when a key is pressed.
      Parameters:
      whenKeyPressed - A KeyPressedHandler that takes a Sprite and an
    • whenKeyReleased

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

      public void setWhenKeyReleased(Sprite.WhenKeyReleasedHandler whenKeyReleased)
      Sets the handle for when a key is released. This allows you to define custom behavior when a key is released.
      Parameters:
      whenKeyReleased - A KeyReleasedHandler that takes a Sprite and an
    • mouseEvent

      public void mouseEvent(processing.event.MouseEvent e)
      Handles mouse events. Override this method to define custom behavior.
      Parameters:
      e - the MouseEvent object containing details about the mouse event
    • 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.
    • setWhenMouseMoved

      public void setWhenMouseMoved(Sprite.WhenMouseMovedHandler whenMouseMoved)
      Sets the handler for when the mouse is moved. This allows you to define custom behavior when the mouse is moved.
      Parameters:
      whenMouseMoved - A MouseMovedHandler that takes a Sprite, x, and y
    • 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.
    • setWhenMouseClicked

      public void setWhenMouseClicked(Sprite.WhenMouseClickedHandler whenMouseClicked)
      Sets the handler for when the mouse is clicked. This allows you to define custom behavior when the mouse is clicked.
      Parameters:
      whenMouseClicked - A MouseClickedHandler that takes a Sprite and a
    • 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.
    • setWhenClicked

      public void setWhenClicked(Sprite.WhenClickedHandler whenClicked)
      Sets the handler for when the sprite is clicked. This allows you to define custom behavior when the sprite is clicked.
      Parameters:
      whenClicked - A ClickedHandler that takes a Sprite as input and
    • 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.
    • 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.
    • goToSprite

      public void goToSprite(Sprite sprite)
      Moves this sprite to the position of the specified sprite.
      Parameters:
      sprite - the sprite to move to
    • 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.
    • 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.
    • 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
    • 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
    • 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
    • setWhenBackdropSwitches

      public void setWhenBackdropSwitches(Sprite.WhenBackdropSwitchesHandler whenBackdropSwitches)
      Sets the handler for when the backdrop switches to a specified name.
      Parameters:
      whenBackdropSwitches - A BackdropSwitchHandler that takes a Sprite and
    • 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
    • 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.
    • 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.
    • 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.
    • 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.
    • 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.
    • broadcast

      public void broadcast(Object message)
      Broadcasts a message to all sprites in the stage except the current sprite, and also to the stage itself.
      Parameters:
      message - The message to be broadcasted. It can be any object.
    • 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:
    • whenIReceive

      public void whenIReceive(Object 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:
    • setWhenIReceive

      public void setWhenIReceive(Sprite.WhenIReceiveHandler whenIReceive)
      Sets the handler for when a message is received. This allows you to define custom behavior. This is useful when you do not want to extend the sprite class and want to use the sprite class directly. For example:
       Sprite sprite = new Sprite();
       sprite.setWhenIReceive((message) -> {
         System.out.println("Received message: " + message);
       });
       
       
      Parameters:
      whenIReceive - An IReceiveHandler that takes a Sprite and a message
    • stamp

      public void stamp()
      See Also:
    • stampToBackground

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

      public void stampToUI()
      Stamps the current sprite to the ui. A stamp is a non interactive version of the sprite.
    • stampToForeground

      public void stampToForeground()
      Stamps the current sprite to the foreground. A stamp is a non interactive version of the sprite.
    • isUI

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

      public 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.
    • getWindow

      public Window getWindow()
      Get the Window object.
      Returns:
      the Window object
    • 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.

    • setRun

      public void setRun(Sprite.RunHandler run)
      Sets the run handler for the sprite. This allows you to define custom behavior when the sprite is run.
      Parameters:
      run - A RunHandler that takes a Sprite as input and defines the
    • 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