public class Sprite extends Object
The Sprite class keeps track of a Sprite (image) and
its size, direction, position and hitbox. Through its methods, a Sprite can
easily resize, rotate, move and detect collision (with other Sprites or points on
the canvas). Coordinates for Sprites always use imageMode(CENTER)
and angles are always measured in degrees.
By default, a Sprite's direction faces
directly right, and it assumes that its image is also right-ward facing.
This can be adjusted using the frontAngle() function.
Here is a simple demo of a Sprite in action:
Sprite s = new Sprite("https://content.ktbyte.com/images/tank.png", 250, 200, 50, 50);
void setup() {
size(500, 400);
}
void draw() {
background(0, 0, 0);
s.display();
s.forward(1);
}
| Constructor and Description |
|---|
Sprite(float x,
float y,
float w,
float h)
Initializes the Sprite with the given coordinates and size to
a rectangle with a solid black color.
|
Sprite(Sprite s)
Makes a copy of the input Sprite, with identical image, coordinates and size.
|
Sprite(String url,
float x,
float y,
float w,
float h)
Initializes the Sprite with the given image URL, coordinates and size.
|
| Modifier and Type | Method and Description |
|---|---|
void |
display()
Draws the Sprite.
|
void |
displayHitbox()
Draws the Sprite's hitbox.
|
float |
distTo(Sprite s)
Calculates the distance from this Sprite to Sprite s
|
float |
distToPoint(float x,
float y)
Calculates the distance from this Sprite to the specified point
|
void |
flip()
Flips Sprite image across its X axis
|
void |
flipX()
Changes the direction of the Sprite by flipping
the x component of its direction
|
void |
flipY()
Changes the direction of the Sprite by flipping
the y component of its direction
|
void |
forward(float steps)
Moves the Sprite forward in the direction it is facing.
|
void |
frontAngle(float degrees)
Adjust the direction of the PImage of the Sprite
without changing its orientation
|
float |
getDir()
Gets the direction of the Sprite
|
float |
getH()
Gets the height of the Sprite
|
def.processing.core.PImage |
getImage()
Gets the image of the Sprite
|
float |
getW()
Gets the width of the Sprite
|
float |
getX()
Gets the x coordinate of the Sprite
|
float |
getY()
Gets the y coordinate of the Sprite
|
static void |
init(def.processing.core.PApplet pa) |
boolean |
insideSprite(Sprite s)
Checks to see if this Sprite is fully inside another sprite.
|
boolean |
isInsideScreen()
Checks whether this Sprite's hitbox is at least partially inside the canvas
|
void |
moveToPoint(float x,
float y)
Moves the Sprite to the specified coordinate.
|
void |
moveToSprite(Sprite s)
Moves the Sprite to the specified Sprite s
|
void |
moveX(float x)
Moves the Sprite in the x axis by the specified amount.
|
void |
moveXY(float dx,
float dy)
Moves the Sprite in the x and y axes by the specified amounts.
|
void |
moveY(float y)
Moves the Sprite in the y axis by the specified amount.
|
void |
resetHitboxCenter()
Recenter the Sprite's hitbox to its center
|
void |
resetRectHitbox()
Resets the Sprite's hitbox to its own width and height
|
void |
resetRoundHitbox()
Sets the Sprite's hitbox to a circle with radius
equals to (sum of its width and height) / 4.
|
void |
setColor(float r,
float g,
float b)
Changes the color of a Sprite.
|
void |
setCoor(float x,
float y)
Moves the Sprite to the specified coordinate.
|
void |
setHitboxCenter(float x,
float y)
Recenter the Sprite's hitbox relative to its center
|
void |
setHitboxPoints(def.processing.core.PVector[] array)
Set the Sprite's hitbox to a polygon defined by
a list of PVectors (points) relative to its center
|
void |
setImage(def.processing.core.PImage img)
Changes the image of the Sprite
|
void |
setRectHitbox(float w,
float h)
Adjusts the Sprite's hitbox, which is set to the Sprite's
width and height by default.
|
void |
setRoundHitbox(float r)
Sets the Sprite's hitbox to a circle.
|
void |
setSize(float w,
float h)
Sets the size of the Sprite.
|
void |
setX(float x) |
void |
setY(float y) |
void |
sideStep(float steps)
Moves the Sprite in the direction 90 degrees clockwise from its forward direction.
|
boolean |
touchingPoint(float x,
float y)
Checks whether this Sprite is touching the specified point.
|
boolean |
touchingSprite(Sprite s)
Checks to see if this Sprite is touching Sprite s.
|
void |
turn(float degrees)
Rotates the Sprite by a specified number of degrees
|
void |
turnToDir(float angle)
Rotates the Sprite to specified (absolute) angle.
|
void |
turnToPoint(float x,
float y)
Rotates the Sprite to face the specified (x, y) location
|
void |
turnToSprite(Sprite s)
Rotates the Sprite to the specified Sprite s
|
public Sprite(String url, float x, float y, float w, float h)
url - The URL of the image for the Sprite to use.x - The initial x coordinate of the Spritey - The initial y coordinate of the Spritew - The initial width of the Spriteh - The initial height of the Spritepublic Sprite(float x,
float y,
float w,
float h)
setColor() functionx - The initial x coordinate of the Spritey - The initial y coordinate of the Spritew - The initial width of the Spriteh - The initial height of the Spritepublic Sprite(Sprite s)
s - The Sprite to copy from.public static void init(def.processing.core.PApplet pa)
public void frontAngle(float degrees)
degrees - The degrees of turn to adjust the PImagepublic void setRectHitbox(float w,
float h)
w - The width of the hitboxh - The height of the hitboxpublic void resetRectHitbox()
public void setRoundHitbox(float r)
r - The radius of the circlepublic void resetRoundHitbox()
public void setHitboxCenter(float x,
float y)
x - the offset in the x direction, in pixelsy - the offset in the y direction, in pixelspublic void resetHitboxCenter()
public void setHitboxPoints(def.processing.core.PVector[] array)
array - An array of points definig the hitbox polygonpublic void setColor(float r,
float g,
float b)
r - The first parameter defining the color.
In default RGB mode, this is the red value.g - The second parameter defining the color.
In default RGB mode, this is the green value.b - The third parameter defining the color.
In default RGB mode, this is the blue value.public void flip()
public void turn(float degrees)
degrees - The number of degrees to turnpublic void turnToPoint(float x,
float y)
x - The x coordinate of the point to facey - The y coordinate of the point to facepublic void turnToDir(float angle)
angle - The direction to turn topublic void turnToSprite(Sprite s)
s - The Sprite to turn topublic void moveToPoint(float x,
float y)
x - The x coordinate of the point to move toy - The y coordinate of the point to move topublic void moveToSprite(Sprite s)
s - The Sprite to move topublic void moveX(float x)
x - The amount to move in pixelspublic void moveY(float y)
y - The amount to move in pixelspublic void moveXY(float dx,
float dy)
x - The amount to move in pixels in the x axisy - The amount to move in pixels in the y axispublic void forward(float steps)
steps - The amount to move in pixels.public void sideStep(float steps)
steps - The amount to move in pixels.public void display()
void draw() function.public void displayHitbox()
void draw() function.public void flipX()
public void flipY()
public void setSize(float w,
float h)
w - The width of the Spriteh - The height of the Spritepublic void setCoor(float x,
float y)
x - The x coordinate of the point to move toy - The y coordinate of the point to move topublic void setX(float x)
public void setY(float y)
public void setImage(def.processing.core.PImage img)
img - The image to set the Sprite topublic float getX()
public float getY()
public float getW()
public float getH()
public def.processing.core.PImage getImage()
public float getDir()
public float distTo(Sprite s)
s - The Sprite to measure distance topublic float distToPoint(float x,
float y)
x - The x coordinate of the pointy - The y coordinate of the pointpublic boolean touchingSprite(Sprite s)
s - The Sprite to detect whethet this Sprite is touchingpublic boolean insideSprite(Sprite s)
s - The Sprite to detect whethet this Sprite is touchingpublic boolean touchingPoint(float x,
float y)
x - The x coordinate of the pointy - The y coordinate of the pointpublic boolean isInsideScreen()
Copyright © 2019 JSweet. All rights reserved.