Class Sprite
- Direct Known Subclasses:
AnimatedSprite,UISprite
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleDirection constant pointing down (180 degrees).static final doubleDirection constant pointing left (270 degrees).static final doubleDirection constant pointing right (90 degrees).static final doubleDirection constant pointing up (0 degrees). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCostume(String name) Add one of the costumes that ship with Scratch for Java to the sprite.voidaddCostume(String name, String imagePath) Add a costume to the sprite.voidaddCostume(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.voidaddCostumes(String prefix, String spriteSheet, int tileWidth, int tileHeight) Adds all tiles from a spritesheet as costumes.protected voidaddedToStage(Stage stage) voidAdd one of the sounds that ship with Scratch for Java to the sprite.voidAdd a sound to the sprite.voidAsks a question and waits for an answer to be typed in.voidBroadcasts a message to all sprites in the stage except the current sprite.voidchangePosition(double x, double y) Changes the position of the sprite by the given amounts.voidChanges the position of the sprite by a given vector.voidchangeSize(double amount) Changes the size of the sprite by a given percentage.voidchangeTint(double step) Changes the tint for the sprite by a step.voidchangeTransparency(double step) Changes how see-through the sprite is by a step.voidchangeVolume(double step) Makes every sound of this sprite louder or quieter.voidchangeX(double x) Changes x by a certain amountvoidchangeY(double y) Changes y by a certain amountclone()Creates a clone of the current sprite.voidPrints a debug message to stdout when debug mode is enabled.voidDisables the hitbox for the sprite.protected voidDisables the nine-slice feature for all costumes of the sprite.doubleCalculates the distance from the current sprite to the mouse pointer.doubledistanceToSprite(Sprite sprite) Calculates the Euclidean distance between this sprite and another sprite.protected voiddraw(processing.core.PGraphics buffer) Draws the sprite if it is not hidden.protected voiddrawDebug(processing.core.PGraphics buffer) Draws debug information for the sprite.voidEnables the hitbox for the sprite.Returns the last answer that was typed in.intReturns the current costume indexReturns the current costume namedoubleGets the seconds passed since the last frame.doubleReturns the direction of the sprite.intReturn 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.getMouse()Retrieves the current position of the mouse cursor.doubleReturns the current x-position of the mouse cursordoubleReturns the current y-position of the mouse cursorgetPen()Returns the pen of the sprite.Retrieves the current position of the sprite.Returns the shaders of this sprite.doublegetSize()Returns the size of the sprite.getStage()Retrieves the current stage associated with this sprite.getText()Retrieves the text associated with this sprite.getTimer()Return the default timerReturn a timer by namegetTint()Retrieves the tint value of the current costume.<T extends Sprite>
TgetTouchingSprite(Class<T> c) Returns the first sprite of the specified type that is currently touching this sprite.getTouchingSprites(Class<T> c) Returns a list of sprites of the specified type that are currently touching this sprite.doubleGets the transparency of the current costume.doubleReturns how loud the sounds of this sprite play.intgetWidth()Return the width of the current costume or the pen size, when no costume is available.doublegetX()Returns the x coordinate of the spritedoublegetY()Returns the y coordinate of the spritevoidglide(double seconds, double x, double y) Slides the sprite to a place over the given time, instead of jumping there.voidgoLayersBackwards(int number) Moves the sprite backwards by a specified number of layers in the stage.voidgoLayersForwards(int number) Moves the sprite forward by a specified number of layers within its stage.voidMoves the sprite to the back layer of the stage.voidMoves this sprite to the front layer of the stage.voidMoves the sprite to the current position of the mouse pointer.voidMoves the sprite to a random position within the boundaries of the stage.voidgoToSprite(Sprite sprite) Moves this sprite to the position of the specified sprite.voidhide()Hides the sprite.voidChecks if the sprite is on the edge of the stage and bounces it back if it is.booleanisAsking()Checks whether a question is on screen and still waiting for an answer.booleanChecks whether the sprite is still on its way to a place it was told to glide to.booleanisKeyPressed(KeyCode keyCode) Returns true if the key is pressedbooleanReturns true is the mouse button is downbooleanisSoundPlaying(String name) Returns true if the sound if playingbooleanReturns true if the rectangle which contains the image is outside of the stagebooleanReturns true is the mouse pointer is touching the hitbox of the sprite.booleanisTouchingSprite(Class<? extends Sprite> c) Checks if this sprite is touching any sprite of the specified class type.booleanisTouchingSprite(Sprite sprite) Checks if this sprite is touching another sprite.protected booleanisUI()Checks if the sprite is part of the user interface.booleanReturns if the sprite is visiblevoidmove(double steps) Moves the sprite towards the current rotation by the received steps.voidMoves the sprite in the direction of the given vector.voidSwitch to the next costume.intpickRandom(int from, int to) Returns a random integer between the specified range.voidPlays a sound.voidpointInDirection(double degrees) Points the sprite in the specified direction.voidPoints the sprite in the direction of the given vector.voidPoints the sprite towards the current position of the mouse pointer.voidPoints the current sprite towards the specified sprite.voidSwitch to the previous costume.voidremove()Removes this sprite from its current stage.protected voidremovedFromStage(Stage stage) voidrun()This method is intended to be overridden by subclasses to define the behavior of the sprite when it is run.voidMakes the sprite display a speech bubble with the specified text.voidDisplays a text message for a specified duration.voidsetDirection(double degrees) Sets the direction of the sprite to a given degrees.voidSets the direction of the sprite to the direction of a given vector.protected voidsetHeight(double height) * Sets the height of the sprite.voidsetHitbox(double... points) Sets the hitbox for the sprite using the provided points.voidSets the hitbox for the sprite using the specified shape.protected voidsetNineSlice(int top, int right, int bottom, int left) Set the nine-slice (also known as nine-patch) parameters for the sprite's costumes.voidsetPosition(double x, double y) Sets the position of the spritevoidSets the position of the sprite based on the coordinates of a given vector.voidsetRotationStyle(RotationStyle style) Sets the rotation style for the sprite.voidsetSize(double percentage) Sets the size of the sprite.voidsetTint(double h) Sets the tint for the sprite with a hue.voidsetTint(double r, double g, double b) Sets the tint for the sprite with rgb.voidSets the tint for the sprite with an color object.voidsetTransparency(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 voidsetUI(boolean isUI) Sets the UI status of the sprite.voidsetVolume(double percent) Sets how loud every sound of this sprite plays.protected voidsetWidth(double width) * Sets the width of the sprite.voidsetX(double x) Sets the x coordinatevoidsetY(double y) Sets the y coordinatevoidshow()Shows the sprite.voidstamp()Stamps the current sprite to the background.voidStamps the sprite onto one of the stage's layers.voidStops the playing of all sounds of the sprite.voidStops the playing of the sound with the given namevoidswitchCostume(double index) Switches the current costume of the sprite to the costume at the specified index.voidswitchCostume(String name) Switch to a costume by name.voidDisplays a thought bubble with the specified text.voidDisplays a thought bubble with the specified text for a given duration.voidturnLeft(double degrees) Rotates the sprite by a certain degrees to the left.voidturnRight(double degrees) Rotates the sprite by a certain degrees to the right.voidThis method is called when the sprite is added to the stage.voidwhenBackdropSwitches(String name) This method is called when the backdrop switches to the specified name.voidThis method is called when the sprite is clicked.voidwhenIReceive(String message) This method is called when a message is received.voidwhenKeyPressed(KeyCode keyCode) This method is called when a key is pressed.voidwhenKeyReleased(KeyCode keyCode) This method is called when a key is released.voidwhenMouseClicked(MouseCode mouseCode) This method is called when a mouse click event occurs.voidwhenMouseMoved(double x, double y) This method is called when the mouse is moved.voidThis method is called when the sprite is removed from the stage.
-
Field Details
-
DIRECTION_UP
public static final double DIRECTION_UPDirection constant pointing up (0 degrees).- See Also:
-
DIRECTION_RIGHT
public static final double DIRECTION_RIGHTDirection constant pointing right (90 degrees).- See Also:
-
DIRECTION_DOWN
public static final double DIRECTION_DOWNDirection constant pointing down (180 degrees).- See Also:
-
DIRECTION_LEFT
public static final double DIRECTION_LEFTDirection constant pointing left (270 degrees).- See Also:
-
-
Constructor Details
-
Sprite
public Sprite()Constructs a new Sprite object with default settings. -
Sprite
Constructs a new Sprite object with a specified costume.- Parameters:
name- a unique name for the costumeimagePath- a path to an image file, or the name of a built-in sprite such as "slimeGreen"
-
Sprite
Copies a Sprite object.- Parameters:
s- a Sprite object to copy
-
-
Method Details
-
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
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
Retrieves the current stage associated with this sprite.- Returns:
- the stage associated with this sprite
-
addCostume
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
Add a costume to the sprite. If a costume with the received name already exists do nothing.- Parameters:
name- a unique nameimagePath- a image path, or the name of a built-in sprite such as "bunny1_jump"
-
addCostume
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
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 costumesspriteSheet- a path to a sprite sheet, or the name of a built-in spritetileWidth- the width of a single tiletileHeight- 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 pixelsright- the size of the right slice in pixelsbottom- the size of the bottom slice in pixelsleft- the size of the left slice in pixels
-
disableNineSlice
protected void disableNineSlice()Disables the nine-slice feature for all costumes of the sprite. -
switchCostume
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
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
Add a sound to the sprite. If a sound with the received name already exists do nothing.- Parameters:
name- a unique namesoundPath- a sound path, or the name of a built-in sound such as "footstep_carpet_000"
-
addSound
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
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
Stops the playing of the sound with the given name- Parameters:
name- Name of the sound
-
isSoundPlaying
Returns true if the sound if playing- Parameters:
name- Name of the sound- Returns:
- playing
-
setTint
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
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
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 coordinatey- a y coordinate- Scratch Block:
- go to x: (x) y: (y)
-
setPosition
Sets the position of the sprite based on the coordinates of a given vector.- Parameters:
v- a vector
-
changePosition
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-axisy- how far to move along the y-axis
-
getPosition
Retrieves the current position of the sprite.- Returns:
- A
Vector2object 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
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
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
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
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
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
Return the default timer- Returns:
- the default timer
- Scratch Block:
- (timer)
-
getTimer
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Retrieves the text associated with this sprite.- Returns:
- the text associated with this sprite
-
think
Displays a thought bubble with the specified text.- Parameters:
text- The text to be displayed in the thought bubble.- Scratch Block:
- think [text]
-
think
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
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
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 takex- where to end upy- 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
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 forgetAnswer()to change.- Parameters:
question- the question to show- Scratch Block:
- ask [question] and wait
-
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
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
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
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
-
removedFromStage
-
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
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.
-