Class Sprite

java.lang.Object
org.openpatch.scratch.Sprite
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
DrawingSprite

public class Sprite extends Object implements Cloneable
A sprite: an object on the stage with costumes, a position and a direction, and code that runs every tick in run(), like a sprite and its scripts in Scratch.
public class Cat extends Sprite {
  public Cat() {
    this.addCostume("cat", "assets/cat.png");
  }

  public void run() {
    this.move(2);
    this.ifOnEdgeBounce();
  }
}

Positions are Scratch's: (0, 0) is the centre of the stage, x grows to the right and y upwards. Directions are Scratch's too: 0 points up, 90 to the right (the start direction), 180 down, 270 to the left. Everything a sprite does happens in ticks of 1/60 s of game time, so move(5) in run() moves 5 steps per tick on every computer.

  • Field Details

    • DIRECTION_UP

      public static final double DIRECTION_UP
      Direction pointing up (0 degrees).
      See Also:
    • DIRECTION_RIGHT

      public static final double DIRECTION_RIGHT
      Direction pointing right (90 degrees), where a new sprite points.
      See Also:
    • DIRECTION_DOWN

      public static final double DIRECTION_DOWN
      Direction pointing down (180 degrees).
      See Also:
    • DIRECTION_LEFT

      public static final double DIRECTION_LEFT
      Direction pointing left (270 degrees).
      See Also:
  • Constructor Details

    • Sprite

      public Sprite()
      A sprite without costumes. Add some with addCostume(String, String).
    • Sprite

      public Sprite(String name, String imagePath)
      A sprite with one costume.
      Parameters:
      name - the costume's name
      imagePath - the image file
  • Method Details

    • extension

      public <T> T extension(Class<T> type, Supplier<T> factory)
      An extension's state for this sprite, made on first use; a clone gets its own. For extensions, not for learner code.
      Type Parameters:
      T - the state
      Parameters:
      type - the state's class
      factory - makes it
      Returns:
      the sprite's one instance
    • whenAddedToStage

      public void whenAddedToStage()
      Called when the sprite was added to a stage. Override it to react.
    • whenRemovedFromStage

      public void whenRemovedFromStage()
      Called when the sprite was removed from its stage. Override it to react.
    • getStage

      public Stage getStage()
      Returns:
      the stage the sprite is on, or null
    • remove

      public void remove()
      Removes the sprite from its stage.
    • debug

      public void debug(Object... values)
      Prints values for debugging, but only while debug mode is on (window.setDebug(true) or F12).
      Parameters:
      values - what to print
    • showVariable

      public void showVariable(String name, Supplier<?> value)
      Shows a variable in a box at the top left of the stage, like a Scratch monitor; its value is read every frame.
      this.showVariable("score", () -> this.score);
      
      Parameters:
      name - the label
      value - gives the value to show
      Scratch Block:
      show variable [name v]
    • hideVariable

      public void hideVariable(String name)
      Parameters:
      name - the label of a variable to hide again
      Scratch Block:
      hide variable [name v]
    • addCostume

      public void addCostume(String name, String imagePath)
      Adds a costume from an image file. The first costume added is the one the sprite wears.
      Parameters:
      name - the costume's name, for switchCostume(String)
      imagePath - the image file (PNG, JPEG, GIF, BMP or TGA)
    • addCostume

      public void addCostume(String imagePath)
      Adds a costume from an image file, named after the file.
      Parameters:
      imagePath - the image file
    • addCostume

      public void addCostume(String name, String imagePath, int x, int y, int width, int height)
      Adds a costume cut out of a larger image, e.g. one frame of a sprite sheet.
      Parameters:
      name - the costume's name
      imagePath - the image file
      x - left edge of the part, in pixels
      y - top edge of the part, in pixels
      width - width of the part
      height - height of the part
    • addCostumes

      public void addCostumes(String name, String imagePath, int width, int height)
      Adds every frame of a sprite sheet as a costume, row by row, named name0, name1, …
      Parameters:
      name - the start of the costumes' names
      imagePath - the image file
      width - width of one frame
      height - height of one frame
    • switchCostume

      public void switchCostume(String name)
      Switches to the costume with that name; an unknown name changes nothing.
      Parameters:
      name - the costume's name
      Scratch Block:
      switch costume to [name v]
    • switchCostume

      public void switchCostume(double index)
      Switches to a costume by number, counting from 0; numbers wrap around like Scratch's.
      Parameters:
      index - the costume's number
      Scratch Block:
      switch costume to [name v]
    • nextCostume

      public void nextCostume()
      Switches to the next costume, after the last one to the first.
      Scratch Block:
      next costume
    • previousCostume

      public void previousCostume()
      Switches to the previous costume, before the first one to the last.
      Scratch Block:
      switch costume to (previous costume)
    • getCurrentCostumeName

      public String getCurrentCostumeName()
      Returns:
      the current costume's name, or null without costumes
      Scratch Block:
      (costume [name v])
    • getCurrentCostumeIndex

      public int getCurrentCostumeIndex()
      Returns:
      the current costume's number, counting from 0
      Scratch Block:
      (costume [number v])
    • addAnimation

      public void addAnimation(String name, String pattern, int frames)
      Adds an animation from numbered image files: the pattern's %d counts from 1.
      this.addAnimation("walk", "assets/walk%d.png", 4); // walk1.png … walk4.png
      
      Parameters:
      name - the animation's name
      pattern - the file names, with %d for the frame number
      frames - how many frames
    • addAnimation

      public void addAnimation(String name, String pattern, int from, int to)
      Adds an animation from numbered image files, from one number to another: for files that count from 0, or to use only some of them. If to is smaller than from, it counts down.
      this.addAnimation("hit", "assets/hit%d.png", 0, 4); // hit0.png … hit4.png
      
      Parameters:
      name - the animation's name
      pattern - the file names, with %d for the frame number
      from - the number of the first frame
      to - the number of the last frame
    • addAnimation

      public void addAnimation(String name, String sheetPath, int width, int height, int... frames)
      Adds an animation from a sprite sheet: frames of equal size, numbered row by row from the top left, starting at 0.
      this.addAnimation("walk", "assets/walk.png", 32, 32, 0, 1, 2, 3);   // the first row
      this.addAnimation("down", "assets/hero.png", 32, 32, 0, 4, 8, 12);  // the first column of 4
      
      Parameters:
      name - the animation's name
      sheetPath - the sprite sheet
      width - the width of a frame
      height - the height of a frame
      frames - the frames to show, in order
    • playAnimation

      public void playAnimation(String name)
      Plays an animation, over and over. Call it every tick (in run()): it shows the next frame each time the animation interval has passed, and starts from the first frame when another animation was playing before.
      Parameters:
      name - the animation's name
    • playAnimation

      public void playAnimation(String name, boolean once)
      Plays an animation; call it every tick.
      Parameters:
      name - the animation's name
      once - whether to stop at the last frame instead of starting over
    • setAnimationFrame

      public void setAnimationFrame(int frame)
      Parameters:
      frame - the frame of the current animation to show next, counting from 0
    • resetAnimation

      public void resetAnimation()
      Starts the current animation from its first frame again.
    • setAnimationInterval

      public void setAnimationInterval(double seconds)
      Parameters:
      seconds - how long each frame shows
    • getAnimationInterval

      public double getAnimationInterval()
      Returns:
      how long each frame shows, in seconds
    • getAnimationFrame

      public int getAnimationFrame()
      Returns:
      the frame of the current animation, counting from 0
    • isAnimationPlayed

      public boolean isAnimationPlayed()
      Returns:
      whether an animation played with once reached its last frame
    • setRotationCenter

      public void setRotationCenter(double x, double y)
      Sets the point of the costumes the sprite turns around and stands on, in costume pixels from the top left corner. Without it, that is the centre of each costume.
      Parameters:
      x - pixels from the left edge
      y - pixels from the top edge
    • addSound

      public void addSound(String name, String soundPath)
      Adds a sound (WAV or OGG).
      Parameters:
      name - the sound's name, for playSound(String)
      soundPath - the sound file
    • addSound

      public void addSound(String soundPath)
      Adds a sound, named after its file.
      Parameters:
      soundPath - the sound file
    • playSound

      public void playSound(String name)
      Starts a sound, like Scratch's "start sound"; a sound that is still playing is not started again.
      Parameters:
      name - the sound's name
      Scratch Block:
      start sound [name v]
    • playSoundCopy

      public SoundPlayback playSoundCopy(String name, double volume)
      Starts another copy of a sound, which plays alongside the others: for shots, steps, coins.
      Parameters:
      name - the sound's name
      volume - the copy's volume from 0 to 100
      Returns:
      the copy, to stop it or change its volume
    • stopSound

      public void stopSound(String name)
      Parameters:
      name - the sound to stop
    • stopAllSounds

      public void stopAllSounds()
      Stops every sound this object started.
      Scratch Block:
      stop all sounds
    • isSoundPlaying

      public boolean isSoundPlaying(String name)
      Parameters:
      name - a sound's name
      Returns:
      whether it is playing
    • setVolume

      public void setVolume(double volume)
      Parameters:
      volume - this object's volume from 0 to 100
      Scratch Block:
      set volume to (percent) %
    • changeVolume

      public void changeVolume(double by)
      Parameters:
      by - how much to change the volume
      Scratch Block:
      change volume by (step)
    • getVolume

      public double getVolume()
      Returns:
      this object's volume from 0 to 100
      Scratch Block:
      (volume)
    • show

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

      public void hide()
      Hides the sprite: it is not drawn, cannot be touched and cannot be clicked.
      Scratch Block:
      hide
    • isVisible

      public boolean isVisible()
      Returns:
      whether the sprite is shown
    • getSize

      public double getSize()
      Returns:
      the size in percent of the costume's own size
      Scratch Block:
      (size)
    • setSize

      public void setSize(double percent)
      Parameters:
      percent - the size in percent of the costume's own size
      Scratch Block:
      set size to (percentage) %
    • changeSize

      public void changeSize(double percent)
      Parameters:
      percent - how much to change the size, in percentage points
      Scratch Block:
      change size by (amount)
    • getWidth

      public double getWidth()
      Returns:
      the width the sprite is drawn at, in stage pixels
    • getHeight

      public double getHeight()
      Returns:
      the height the sprite is drawn at, in stage pixels
    • setWidth

      public void setWidth(double width)
      Draws the sprite this wide, whatever its costume's width and size: for buttons, panels and platforms. With setNineSlice(int, int, int, int) the corners keep their size.
      Parameters:
      width - the width in stage pixels; 0 to follow the costume and size again
    • setHeight

      public void setHeight(double height)
      Draws the sprite this high; see setWidth(double).
      Parameters:
      height - the height in stage pixels; 0 to follow the costume and size again
    • changeWidth

      public void changeWidth(double by)
      Parameters:
      by - how much wider to draw the sprite
    • changeHeight

      public void changeHeight(double by)
      Parameters:
      by - how much higher to draw the sprite
    • setNineSlice

      public void setNineSlice(int top, int right, int bottom, int left)
      Stretches only the middle of the costume when the sprite is drawn at its own width and height: the four corners keep their size (scaled with the sprite's size), the edges stretch along one side.
      Parameters:
      top - costume pixels at the top that keep their height
      right - costume pixels at the right that keep their width
      bottom - costume pixels at the bottom that keep their height
      left - costume pixels at the left that keep their width
    • disableNineSlice

      public void disableNineSlice()
      Stretches the whole costume again.
    • setColorEffect

      public void setColorEffect(double value)
      Scratch's colour effect: shifts every colour of the costume around the colour wheel. 0 is no change; 100 is halfway round; 200 is all the way round again. Like Scratch, it gives colours too grey or too dark to show a hue a little one, so white and black are tinted too.
      Parameters:
      value - the effect
      Scratch Block:
      set [color v] effect to (value)
    • changeColorEffect

      public void changeColorEffect(double by)
      Parameters:
      by - how much to change the colour effect
      Scratch Block:
      change [color v] effect by (by)
    • getColorEffect

      public double getColorEffect()
      Returns:
      the colour effect
    • setTint

      public void setTint(Color color)
      Multiplies the costume by a colour: white leaves it as it is, red keeps only its red. Not a Scratch block: Scratch's colour effect is setColorEffect(double).
      Parameters:
      color - the tint, or null for none
    • setTint

      public void setTint(double red, double green, double blue)
      Parameters:
      red - red from 0 to 255
      green - green from 0 to 255
      blue - blue from 0 to 255
    • getTint

      public Color getTint()
      Returns:
      the tint, or null for none
    • setTransparency

      public void setTransparency(double percent)
      Scratch's ghost effect.
      Parameters:
      percent - 0 opaque, 100 invisible
      Scratch Block:
      set [ghost v] effect to (transparency)
    • changeTransparency

      public void changeTransparency(double by)
      Parameters:
      by - how much to change the transparency
      Scratch Block:
      change [ghost v] effect by (step)
    • getTransparency

      public double getTransparency()
      Returns:
      the transparency in percent
    • setLayer

      public void setLayer(Layer layer)
      Moves the sprite to another layer, in front of the sprites already there. A sprite in Layer.UI ignores the camera; its pen moves to the UI layer too.
      Parameters:
      layer - the layer
    • getLayer

      public Layer getLayer()
      Returns:
      the sprite's layer
    • goToFrontLayer

      public void goToFrontLayer()
      Moves the sprite in front of every other sprite of its layer.
      Scratch Block:
      go to [front v] layer
    • goToBackLayer

      public void goToBackLayer()
      Moves the sprite behind every other sprite of its layer.
      Scratch Block:
      go to [back v] layer
    • goLayersForwards

      public void goLayersForwards(int number)
      Parameters:
      number - how many sprites of its layer to move in front of
      Scratch Block:
      go [forward v] (number) layers
    • goLayersBackwards

      public void goLayersBackwards(int number)
      Parameters:
      number - how many sprites of its layer to move behind
      Scratch Block:
      go [backward v] (number) layers
    • getX

      public double getX()
      Returns:
      x of the sprite's position
      Scratch Block:
      (x position)
    • getY

      public double getY()
      Returns:
      y of the sprite's position
      Scratch Block:
      (y position)
    • getPosition

      public Vector2 getPosition()
      Returns:
      the sprite's position
    • setPosition

      public void setPosition(double x, double y)
      Goes to a position; with the pen down, it draws a line there.
      Parameters:
      x - x of the new position
      y - y of the new position
      Scratch Block:
      go to x: (x) y: (y)
    • setPosition

      public void setPosition(Vector2 position)
      Parameters:
      position - the new position
      Scratch Block:
      go to x: (x) y: (y)
    • setX

      public void setX(double x)
      Parameters:
      x - the new x
      Scratch Block:
      set x to (x)
    • setY

      public void setY(double y)
      Parameters:
      y - the new y
      Scratch Block:
      set y to (y)
    • changeX

      public void changeX(double dx)
      Parameters:
      dx - how far to move along x
      Scratch Block:
      change x by (x)
    • changeY

      public void changeY(double dy)
      Parameters:
      dy - how far to move along y
      Scratch Block:
      change y by (y)
    • changePosition

      public void changePosition(double dx, double dy)
      Parameters:
      dx - how far to move along x
      dy - how far to move along y
    • changePosition

      public void changePosition(Vector2 by)
      Parameters:
      by - how far to move
    • move

      public void move(double steps)
      Moves forward in the sprite's direction.
      Parameters:
      steps - how far, in stage pixels (negative moves backwards)
      Scratch Block:
      move (steps) steps
    • getDirection

      public double getDirection()
      Returns:
      the direction, from 0 (inclusive) to 360 (exclusive); 90 is right
      Scratch Block:
      (direction)
    • setDirection

      public void setDirection(double degrees)
      Points in a direction: 0 up, 90 right, 180 down, 270 left; other numbers go around.
      Parameters:
      degrees - the direction
      Scratch Block:
      point in direction (degrees)
    • turnRight

      public void turnRight(double degrees)
      Parameters:
      degrees - how far to turn clockwise
      Scratch Block:
      turn right (degrees) degrees
    • turnLeft

      public void turnLeft(double degrees)
      Parameters:
      degrees - how far to turn anticlockwise
      Scratch Block:
      turn left (degrees) degrees
    • pointTowards

      public void pointTowards(Vector2 position)
      Points towards a position.
      Parameters:
      position - where to point
    • pointTowards

      public void pointTowards(Sprite sprite)
      Parameters:
      sprite - the sprite to point at
    • pointTowardsMousePointer

      public void pointTowardsMousePointer()
      Points towards the mouse pointer.
      Scratch Block:
      point towards [mouse-pointer v]
    • setRotationStyle

      public void setRotationStyle(RotationStyle style)
      Parameters:
      style - how the costume follows the direction
      Scratch Block:
      set rotation style [left-right v]
    • getRotationStyle

      public RotationStyle getRotationStyle()
      Returns:
      how the costume follows the direction
    • goToRandomPosition

      public void goToRandomPosition()
      Goes to a random position on the stage.
      Scratch Block:
      go to [random position v]
    • goToMousePointer

      public void goToMousePointer()
      Goes to the mouse pointer.
      Scratch Block:
      go to [mouse-pointer v]
    • goToSprite

      public void goToSprite(Sprite sprite)
      Parameters:
      sprite - the sprite whose position to go to
      Scratch Block:
      go to [sprite v]
    • glide

      public void glide(double seconds, double x, double y)
      Glides to a position in a number of seconds of game time; the sprite keeps running its code meanwhile. A new glide replaces one that is still going.
      Parameters:
      seconds - how long the glide takes
      x - x of the target
      y - y of the target
      Scratch Block:
      glide (seconds) secs to x: (x) y: (y)
    • isGliding

      public boolean isGliding()
      Returns:
      whether a glide is still going
    • ifOnEdgeBounce

      public void ifOnEdgeBounce()
      Turns around when touching an edge of the stage and moves back inside, as in Scratch.
      Scratch Block:
      if on edge, bounce
    • getShaders

      public Shaders getShaders()
      Returns:
      the sprite's shaders
    • setHitbox

      public void setHitbox(Shape shape)
      Sets the hitbox: the shape that counts for touching, in costume pixels (origin at the costume's top left corner, y down). It turns, grows and mirrors with the sprite.
      Parameters:
      shape - the hitbox
    • disableHitbox

      public void disableHitbox()
      Touching ignores this sprite until enableHitbox().
    • enableHitbox

      public void enableHitbox()
      Touching counts this sprite again.
    • getHitbox

      public Shape getHitbox()
      The hitbox where the sprite is now, in stage coordinates: by default the rectangle around the costume's painted pixels; null for a sprite without costumes.
      Returns:
      the hitbox, or null
    • isTouchingSprite

      public boolean isTouchingSprite(Sprite sprite)
      Parameters:
      sprite - another sprite
      Returns:
      whether the two hitboxes overlap (touching edges do not count); a hidden sprite touches nothing, and the UI layer never touches the world
      Scratch Block:
      <touching [sprite v]?>
    • isTouchingSprite

      public boolean isTouchingSprite(Class<? extends Sprite> type)
      Parameters:
      type - a class of sprites
      Returns:
      whether the sprite touches any sprite of that class (or a subclass)
      Scratch Block:
      <touching [sprite v]?>
    • getTouchingSprite

      public <T extends Sprite> T getTouchingSprite(Class<T> type)
      Type Parameters:
      T - the class
      Parameters:
      type - a class of sprites
      Returns:
      a sprite of that class the sprite touches, or null
    • getTouchingSprites

      public <T extends Sprite> List<T> getTouchingSprites(Class<T> type)
      Type Parameters:
      T - the class
      Parameters:
      type - a class of sprites
      Returns:
      every sprite of that class the sprite touches
    • isTouchingColor

      public boolean isTouchingColor(Color color)
      Whether the sprite touches a colour: a painted pixel of the sprite lies over that colour, as the stage shows it now (backdrop, stage colour, pen strokes, stamps and other sprites with their effects; not texts, shaders or the UI layer). Colours match as in Scratch, with a small tolerance.
      Parameters:
      color - the colour to look for
      Returns:
      whether the sprite touches it
      Scratch Block:
      <touching color [#ff0000]?>
    • isTouchingColor

      public boolean isTouchingColor(double red, double green, double blue)
      Parameters:
      red - red from 0 to 255
      green - green from 0 to 255
      blue - blue from 0 to 255
      Returns:
      whether the sprite touches that colour
      Scratch Block:
      <touching color [#ff0000]?>
    • isColorTouchingColor

      public boolean isColorTouchingColor(Color mine, Color color)
      Whether a colour of the sprite touches a colour on the stage, like Scratch's "color is touching color".
      Parameters:
      mine - a colour of the sprite
      color - the colour to look for underneath
      Returns:
      whether they touch
      Scratch Block:
      <color [#ff0000] is touching [#00ff00]?>
    • isTouchingMousePointer

      public boolean isTouchingMousePointer()
      Returns:
      whether the mouse pointer is over the sprite's hitbox
      Scratch Block:
      <touching [mouse-pointer v]?>
    • isTouchingEdge

      public boolean isTouchingEdge()
      Returns:
      whether the sprite's hitbox reaches an edge of the stage
      Scratch Block:
      <touching [edge v]?>
    • distanceToMousePointer

      public double distanceToMousePointer()
      Returns:
      the distance to the mouse pointer
      Scratch Block:
      (distance to [mouse-pointer v])
    • distanceToSprite

      public double distanceToSprite(Sprite sprite)
      Parameters:
      sprite - another sprite
      Returns:
      the distance between the two positions
      Scratch Block:
      (distance to [sprite v])
    • getTimer

      public Timer getTimer()
      Returns:
      this object's timer, counting game time since it was made or reset
      Scratch Block:
      (timer)
    • getMouseX

      public double getMouseX()
      Returns:
      x of the mouse pointer, in the sprite's coordinates (world, or the stage for UI)
      Scratch Block:
      (mouse x)
    • getMouseY

      public double getMouseY()
      Returns:
      y of the mouse pointer
      Scratch Block:
      (mouse y)
    • getMouse

      public Vector2 getMouse()
      Returns:
      the mouse pointer's position, in world coordinates (or stage coordinates for UI)
    • isMouseDown

      public boolean isMouseDown()
      Returns:
      whether a mouse button is held down
      Scratch Block:
      <mouse down?>
    • isKeyPressed

      public boolean isKeyPressed(KeyCode key)
      Parameters:
      key - a key
      Returns:
      whether the key is held down
      Scratch Block:
      <key [space v] pressed?>
    • whenKeyPressed

      public void whenKeyPressed(KeyCode key)
      Called when a key is pressed, once per press (holding a key down does not repeat it).
      Parameters:
      key - the key
      Scratch Block:
      when [space v] key pressed
    • whenKeyReleased

      public void whenKeyReleased(KeyCode key)
      Called when a key is released.
      Parameters:
      key - the key
    • whenMouseMoved

      public void whenMouseMoved(double x, double y)
      Called when the mouse moves.
      Parameters:
      x - x of the mouse pointer (world)
      y - y of the mouse pointer (world)
    • whenMouseClicked

      public void whenMouseClicked(MouseCode button)
      Called when a mouse button is pressed anywhere on the stage.
      Parameters:
      button - the button
    • whenClicked

      public void whenClicked()
      Called when this sprite is clicked: a mouse button is pressed over its hitbox, and no sprite in front of it is under the pointer.
      Scratch Block:
      when this sprite clicked
    • broadcast

      public void broadcast(String message)
      Sends a message to the stage and every sprite on it; their whenIReceive(String) runs at once, before this method returns.
      Parameters:
      message - the message
      Scratch Block:
      broadcast [message v]
    • whenIReceive

      public void whenIReceive(String message)
      Called when a message is broadcast.
      Parameters:
      message - the message
      Scratch Block:
      when I receive [message v]
    • whenBackdropSwitches

      public void whenBackdropSwitches(String name)
      Called when the stage switches its backdrop.
      Parameters:
      name - the new backdrop's name
      Scratch Block:
      when backdrop switches to [name v]
    • pickRandom

      public int pickRandom(int from, int to)
      A random whole number between two numbers, both included.
      Parameters:
      from - the lowest
      to - the highest
      Returns:
      the number
      Scratch Block:
      (pick random (from) to (to))
    • run

      public void run()
      Runs every tick, 60 times a second of game time. Override it with the sprite's code.
    • getText

      public Text getText()
      Returns:
      the sprite's text, which say and think show in a bubble
    • say

      public void say(String text)
      Shows a speech bubble until the sprite says something else; an empty text removes it.
      Parameters:
      text - what the sprite says
      Scratch Block:
      say [text]
    • say

      public void say(String text, double seconds)
      Shows a speech bubble for a number of seconds of game time.
      Parameters:
      text - what the sprite says
      seconds - how long
      Scratch Block:
      say [text] for (seconds) seconds
    • think

      public void think(String text)
      Shows a thought bubble until the sprite thinks something else; an empty text removes it.
      Parameters:
      text - what the sprite thinks
      Scratch Block:
      think [text]
    • think

      public void think(String text, double seconds)
      Shows a thought bubble for a number of seconds of game time.
      Parameters:
      text - what the sprite thinks
      seconds - how long
      Scratch Block:
      think [text] for (seconds) seconds
    • ask

      public void ask(String question)
      Asks a question: the sprite says it in a bubble, and a box to type the answer in appears at the bottom of the stage. Enter finishes it; see Stage.ask(String).
      Parameters:
      question - the question
      Scratch Block:
      ask [question] and wait
    • isAsking

      public boolean isAsking()
      Returns:
      whether a question is waiting for its answer
    • getAnswer

      public String getAnswer()
      Returns:
      the answer to the last question
      Scratch Block:
      (answer)
    • createClone

      public <T extends Sprite> T createClone()
      Makes a clone: a copy of this sprite with the same costumes, position, direction, effects and variables, added to the stage just behind this sprite, as in Scratch. Its whenStartsAsClone() runs; its constructor does not. Numbers, texts and booleans in its variables are its own; lists and other objects are shared with this sprite until the clone gets its own in whenStartsAsClone().
      Bullet bullet = this.createClone();
      
      Type Parameters:
      T - the sprite's class (no cast needed)
      Returns:
      the clone
    • whenStartsAsClone

      public void whenStartsAsClone()
      Called in a new clone, right after it was made.
      Scratch Block:
      when I start as a clone
    • isClone

      public boolean isClone()
      Returns:
      whether this sprite is a clone
    • deleteThisClone

      public void deleteThisClone()
      Removes this sprite if it is a clone, stopping its sounds; does nothing for the original.
      Scratch Block:
      delete this clone
    • getPen

      public Pen getPen()
      Returns:
      the sprite's pen, which follows it
    • stamp

      public void stamp()
      Stamps the costume onto the canvas of the sprite's pen layer (behind the sprites unless the pen was moved): a picture of the sprite as it looks now, effects included, that stays.
      Scratch Block:
      stamp
    • stamp

      public void stamp(Layer layer)
      Stamps the costume onto the canvas of a layer.
      Parameters:
      layer - the layer
      Scratch Block:
      stamp
    • toString

      public String toString()
      The sprite's centre of the costume on the stage, as Scratch's "x position"/"y position".
      Overrides:
      toString in class Object