Class Sprite
java.lang.Object
org.openpatch.scratch.Sprite
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
DrawingSprite
A sprite: an object on the stage with costumes, a position and a direction, and code that runs
every tick in
run(), like a sprite and its scripts in Scratch.
public class Cat extends Sprite {
public Cat() {
this.addCostume("cat", "assets/cat.png");
}
public void run() {
this.move(2);
this.ifOnEdgeBounce();
}
}
Positions are Scratch's: (0, 0) is the centre of the stage, x grows to the right and y
upwards. Directions are Scratch's too: 0 points up, 90 to the right (the start direction), 180
down, 270 to the left. Everything a sprite does happens in ticks of 1/60 s of game time, so
move(5) in run() moves 5 steps per tick on every computer.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleDirection pointing down (180 degrees).static final doubleDirection pointing left (270 degrees).static final doubleDirection pointing right (90 degrees), where a new sprite points.static final doubleDirection pointing up (0 degrees). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAnimation(String name, String pattern, int frames) Adds an animation from numbered image files: the pattern's%dcounts from 1.voidaddAnimation(String name, String pattern, int from, int to) Adds an animation from numbered image files, from one number to another: for files that count from 0, or to use only some of them.voidaddAnimation(String name, String sheetPath, int width, int height, int... frames) Adds an animation from a sprite sheet: frames of equal size, numbered row by row from the top left, starting at 0.voidaddCostume(String imagePath) Adds a costume from an image file, named after the file.voidaddCostume(String name, String imagePath) Adds a costume from an image file.voidaddCostume(String name, String imagePath, int x, int y, int width, int height) Adds a costume cut out of a larger image, e.g.voidaddCostumes(String name, String imagePath, int width, int height) Adds every frame of a sprite sheet as a costume, row by row, namedname0,name1, …voidAdds a sound, named after its file.voidAdds a sound (WAV or OGG).voidAsks a question: the sprite says it in a bubble, and a box to type the answer in appears at the bottom of the stage.voidSends a message to the stage and every sprite on it; theirwhenIReceive(String)runs at once, before this method returns.voidchangeColorEffect(double by) voidchangeHeight(double by) voidchangePosition(double dx, double dy) voidvoidchangeSize(double percent) voidchangeTransparency(double by) voidchangeVolume(double by) voidchangeWidth(double by) voidchangeX(double dx) voidchangeY(double dy) <T extends Sprite>
TMakes a clone: a copy of this sprite with the same costumes, position, direction, effects and variables, added to the stage just behind this sprite, as in Scratch.voidPrints values for debugging, but only while debug mode is on (window.setDebug(true)or F12).voidRemoves this sprite if it is a clone, stopping its sounds; does nothing for the original.voidTouching ignores this sprite untilenableHitbox().voidStretches the whole costume again.doubledoubledistanceToSprite(Sprite sprite) voidTouching counts this sprite again.<T> TAn extension's state for this sprite, made on first use; a clone gets its own.intdoubledoubleintdoubledoubleThe hitbox where the sprite is now, in stage coordinates: by default the rectangle around the costume's painted pixels; null for a sprite without costumes.getLayer()getMouse()doubledoublegetPen()doublegetSize()getStage()getText()getTimer()getTint()<T extends Sprite>
TgetTouchingSprite(Class<T> type) getTouchingSprites(Class<T> type) doubledoubledoublegetWidth()doublegetX()doublegetY()voidglide(double seconds, double x, double y) Glides to a position in a number of seconds of game time; the sprite keeps running its code meanwhile.voidgoLayersBackwards(int number) voidgoLayersForwards(int number) voidMoves the sprite behind every other sprite of its layer.voidMoves the sprite in front of every other sprite of its layer.voidGoes to the mouse pointer.voidGoes to a random position on the stage.voidgoToSprite(Sprite sprite) voidhide()Hides the sprite: it is not drawn, cannot be touched and cannot be clicked.voidhideVariable(String name) voidTurns around when touching an edge of the stage and moves back inside, as in Scratch.booleanbooleanisAsking()booleanisClone()booleanisColorTouchingColor(Color mine, Color color) Whether a colour of the sprite touches a colour on the stage, like Scratch's "color is touching color".booleanbooleanisKeyPressed(KeyCode key) booleanbooleanisSoundPlaying(String name) booleanisTouchingColor(double red, double green, double blue) booleanisTouchingColor(Color color) Whether the sprite touches a colour: a painted pixel of the sprite lies over that colour, as the stage shows it now (backdrop, stage colour, pen strokes, stamps and other sprites with their effects; not texts, shaders or the UI layer).booleanbooleanbooleanisTouchingSprite(Class<? extends Sprite> type) booleanisTouchingSprite(Sprite sprite) booleanvoidmove(double steps) Moves forward in the sprite's direction.voidSwitches to the next costume, after the last one to the first.intpickRandom(int from, int to) A random whole number between two numbers, both included.voidplayAnimation(String name) Plays an animation, over and over.voidplayAnimation(String name, boolean once) Plays an animation; call it every tick.voidStarts a sound, like Scratch's "start sound"; a sound that is still playing is not started again.playSoundCopy(String name, double volume) Starts another copy of a sound, which plays alongside the others: for shots, steps, coins.voidpointTowards(Sprite sprite) voidpointTowards(Vector2 position) Points towards a position.voidPoints towards the mouse pointer.voidSwitches to the previous costume, before the first one to the last.voidremove()Removes the sprite from its stage.voidStarts the current animation from its first frame again.voidrun()Runs every tick, 60 times a second of game time.voidShows a speech bubble until the sprite says something else; an empty text removes it.voidShows a speech bubble for a number of seconds of game time.voidsetAnimationFrame(int frame) voidsetAnimationInterval(double seconds) voidsetColorEffect(double value) Scratch's colour effect: shifts every colour of the costume around the colour wheel.voidsetDirection(double degrees) Points in a direction: 0 up, 90 right, 180 down, 270 left; other numbers go around.voidsetHeight(double height) Draws the sprite this high; seesetWidth(double).voidSets the hitbox: the shape that counts for touching, in costume pixels (origin at the costume's top left corner, y down).voidMoves the sprite to another layer, in front of the sprites already there.voidsetNineSlice(int top, int right, int bottom, int left) Stretches only the middle of the costume when the sprite is drawn at its own width and height: the four corners keep their size (scaled with the sprite's size), the edges stretch along one side.voidsetPosition(double x, double y) Goes to a position; with the pen down, it draws a line there.voidsetPosition(Vector2 position) voidsetRotationCenter(double x, double y) Sets the point of the costumes the sprite turns around and stands on, in costume pixels from the top left corner.voidsetRotationStyle(RotationStyle style) voidsetSize(double percent) voidsetTint(double red, double green, double blue) voidMultiplies the costume by a colour: white leaves it as it is, red keeps only its red.voidsetTransparency(double percent) Scratch's ghost effect.voidsetVolume(double volume) voidsetWidth(double width) Draws the sprite this wide, whatever its costume's width and size: for buttons, panels and platforms.voidsetX(double x) voidsetY(double y) voidshow()Shows the sprite.voidshowVariable(String name, Supplier<?> value) Shows a variable in a box at the top left of the stage, like a Scratch monitor; its value is read every frame.voidstamp()Stamps the costume onto the canvas of the sprite's pen layer (behind the sprites unless the pen was moved): a picture of the sprite as it looks now, effects included, that stays.voidStamps the costume onto the canvas of a layer.voidStops every sound this object started.voidvoidswitchCostume(double index) Switches to a costume by number, counting from 0; numbers wrap around like Scratch's.voidswitchCostume(String name) Switches to the costume with that name; an unknown name changes nothing.voidShows a thought bubble until the sprite thinks something else; an empty text removes it.voidShows a thought bubble for a number of seconds of game time.toString()The sprite's centre of the costume on the stage, as Scratch's "x position"/"y position".voidturnLeft(double degrees) voidturnRight(double degrees) voidCalled when the sprite was added to a stage.voidwhenBackdropSwitches(String name) Called when the stage switches its backdrop.voidCalled when this sprite is clicked: a mouse button is pressed over its hitbox, and no sprite in front of it is under the pointer.voidwhenIReceive(String message) Called when a message is broadcast.voidwhenKeyPressed(KeyCode key) Called when a key is pressed, once per press (holding a key down does not repeat it).voidwhenKeyReleased(KeyCode key) Called when a key is released.voidwhenMouseClicked(MouseCode button) Called when a mouse button is pressed anywhere on the stage.voidwhenMouseMoved(double x, double y) Called when the mouse moves.voidCalled when the sprite was removed from its stage.voidCalled in a new clone, right after it was made.
-
Field Details
-
DIRECTION_UP
public static final double DIRECTION_UPDirection pointing up (0 degrees).- See Also:
-
DIRECTION_RIGHT
public static final double DIRECTION_RIGHTDirection pointing right (90 degrees), where a new sprite points.- See Also:
-
DIRECTION_DOWN
public static final double DIRECTION_DOWNDirection pointing down (180 degrees).- See Also:
-
DIRECTION_LEFT
public static final double DIRECTION_LEFTDirection pointing left (270 degrees).- See Also:
-
-
Constructor Details
-
Sprite
public Sprite()A sprite without costumes. Add some withaddCostume(String, String). -
Sprite
-
-
Method Details
-
extension
-
whenAddedToStage
public void whenAddedToStage()Called when the sprite was added to a stage. Override it to react. -
whenRemovedFromStage
public void whenRemovedFromStage()Called when the sprite was removed from its stage. Override it to react. -
getStage
- Returns:
- the stage the sprite is on, or null
-
remove
public void remove()Removes the sprite from its stage. -
debug
Prints values for debugging, but only while debug mode is on (window.setDebug(true)or F12).- Parameters:
values- what to print
-
showVariable
Shows a variable in a box at the top left of the stage, like a Scratch monitor; its value is read every frame.this.showVariable("score", () -> this.score);- Parameters:
name- the labelvalue- gives the value to show- Scratch Block:
- show variable [name v]
-
hideVariable
- Parameters:
name- the label of a variable to hide again- Scratch Block:
- hide variable [name v]
-
addCostume
Adds a costume from an image file. The first costume added is the one the sprite wears.- Parameters:
name- the costume's name, forswitchCostume(String)imagePath- the image file (PNG, JPEG, GIF, BMP or TGA)
-
addCostume
Adds a costume from an image file, named after the file.- Parameters:
imagePath- the image file
-
addCostume
Adds a costume cut out of a larger image, e.g. one frame of a sprite sheet.- Parameters:
name- the costume's nameimagePath- the image filex- left edge of the part, in pixelsy- top edge of the part, in pixelswidth- width of the partheight- height of the part
-
addCostumes
-
switchCostume
Switches to the costume with that name; an unknown name changes nothing.- Parameters:
name- the costume's name- Scratch Block:
- switch costume to [name v]
-
switchCostume
public void switchCostume(double index) Switches to a costume by number, counting from 0; numbers wrap around like Scratch's.- Parameters:
index- the costume's number- Scratch Block:
- switch costume to [name v]
-
nextCostume
public void nextCostume()Switches to the next costume, after the last one to the first.- Scratch Block:
- next costume
-
previousCostume
public void previousCostume()Switches to the previous costume, before the first one to the last.- Scratch Block:
- switch costume to (previous costume)
-
getCurrentCostumeName
- Returns:
- the current costume's name, or null without costumes
- Scratch Block:
- (costume [name v])
-
getCurrentCostumeIndex
public int getCurrentCostumeIndex()- Returns:
- the current costume's number, counting from 0
- Scratch Block:
- (costume [number v])
-
addAnimation
Adds an animation from numbered image files: the pattern's%dcounts from 1.this.addAnimation("walk", "assets/walk%d.png", 4); // walk1.png … walk4.png- Parameters:
name- the animation's namepattern- the file names, with%dfor the frame numberframes- how many frames
-
addAnimation
Adds an animation from numbered image files, from one number to another: for files that count from 0, or to use only some of them. Iftois smaller thanfrom, it counts down.this.addAnimation("hit", "assets/hit%d.png", 0, 4); // hit0.png … hit4.png- Parameters:
name- the animation's namepattern- the file names, with%dfor the frame numberfrom- the number of the first frameto- the number of the last frame
-
addAnimation
Adds an animation from a sprite sheet: frames of equal size, numbered row by row from the top left, starting at 0.this.addAnimation("walk", "assets/walk.png", 32, 32, 0, 1, 2, 3); // the first row this.addAnimation("down", "assets/hero.png", 32, 32, 0, 4, 8, 12); // the first column of 4- Parameters:
name- the animation's namesheetPath- the sprite sheetwidth- the width of a frameheight- the height of a frameframes- the frames to show, in order
-
playAnimation
Plays an animation, over and over. Call it every tick (inrun()): it shows the next frame each time the animation interval has passed, and starts from the first frame when another animation was playing before.- Parameters:
name- the animation's name
-
playAnimation
Plays an animation; call it every tick.- Parameters:
name- the animation's nameonce- whether to stop at the last frame instead of starting over
-
setAnimationFrame
public void setAnimationFrame(int frame) - Parameters:
frame- the frame of the current animation to show next, counting from 0
-
resetAnimation
public void resetAnimation()Starts the current animation from its first frame again. -
setAnimationInterval
public void setAnimationInterval(double seconds) - Parameters:
seconds- how long each frame shows
-
getAnimationInterval
public double getAnimationInterval()- Returns:
- how long each frame shows, in seconds
-
getAnimationFrame
public int getAnimationFrame()- Returns:
- the frame of the current animation, counting from 0
-
isAnimationPlayed
public boolean isAnimationPlayed()- Returns:
- whether an animation played with
oncereached its last frame
-
setRotationCenter
public void setRotationCenter(double x, double y) Sets the point of the costumes the sprite turns around and stands on, in costume pixels from the top left corner. Without it, that is the centre of each costume.- Parameters:
x- pixels from the left edgey- pixels from the top edge
-
addSound
Adds a sound (WAV or OGG).- Parameters:
name- the sound's name, forplaySound(String)soundPath- the sound file
-
addSound
Adds a sound, named after its file.- Parameters:
soundPath- the sound file
-
playSound
Starts a sound, like Scratch's "start sound"; a sound that is still playing is not started again.- Parameters:
name- the sound's name- Scratch Block:
- start sound [name v]
-
playSoundCopy
Starts another copy of a sound, which plays alongside the others: for shots, steps, coins.- Parameters:
name- the sound's namevolume- the copy's volume from 0 to 100- Returns:
- the copy, to stop it or change its volume
-
stopSound
- Parameters:
name- the sound to stop
-
stopAllSounds
public void stopAllSounds()Stops every sound this object started.- Scratch Block:
- stop all sounds
-
isSoundPlaying
- Parameters:
name- a sound's name- Returns:
- whether it is playing
-
setVolume
public void setVolume(double volume) - Parameters:
volume- this object's volume from 0 to 100- Scratch Block:
- set volume to (percent) %
-
changeVolume
public void changeVolume(double by) - Parameters:
by- how much to change the volume- Scratch Block:
- change volume by (step)
-
getVolume
public double getVolume()- Returns:
- this object's volume from 0 to 100
- Scratch Block:
- (volume)
-
show
public void show()Shows the sprite.- Scratch Block:
- show
-
hide
public void hide()Hides the sprite: it is not drawn, cannot be touched and cannot be clicked.- Scratch Block:
- hide
-
isVisible
public boolean isVisible()- Returns:
- whether the sprite is shown
-
getSize
public double getSize()- Returns:
- the size in percent of the costume's own size
- Scratch Block:
- (size)
-
setSize
public void setSize(double percent) - Parameters:
percent- the size in percent of the costume's own size- Scratch Block:
- set size to (percentage) %
-
changeSize
public void changeSize(double percent) - Parameters:
percent- how much to change the size, in percentage points- Scratch Block:
- change size by (amount)
-
getWidth
public double getWidth()- Returns:
- the width the sprite is drawn at, in stage pixels
-
getHeight
public double getHeight()- Returns:
- the height the sprite is drawn at, in stage pixels
-
setWidth
public void setWidth(double width) Draws the sprite this wide, whatever its costume's width and size: for buttons, panels and platforms. WithsetNineSlice(int, int, int, int)the corners keep their size.- Parameters:
width- the width in stage pixels; 0 to follow the costume and size again
-
setHeight
public void setHeight(double height) Draws the sprite this high; seesetWidth(double).- Parameters:
height- the height in stage pixels; 0 to follow the costume and size again
-
changeWidth
public void changeWidth(double by) - Parameters:
by- how much wider to draw the sprite
-
changeHeight
public void changeHeight(double by) - Parameters:
by- how much higher to draw the sprite
-
setNineSlice
public void setNineSlice(int top, int right, int bottom, int left) Stretches only the middle of the costume when the sprite is drawn at its own width and height: the four corners keep their size (scaled with the sprite's size), the edges stretch along one side.- Parameters:
top- costume pixels at the top that keep their heightright- costume pixels at the right that keep their widthbottom- costume pixels at the bottom that keep their heightleft- costume pixels at the left that keep their width
-
disableNineSlice
public void disableNineSlice()Stretches the whole costume again. -
setColorEffect
public void setColorEffect(double value) Scratch's colour effect: shifts every colour of the costume around the colour wheel. 0 is no change; 100 is halfway round; 200 is all the way round again. Like Scratch, it gives colours too grey or too dark to show a hue a little one, so white and black are tinted too.- Parameters:
value- the effect- Scratch Block:
- set [color v] effect to (value)
-
changeColorEffect
public void changeColorEffect(double by) - Parameters:
by- how much to change the colour effect- Scratch Block:
- change [color v] effect by (by)
-
getColorEffect
public double getColorEffect()- Returns:
- the colour effect
-
setTint
Multiplies the costume by a colour: white leaves it as it is, red keeps only its red. Not a Scratch block: Scratch's colour effect issetColorEffect(double).- Parameters:
color- the tint, or null for none
-
setTint
public void setTint(double red, double green, double blue) - Parameters:
red- red from 0 to 255green- green from 0 to 255blue- blue from 0 to 255
-
getTint
- Returns:
- the tint, or null for none
-
setTransparency
public void setTransparency(double percent) Scratch's ghost effect.- Parameters:
percent- 0 opaque, 100 invisible- Scratch Block:
- set [ghost v] effect to (transparency)
-
changeTransparency
public void changeTransparency(double by) - Parameters:
by- how much to change the transparency- Scratch Block:
- change [ghost v] effect by (step)
-
getTransparency
public double getTransparency()- Returns:
- the transparency in percent
-
setLayer
-
getLayer
- Returns:
- the sprite's layer
-
goToFrontLayer
public void goToFrontLayer()Moves the sprite in front of every other sprite of its layer.- Scratch Block:
- go to [front v] layer
-
goToBackLayer
public void goToBackLayer()Moves the sprite behind every other sprite of its layer.- Scratch Block:
- go to [back v] layer
-
goLayersForwards
public void goLayersForwards(int number) - Parameters:
number- how many sprites of its layer to move in front of- Scratch Block:
- go [forward v] (number) layers
-
goLayersBackwards
public void goLayersBackwards(int number) - Parameters:
number- how many sprites of its layer to move behind- Scratch Block:
- go [backward v] (number) layers
-
getX
public double getX()- Returns:
- x of the sprite's position
- Scratch Block:
- (x position)
-
getY
public double getY()- Returns:
- y of the sprite's position
- Scratch Block:
- (y position)
-
getPosition
- Returns:
- the sprite's position
-
setPosition
public void setPosition(double x, double y) Goes to a position; with the pen down, it draws a line there.- Parameters:
x- x of the new positiony- y of the new position- Scratch Block:
- go to x: (x) y: (y)
-
setPosition
- Parameters:
position- the new position- Scratch Block:
- go to x: (x) y: (y)
-
setX
public void setX(double x) - Parameters:
x- the new x- Scratch Block:
- set x to (x)
-
setY
public void setY(double y) - Parameters:
y- the new y- Scratch Block:
- set y to (y)
-
changeX
public void changeX(double dx) - Parameters:
dx- how far to move along x- Scratch Block:
- change x by (x)
-
changeY
public void changeY(double dy) - Parameters:
dy- how far to move along y- Scratch Block:
- change y by (y)
-
changePosition
public void changePosition(double dx, double dy) - Parameters:
dx- how far to move along xdy- how far to move along y
-
changePosition
- Parameters:
by- how far to move
-
move
public void move(double steps) Moves forward in the sprite's direction.- Parameters:
steps- how far, in stage pixels (negative moves backwards)- Scratch Block:
- move (steps) steps
-
getDirection
public double getDirection()- Returns:
- the direction, from 0 (inclusive) to 360 (exclusive); 90 is right
- Scratch Block:
- (direction)
-
setDirection
public void setDirection(double degrees) Points in a direction: 0 up, 90 right, 180 down, 270 left; other numbers go around.- Parameters:
degrees- the direction- Scratch Block:
- point in direction (degrees)
-
turnRight
public void turnRight(double degrees) - Parameters:
degrees- how far to turn clockwise- Scratch Block:
- turn right (degrees) degrees
-
turnLeft
public void turnLeft(double degrees) - Parameters:
degrees- how far to turn anticlockwise- Scratch Block:
- turn left (degrees) degrees
-
pointTowards
Points towards a position.- Parameters:
position- where to point
-
pointTowards
- Parameters:
sprite- the sprite to point at
-
pointTowardsMousePointer
public void pointTowardsMousePointer()Points towards the mouse pointer.- Scratch Block:
- point towards [mouse-pointer v]
-
setRotationStyle
- Parameters:
style- how the costume follows the direction- Scratch Block:
- set rotation style [left-right v]
-
getRotationStyle
- Returns:
- how the costume follows the direction
-
goToRandomPosition
public void goToRandomPosition()Goes to a random position on the stage.- Scratch Block:
- go to [random position v]
-
goToMousePointer
public void goToMousePointer()Goes to the mouse pointer.- Scratch Block:
- go to [mouse-pointer v]
-
goToSprite
- Parameters:
sprite- the sprite whose position to go to- Scratch Block:
- go to [sprite v]
-
glide
public void glide(double seconds, double x, double y) Glides to a position in a number of seconds of game time; the sprite keeps running its code meanwhile. A new glide replaces one that is still going.- Parameters:
seconds- how long the glide takesx- x of the targety- y of the target- Scratch Block:
- glide (seconds) secs to x: (x) y: (y)
-
isGliding
public boolean isGliding()- Returns:
- whether a glide is still going
-
ifOnEdgeBounce
public void ifOnEdgeBounce()Turns around when touching an edge of the stage and moves back inside, as in Scratch.- Scratch Block:
- if on edge, bounce
-
getShaders
- Returns:
- the sprite's shaders
-
setHitbox
Sets the hitbox: the shape that counts for touching, in costume pixels (origin at the costume's top left corner, y down). It turns, grows and mirrors with the sprite.- Parameters:
shape- the hitbox
-
disableHitbox
public void disableHitbox()Touching ignores this sprite untilenableHitbox(). -
enableHitbox
public void enableHitbox()Touching counts this sprite again. -
getHitbox
The hitbox where the sprite is now, in stage coordinates: by default the rectangle around the costume's painted pixels; null for a sprite without costumes.- Returns:
- the hitbox, or null
-
isTouchingSprite
- Parameters:
sprite- another sprite- Returns:
- whether the two hitboxes overlap (touching edges do not count); a hidden sprite touches nothing, and the UI layer never touches the world
- Scratch Block:
- <touching [sprite v]?>
-
isTouchingSprite
-
getTouchingSprite
-
getTouchingSprites
-
isTouchingColor
Whether the sprite touches a colour: a painted pixel of the sprite lies over that colour, as the stage shows it now (backdrop, stage colour, pen strokes, stamps and other sprites with their effects; not texts, shaders or the UI layer). Colours match as in Scratch, with a small tolerance.- Parameters:
color- the colour to look for- Returns:
- whether the sprite touches it
- Scratch Block:
- <touching color [#ff0000]?>
-
isTouchingColor
public boolean isTouchingColor(double red, double green, double blue) - Parameters:
red- red from 0 to 255green- green from 0 to 255blue- blue from 0 to 255- Returns:
- whether the sprite touches that colour
- Scratch Block:
- <touching color [#ff0000]?>
-
isColorTouchingColor
Whether a colour of the sprite touches a colour on the stage, like Scratch's "color is touching color".- Parameters:
mine- a colour of the spritecolor- the colour to look for underneath- Returns:
- whether they touch
- Scratch Block:
- <color [#ff0000] is touching [#00ff00]?>
-
isTouchingMousePointer
public boolean isTouchingMousePointer()- Returns:
- whether the mouse pointer is over the sprite's hitbox
- Scratch Block:
- <touching [mouse-pointer v]?>
-
isTouchingEdge
public boolean isTouchingEdge()- Returns:
- whether the sprite's hitbox reaches an edge of the stage
- Scratch Block:
- <touching [edge v]?>
-
distanceToMousePointer
public double distanceToMousePointer()- Returns:
- the distance to the mouse pointer
- Scratch Block:
- (distance to [mouse-pointer v])
-
distanceToSprite
- Parameters:
sprite- another sprite- Returns:
- the distance between the two positions
- Scratch Block:
- (distance to [sprite v])
-
getTimer
- Returns:
- this object's timer, counting game time since it was made or reset
- Scratch Block:
- (timer)
-
getMouseX
public double getMouseX()- Returns:
- x of the mouse pointer, in the sprite's coordinates (world, or the stage for UI)
- Scratch Block:
- (mouse x)
-
getMouseY
public double getMouseY()- Returns:
- y of the mouse pointer
- Scratch Block:
- (mouse y)
-
getMouse
- Returns:
- the mouse pointer's position, in world coordinates (or stage coordinates for UI)
-
isMouseDown
public boolean isMouseDown()- Returns:
- whether a mouse button is held down
- Scratch Block:
- <mouse down?>
-
isKeyPressed
- Parameters:
key- a key- Returns:
- whether the key is held down
- Scratch Block:
- <key [space v] pressed?>
-
whenKeyPressed
Called when a key is pressed, once per press (holding a key down does not repeat it).- Parameters:
key- the key- Scratch Block:
- when [space v] key pressed
-
whenKeyReleased
-
whenMouseMoved
public void whenMouseMoved(double x, double y) Called when the mouse moves.- Parameters:
x- x of the mouse pointer (world)y- y of the mouse pointer (world)
-
whenMouseClicked
Called when a mouse button is pressed anywhere on the stage.- Parameters:
button- the button
-
whenClicked
public void whenClicked()Called when this sprite is clicked: a mouse button is pressed over its hitbox, and no sprite in front of it is under the pointer.- Scratch Block:
- when this sprite clicked
-
broadcast
Sends a message to the stage and every sprite on it; theirwhenIReceive(String)runs at once, before this method returns.- Parameters:
message- the message- Scratch Block:
- broadcast [message v]
-
whenIReceive
Called when a message is broadcast.- Parameters:
message- the message- Scratch Block:
- when I receive [message v]
-
whenBackdropSwitches
Called when the stage switches its backdrop.- Parameters:
name- the new backdrop's name- Scratch Block:
- when backdrop switches to [name v]
-
pickRandom
public int pickRandom(int from, int to) A random whole number between two numbers, both included.- Parameters:
from- the lowestto- the highest- Returns:
- the number
- Scratch Block:
- (pick random (from) to (to))
-
run
public void run()Runs every tick, 60 times a second of game time. Override it with the sprite's code. -
getText
- Returns:
- the sprite's text, which say and think show in a bubble
-
say
Shows a speech bubble until the sprite says something else; an empty text removes it.- Parameters:
text- what the sprite says- Scratch Block:
- say [text]
-
say
Shows a speech bubble for a number of seconds of game time.- Parameters:
text- what the sprite saysseconds- how long- Scratch Block:
- say [text] for (seconds) seconds
-
think
Shows a thought bubble until the sprite thinks something else; an empty text removes it.- Parameters:
text- what the sprite thinks- Scratch Block:
- think [text]
-
think
Shows a thought bubble for a number of seconds of game time.- Parameters:
text- what the sprite thinksseconds- how long- Scratch Block:
- think [text] for (seconds) seconds
-
ask
Asks a question: the sprite says it in a bubble, and a box to type the answer in appears at the bottom of the stage. Enter finishes it; seeStage.ask(String).- Parameters:
question- the question- Scratch Block:
- ask [question] and wait
-
isAsking
public boolean isAsking()- Returns:
- whether a question is waiting for its answer
-
getAnswer
- Returns:
- the answer to the last question
- Scratch Block:
- (answer)
-
createClone
Makes a clone: a copy of this sprite with the same costumes, position, direction, effects and variables, added to the stage just behind this sprite, as in Scratch. ItswhenStartsAsClone()runs; its constructor does not. Numbers, texts and booleans in its variables are its own; lists and other objects are shared with this sprite until the clone gets its own inwhenStartsAsClone().Bullet bullet = this.createClone();- Type Parameters:
T- the sprite's class (no cast needed)- Returns:
- the clone
-
whenStartsAsClone
public void whenStartsAsClone()Called in a new clone, right after it was made.- Scratch Block:
- when I start as a clone
-
isClone
public boolean isClone()- Returns:
- whether this sprite is a clone
-
deleteThisClone
public void deleteThisClone()Removes this sprite if it is a clone, stopping its sounds; does nothing for the original.- Scratch Block:
- delete this clone
-
getPen
- Returns:
- the sprite's pen, which follows it
-
stamp
public void stamp()Stamps the costume onto the canvas of the sprite's pen layer (behind the sprites unless the pen was moved): a picture of the sprite as it looks now, effects included, that stays.- Scratch Block:
- stamp
-
stamp
Stamps the costume onto the canvas of a layer.- Parameters:
layer- the layer- Scratch Block:
- stamp
-
toString
-