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.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic interfacestatic interfacestatic interfacestatic interfacestatic interfacestatic interfacestatic interfacestatic interfacestatic interface -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCostume(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) Adds a new shader to the sprite.voidAdd a sound to the sprite.voidAdd a new timer by name.voidBroadcasts a message to all sprites in the stage except the current sprite, and also to the stage itself.voidBroadcasts a message to all sprites in the stage except the current sprite.voidchangeHeight(double amount) Changes the height of the sprite by a given amount.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 the transparency for the sprite.voidchangeWidth(double amount) Returns the height of the sprite.voidchangeX(double x) Changes x by a certain amountvoidchangeY(double y) Changes y by a certain amountclone()Creates a clone of the current sprite.voidDisables the hitbox for the sprite.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.intReturns the current costume indexReturns the current costume nameintReturns the current day of the monthintReturns the current day of the weekintReturns the current hourintReturns the current millisecondintReturns the current minuteintReturns the current monthintReturns the current secondRetrieves the current shader.intRetrieves the index of the current shader.Retrieves the name of the current shader.intReturns the current yearintReturns the days since 2010/01/01doubleGets 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.Retrieves a shader by name.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.intgetWidth()Return the width of the current costume or the pen size, when no costume is available.Get the Window object.doublegetX()Returns the x coordinate of the spritedoublegetY()Returns the y coordinate of the spritevoidgoLayersBackwards(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.booleanisKeyPressed(int 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.booleanisUI()Checks if the sprite is part of the user interface.voidisUI(boolean isUI) Sets the UI status of the sprite.booleanReturns if the sprite is visiblevoidkeyEvent(processing.event.KeyEvent e) voidmouseEvent(processing.event.MouseEvent e) Handles mouse events.voidmove(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.voidSets the next shader as the current shader.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 next costume.voidremove()Removes this sprite from its current stage.protected voidremovedFromStage(Stage stage) voidremoveSound(String name) Remove a sound from the sprite.voidremoveTimer(String name) Remove a timer by name.voidResets the current shader to -1, which means no shader is currently active.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.voidsetHeight(double height) * Sets the height of the sprite.voidsetHitbox(double... points) Sets the hitbox for the sprite using the provided points.voidsetHitbox(double[] xPoints, double[] yPoints) Sets the hitbox for the sprite using the provided x and y coordinates.voidSets the hitbox for the sprite using the provided Hitbox object.voidSets the hitbox for the sprite using the specified shape.voidsetNineSlice(int top, int right, int bottom, int left) Set the nine-slice (also known as nine-patch) parameters for the sprite's costumes.voidsetOnEdgeBounce(boolean b) Sets if the sprite should bounce when hitting the edge of the screen.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.voidsetRun(Sprite.RunHandler run) Sets the run handler 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 the transparency of the sprite.voidSets the handler for the whenAddedToStage event.voidsetWhenBackdropSwitches(Sprite.WhenBackdropSwitchesHandler whenBackdropSwitches) Sets the handler for when the backdrop switches to a specified name.voidsetWhenClicked(Sprite.WhenClickedHandler whenClicked) Sets the handler for when the sprite is clicked.voidsetWhenIReceive(Sprite.WhenIReceiveHandler whenIReceive) Sets the handler for when a message is received.voidsetWhenKeyPressed(Sprite.WhenKeyPressedHandler whenKeyPressed) Sets the handler for when a key is pressed.voidsetWhenKeyReleased(Sprite.WhenKeyReleasedHandler whenKeyReleased) Sets the handle for when a key is released.voidsetWhenMouseClicked(Sprite.WhenMouseClickedHandler whenMouseClicked) Sets the handler for when the mouse is clicked.voidsetWhenMouseMoved(Sprite.WhenMouseMovedHandler whenMouseMoved) Sets the handler for when the mouse is moved.voidSets the handler for the whenRemovedFromStage event.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 current sprite to the background.voidStamps the current sprite to the foreground.voidStamps the current sprite to the ui.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.voidswitchShader(double index) Switch to a shader by index.voidswitchShader(String name) Switch to a shader 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.voidwhenAddedToStage(Stage stage) This 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(Object message) This method is called when a message is received.voidwhenIReceive(String message) This method is called when a message is received.voidwhenKeyPressed(int keyCode) This method is called when a key is pressed.voidwhenKeyReleased(int 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.voidwhenRemovedFromStage(Stage stage) This method is called when the sprite is removed from the stage.
-
Constructor Details
-
Sprite
public Sprite()Constructs a new Sprite object with default settings. -
Sprite
-
Sprite
-
-
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.
-
setWhenAddedToStageHandler
Sets the handler for the whenAddedToStage event. This handler will be called when the sprite is added to a stage.- Parameters:
handler- an AddedToStageHandler that takes a Stage as an argument.
-
whenRemovedFromStage
public void whenRemovedFromStage()This method is called when the sprite is removed from the stage. Override this method to define custom behavior when the sprite is removed. -
whenRemovedFromStage
This method is called when the sprite is removed from the stage. Override this method to define custom behavior when the sprite is removed.- Parameters:
stage- The stage from which the sprite is removed from.
-
setWhenRemovedFromStageHandler
Sets the handler for the whenRemovedFromStage event. This handler will be called when the sprite is removed from a stage.- Parameters:
handler- an RemovedFromStageHandler that takes a Stage as an
-
remove
public void remove()Removes this sprite from its current stage. -
getStage
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-fragmentShaderPath- the path to the fragment shader filevertexShaderPath- the path to the vertex shader file- 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()Resets the current shader to -1, which means no shader is currently active. This method can be used to disable the shader effect on the sprite. -
getShader
-
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
-
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 sheettileWidth- the width of a single tiletileHeight- the height of a single tile
-
setNineSlice
public void setNineSlice(int top, int right, int bottom, int left) Set the nine-slice (also known as nine-patch) parameters for the sprite's costumes.- Parameters:
top- the size of the top slice in 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
public 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
-
switchCostume
public void switchCostume(double index) Switches the current costume of the sprite to the costume at the specified index.- Parameters:
index- The index of the costume to switch to.
-
nextCostume
public void nextCostume()Switch to the next costume. -
previousCostume
public void previousCostume()Switch to the next costume. -
getCurrentCostumeName
-
getCurrentCostumeIndex
public int getCurrentCostumeIndex()Returns the current costume index- Returns:
- a costume index
-
addSound
-
removeSound
Remove a sound from the sprite.- Parameters:
name- the sound name
-
playSound
-
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
Retrieves the tint value of the current costume.- Returns:
- the tint value of the current costume, or null if there are no costumes.
-
setTransparency
public void setTransparency(double transparency) Sets the transparency of the sprite.- Parameters:
transparency- 0 full transparency, 255 no transparency- See Also:
-
changeTransparency
public void changeTransparency(double step) Changes the transparency for the sprite.- Parameters:
step- a step value- See Also:
-
getTransparency
public double getTransparency()Gets the transparency of the current costume.- Returns:
- the transparency of the current costume, or 0 if there are no
- See Also:
-
hide
public void hide()Hides the sprite. The pen is not effected. -
show
public void show()Shows the sprite. -
isVisible
public boolean isVisible()Returns if the sprite is visible- Returns:
- is visible
-
getSize
public double getSize()Returns the size of the sprite.- Returns:
- size in percentage
-
setSize
public void setSize(double percentage) Sets the size of the sprite.- Parameters:
percentage- a percentage [0...100]
-
setHeight
public void setHeight(double height) * Sets the height of the sprite.- Parameters:
height- a height in pixels
-
changeHeight
public void changeHeight(double amount) Changes the height of the sprite by a given amount.- Parameters:
amount- a height in pixels
-
setWidth
public void setWidth(double width) * Sets the width of the sprite.- Parameters:
width- a width in pixels
-
changeWidth
public void changeWidth(double amount) Returns the height of the sprite.- Parameters:
amount- a amount in pixels
-
changeSize
public void changeSize(double amount) Changes the size of the sprite by a given percentage.- Parameters:
amount- a percentage [0...100]
-
setOnEdgeBounce
public void setOnEdgeBounce(boolean b) Sets if the sprite should bounce when hitting the edge of the screen. This method is for making is attribute perment.- Parameters:
b- true if the sprite should bounce
-
ifOnEdgeBounce
public void ifOnEdgeBounce()Checks if the sprite is on the edge of the stage and bounces it back if it is. The sprite's direction is reversed when it hits the left or right border, and it is reversed and rotated by 180 degrees when it hits the top or bottom border. The method does nothing if the hitbox is disabled or if the sprite is a UI element. -
setRotationStyle
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
-
changePosition
Changes the position of the sprite by a given vector.- Parameters:
v- a vector representing the change in position
-
getPosition
-
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
-
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
-
getTimer
-
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 the hitbox of the sprite.- Returns:
- true if touching
-
isTouchingEdge
public boolean isTouchingEdge()Returns true if the rectangle which contains the image is outside of the stage- Returns:
- true if outside
-
distanceToMousePointer
public double distanceToMousePointer()Calculates the distance from the current sprite to the mouse pointer.- Returns:
- the distance to the mouse pointer as a double.
-
distanceToSprite
Calculates the Euclidean distance between this sprite and another sprite.- Parameters:
sprite- the other sprite to which the distance is calculated- Returns:
- the distance between this sprite and the specified sprite
-
setHitbox
public void setHitbox(double... points) Sets the hitbox for the sprite using the provided points. The points should be provided in pairs representing the x and y coordinates.- Parameters:
points- an array of integers representing the x and y coordinates of the hitbox vertices. The length of the array should be even, with each pair of integers representing a point (x, y).
-
setHitbox
public void setHitbox(double[] xPoints, double[] yPoints) Sets the hitbox for the sprite using the provided x and y coordinates.- Parameters:
xPoints- an array of x coordinates for the hitboxyPoints- an array of y coordinar 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
-
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:
-
setWhenKeyPressed
Sets the handler for when a key is pressed. This allows you to define custom behavior when a key is pressed.- Parameters:
whenKeyPressed- A KeyPressedHandler that takes a Sprite and an
-
whenKeyReleased
public void whenKeyReleased(int keyCode) This method is called when a key is released. Override this method to define custom behavior.- Parameters:
keyCode- the code of the key that was released- See Also:
-
setWhenKeyReleased
Sets the handle for when a key is released. This allows you to define custom behavior when a key is released.- Parameters:
whenKeyReleased- A KeyReleasedHandler that takes a Sprite and an
-
mouseEvent
public void mouseEvent(processing.event.MouseEvent e) Handles mouse events. Override this method to define custom behavior.- Parameters:
e- the MouseEvent object containing details about the mouse event
-
whenMouseMoved
public void whenMouseMoved(double x, double y) This method is called when the mouse is moved. Override this method to define custom behavior.- Parameters:
x- The x-coordinate of the mouse pointer.y- The y-coordinate of the mouse pointer.
-
setWhenMouseMoved
Sets the handler for when the mouse is moved. This allows you to define custom behavior when the mouse is moved.- Parameters:
whenMouseMoved- A MouseMovedHandler that takes a Sprite, x, and y
-
whenMouseClicked
This method is called when a mouse click event occurs. Override this method to define custom behavior.- Parameters:
mouseCode- The code representing the mouse button that was clicked.
-
setWhenMouseClicked
Sets the handler for when the mouse is clicked. This allows you to define custom behavior when the mouse is clicked.- Parameters:
whenMouseClicked- A MouseClickedHandler that takes a Sprite and a
-
whenClicked
public void whenClicked()This method is called when the sprite is clicked. Override this method to define custom behavior for the sprite when it is clicked. -
setWhenClicked
Sets the handler for when the sprite is clicked. This allows you to define custom behavior when the sprite is clicked.- Parameters:
whenClicked- A ClickedHandler that takes a Sprite as input and
-
goToRandomPosition
public void goToRandomPosition()Moves the sprite to a random position within the boundaries of the stage. The new position is determined by generating random coordinates within the width and height of the stage. -
goToMousePointer
public void goToMousePointer()Moves the sprite to the current position of the mouse pointer. This method updates the sprite's position to the coordinates of the mouse cursor. -
goToSprite
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
-
setWhenBackdropSwitches
Sets the handler for when the backdrop switches to a specified name.- Parameters:
whenBackdropSwitches- A BackdropSwitchHandler that takes a Sprite and
-
pickRandom
public int pickRandom(int from, int to) Returns a random integer between the specified range.- Parameters:
from- the lower bound of the range (inclusive)to- the upper bound of the range (exclusive)- Returns:
- a random integer between the specified range
-
getText
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.- Scratch Block:
- broadcast [message v]
-
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:
-
setWhenIReceive
Sets the handler for when a message is received. This allows you to define custom behavior. This is useful when you do not want to extend the sprite class and want to use the sprite class directly. For example:Sprite sprite = new Sprite(); sprite.setWhenIReceive((message) -> { System.out.println("Received message: " + message); });- Parameters:
whenIReceive- An IReceiveHandler that takes a Sprite and a message
-
stamp
public void stamp()Stamps the current sprite to the background. A stamp is a non interactive version of the sprite. -
stampToBackground
public void stampToBackground()Stamps the current sprite to the background. A stamp is a non interactive version of the sprite. -
stampToUI
public void stampToUI()Stamps the current sprite to the ui. A stamp is a non interactive version of the sprite. -
stampToForeground
public void stampToForeground()Stamps the current sprite to the foreground. A stamp is a non interactive version of the sprite. -
isUI
public void isUI(boolean isUI) Sets the UI status of the sprite.- Parameters:
isUI- A boolean value indicating whether the sprite is part of the UI.
-
isUI
public boolean isUI()Checks if the sprite is part of the user interface.- Returns:
- true if the sprite is part of the user interface, false otherwise.
-
getWindow
-
run
public void run()This method is intended to be overridden by subclasses to define the behavior of the sprite when it is run. By default, this method does nothing.It is called every frame.
-
setRun
Sets the run handler for the sprite. This allows you to define custom behavior when the sprite is run.- Parameters:
run- A RunHandler that takes a Sprite as input and defines the
-
addedToStage
-
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
-