Package org.openpatch.scratch
Class Sprite
java.lang.Object
org.openpatch.scratch.Sprite
- Direct Known Subclasses:
AnimatedSprite
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.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addCostume
(String name, String imagePath) Add a costume to the sprite.void
addCostume
(String name, String spriteSheetPath, int x, int y, int width, int height) Adds a new costume to the sprite if a costume with the same name does not already exist.void
addCostumes
(String prefix, String spriteSheet, int tileWidth, int tileHeight) Adds all tiles from a spritesheet as costumes.Adds a new shader to the sprite.void
Add a sound to the sprite.void
Add a new timer by name.void
Broadcasts a message to all sprites in the stage except the current sprite, and also to the stage itself.void
Broadcasts a message to all sprites in the stage except the current sprite.void
changeSize
(double amount) Changes the size of the sprite by a given percentage.void
changeTint
(double step) Changes the tint for the sprite by a step.void
changeTransparency
(double step) Changes the transparency for the sprite.void
changeX
(double x) Changes x by a certain amountvoid
changeY
(double y) Changes y by a certain amountvoid
Disables the hitbox for the sprite.double
Calculates the distance from the current sprite to the mouse pointer.double
distanceToSprite
(Sprite sprite) Calculates the Euclidean distance between this sprite and another sprite.void
Enables the hitbox for the sprite.int
Returns the current costume indexReturns the current costume nameint
Returns the current day of the monthint
Returns the current day of the weekint
Returns the current hourint
Returns the current millisecondint
Returns the current minuteint
Returns the current monthint
Returns the current secondRetrieves the current shader.int
Retrieves the index of the current shader.Retrieves the name of the current shader.int
Returns the current yearint
Returns the days since 2010/01/01double
Gets the seconds passed since the last frame.double
Returns the direction of the sprite.int
Return the height of the current costume or the pen size, when no costume is available.Returns the hitbox of the sprite based on its current costume, position, and rotation.getMouse()
Retrieves the current position of the mouse cursor.double
Returns the current x-position of the mouse cursordouble
Returns the current y-position of the mouse cursorgetPen()
Returns the pen of the sprite.Retrieves the current position of the sprite.Retrieves a shader by name.double
getSize()
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 namedouble
getTint()
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.int
getWidth()
Return the width of the current costume or the pen size, when no costume is available.double
getX()
Returns the x coordinate of the spritedouble
getY()
Returns the y coordinate of the spritevoid
goLayersBackwards
(int number) Moves the sprite backwards by a specified number of layers in the stage.void
goLayersForwards
(int number) Moves the sprite forward by a specified number of layers within its stage.void
Moves the sprite to the back layer of the stage.void
Moves this sprite to the front layer of the stage.void
Moves the sprite to the current position of the mouse pointer.void
Moves the sprite to a random position within the boundaries of the stage.void
goToSprite
(Sprite sprite) Moves this sprite to the position of the specified sprite.void
hide()
Hides the sprite.void
Checks if the sprite is on the edge of the stage and bounces it back if it is.boolean
isKeyPressed
(int keyCode) Returns true if the key is pressedboolean
Returns true is the mouse button is downboolean
isSoundPlaying
(String name) Returns true if the sound if playingboolean
Returns true if the rectangle which contains the image is outside of the stageboolean
Returns true is the mouse pointer is touching a non transparent area of the sprite.boolean
isTouchingSprite
(Class<? extends Sprite> c) Checks if this sprite is touching any sprite of the specified class type.boolean
isTouchingSprite
(Sprite sprite) Checks if this sprite is touching another sprite.boolean
isUI()
Checks if the sprite is part of the user interface.void
isUI
(boolean isUI) Sets the UI status of the sprite.boolean
Returns if the sprite is visiblevoid
keyEvent
(processing.event.KeyEvent e) void
mouseEvent
(processing.event.MouseEvent e) Handles mouse events.void
move
(double steps) Moves the sprite towards the current rotation by the received steps.void
Moves the sprite in the direction of the given vector.void
Switch to the next costume.void
Sets the next shader as the current shader.int
pickRandom
(int from, int to) Returns a random integer between the specified range.void
Plays a sound.void
pointInDirection
(double degrees) Points the sprite in the specified direction.void
Points the sprite in the direction of the given vector.void
Points the sprite towards the current position of the mouse pointer.void
Points the current sprite towards the specified sprite.void
remove()
Removes this sprite from its current stage.void
removeSound
(String name) Remove a sound from the sprite.void
removeTimer
(String name) Remove a timer by name.void
void
run()
This method is intended to be overridden by subclasses to define the behavior of the sprite when it is run.void
Makes the sprite display a speech bubble with the specified text.void
Displays a text message for a specified duration.void
setDirection
(double degrees) Sets the direction of the sprite to a given degrees.void
Sets the direction of the sprite to the direction of a given vector.void
setHitbox
(int... points) Sets the hitbox for the sprite using the provided points.void
setHitbox
(int[] xPoints, int[] yPoints) Sets the hitbox for the sprite using the provided x and y coordinates.void
Sets the hitbox for the sprite using the specified shape.void
Sets the hitbox for the sprite using the provided Hitbox object.void
setOnEdgeBounce
(boolean b) Sets if the sprite should bounce when hitting the edge of the screen.void
setPosition
(double x, double y) Sets the position of the spritevoid
Sets the position of the sprite based on the coordinates of a given vector.void
setRotationStyle
(RotationStyle style) Sets the rotation style for the sprite.void
setSize
(double percentage) Sets the size of the sprite.void
setTint
(double h) Sets the tint for the sprite with a hue.void
setTint
(double r, double g, double b) Sets the tint for the sprite with rgb.void
Sets the tint for the sprite with an color object.void
setTransparency
(double transparency) Sets the transparency of the sprite.void
setX
(double x) Sets the x coordinatevoid
setY
(double y) Sets the y coordinatevoid
show()
Shows the sprite.void
stamp()
void
Stamps the current sprite to the background.void
Stamps the current sprite to the foreground.void
Stamps the current sprite to the ui.void
Stops the playing of all sounds of the sprite.void
Stops the playing of the sound with the given namevoid
switchCostume
(double index) Switches the current costume of the sprite to the costume at the specified index.void
switchCostume
(String name) Switch to a costume by name.void
switchShader
(double index) Switch to a shader by index.void
switchShader
(String name) Switch to a shader by name.void
Displays a thought bubble with the specified text.void
Displays a thought bubble with the specified text for a given duration.void
turnLeft
(double degrees) Rotates the sprite by a certain degrees to the left.void
turnRight
(double degrees) Rotates the sprite by a certain degrees to the right.void
This method is called when the sprite is added to the stage.void
whenAddedToStage
(Stage stage) This method is called when the sprite is added to the stage.void
whenBackdropSwitches
(String name) This method is called when the backdrop switches to the specified name.void
This method is called when the sprite is clicked.void
whenIReceive
(Object message) This method is called when a message is received.void
whenIReceive
(String message) This method is called when a message is received.void
whenKeyPressed
(int keyCode) This method is called when a key is pressed.void
whenKeyReleased
(int keyCode) This method is called when a key is released.void
whenMouseClicked
(MouseCode mouseCode) This method is called when a mouse click event occurs.void
whenMouseMoved
(double x, double y) This method is called when the mouse is moved.void
This method is called when the sprite is removed from the stage.void
whenRemovedFromStage
(Stage stage) This method is called when the sprite is removed from the stage.
-
Constructor Details
-
Sprite
public Sprite() -
Sprite
-
Sprite
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
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.
-
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
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.
-
remove
public void remove()Removes this sprite from its current stage. -
getStage
Retrieves the current stage associated with this sprite.- Returns:
- the stage associated with this sprite
-
addShader
Adds a new shader to the sprite. If a shader with the received name already exists, this method does nothing.- Parameters:
name
-shaderPath
-- Returns:
- the shader
-
switchShader
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() -
getShader
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
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
Retrieves the current shader.- Returns:
- the current shader, or null if no shaders exist
-
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
-
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.- Parameters:
prefix
- a prefix for all generated costumesspriteSheet
- a path to a sprite sheettileWidth
- the width of a single tiletileHeight
- the height of a single tile
-
switchCostume
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. -
getCurrentCostumeName
Returns the current costume name- Returns:
- a costume name
-
getCurrentCostumeIndex
public int getCurrentCostumeIndex()Returns the current costume index- Returns:
- a costume index
-
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
-
removeSound
Remove a sound from the sprite.- Parameters:
name
- the sound name
-
playSound
Plays a sound.- Parameters:
name
- the sound name
-
stopAllSounds
public void stopAllSounds()Stops the playing of all sounds of the sprite. -
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:
-
changeTint
public void changeTint(double step) Changes the tint for the sprite by a step.- Parameters:
step
- a step value
-
getTint
public double getTint()Retrieves the tint value of the current costume.- Returns:
- the tint value of the current costume, or 0 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:
-
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]
-
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
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 coordinatey
- a y coordinate
-
setPosition
Sets the position of the sprite based on the coordinates of a given vector.- Parameters:
v
- a vector
-
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
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
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
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
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
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
Return the default timer- Returns:
- the default timer
-
getTimer
Return a timer by name- Returns:
- a timer
-
addTimer
Add a new timer by name. Overwriting default is not permitted.- Parameters:
name
- the name of the timer
-
removeTimer
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 a non transparent area 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
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(int... 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(int[] xPoints, int[] yPoints) Sets the hitbox for the sprite using the provided x and y coordinates.- Parameters:
xPoints
- an array of x coordinates for the hitboxyPoints
- an array of y coordinates for the hitbox
-
setHitbox
Sets the hitbox for the sprite using the provided Hitbox object.- Parameters:
hitbox
- the Hitbox object to set
-
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.
-
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
-
getMouseY
public double getMouseY()Returns the current y-position of the mouse cursor- Returns:
- y-position
-
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:
-
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:
-
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.
-
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. -
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
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
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
-
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
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.
-
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.
-
say
Makes the sprite display a speech bubble with the specified text.- Parameters:
text
- The text to be displayed in the speech bubble.
-
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.
-
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.
-
broadcast
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
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
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:
-
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.
-
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.
-