public class GSprite extends GObject
This class can be useful for creating objects in basic 2D games. It represents a basic in-game entity with a position and size that can move with a given velocity and acceleration around the screen. It can also collide with other sprites. A sprite can be drawn as either a bitmap image, a rectangle, or an oval.
Since we cannot possibly think of all possible attributes you would want to store inside your sprite objects, we provide an internal map of key/value pairs called 'extra' properties. You can call getExtra and setExtra to retrieve/store extra data inside any sprite.
| Constructor and Description |
|---|
GSprite()
Constructs a new empty sprite at (0, 0).
|
GSprite(ArrayList<Bitmap> bitmaps)
Constructs a new sprite at (0, 0) that displays the given bitmaps.
|
GSprite(ArrayList<Bitmap> bitmaps,
float x,
float y)
Constructs a new sprite at the given x/y location that displays the given bitmaps.
|
GSprite(Bitmap bitmap)
Constructs a new sprite at (0, 0) that displays the given bitmap.
|
GSprite(Bitmap bitmap,
float x,
float y)
Constructs a new sprite at the given x/y location that displays the given bitmap.
|
GSprite(Bitmap bitmap,
float x,
float y,
float width,
float height)
Constructs a new sprite at the given x/y location and size that displays the given bitmap.
|
GSprite(float x,
float y,
float width,
float height)
Constructs a new sprite at the given x/y location that displays a plain
rectangle of the given size.
|
GSprite(GObject object)
Constructs a new sprite at the given x/y location that displays the given graphical object.
|
| Modifier and Type | Method and Description |
|---|---|
void |
bound()
Moves this sprite so that it is within the bounds of its canvas.
|
void |
bound(Canvas canvas)
Moves this sprite so that it is within the bounds of the given canvas.
|
void |
bound(float leftX,
float topY,
float rightX,
float bottomY)
Moves this sprite so that it is within the bounds of the given x/y area.
|
void |
bound(RectF rect)
Moves this sprite so that it is within the bounds of the given rectangle.
|
void |
boundHorizontal()
Moves this sprite so that it is within the horizontal (x) bounds of its canvas.
|
void |
boundHorizontal(Canvas canvas)
Moves this sprite so that it is within the horizontal (x) bounds of the given canvas.
|
void |
boundHorizontal(float leftX,
float rightX)
Moves this sprite so that it is within the horizontal (x) bounds of the given x area.
|
void |
boundHorizontal(RectF rect)
Moves this sprite so that it is within the horizontal (x) bounds of the given rectangle.
|
void |
boundVertical()
Moves this sprite so that it is within the vertical (y) bounds of its canvas.
|
void |
boundVertical(Canvas canvas)
Moves this sprite so that it is within the vertical (y) bounds of the given canvas.
|
void |
boundVertical(float topY,
float bottomY)
Moves this sprite so that it is within the vertical (y) bounds of the given y area.
|
void |
boundVertical(RectF rect)
Moves this sprite so that it is within the vertical (y) bounds of the given rectangle.
|
void |
clearExtras()
Removes all extra properties inside this sprite.
|
boolean |
collidesWith(GSprite otherSprite)
Returns true if this sprite collides with the given other sprite.
|
void |
flipVelocity()
Inverts the sign of the sprite's velocity in both directions.
|
void |
flipVelocityX()
Inverts the sign of the sprite's velocity in the x direction.
|
void |
flipVelocityY()
Inverts the sign of the sprite's velocity in the y direction.
|
float |
getAccelerationX()
Returns this sprite's x acceleration; this will be 0 unless you have called setAcceleration.
|
float |
getAccelerationY()
Returns this sprite's yacceleration; this will be 0 unless you have called setAcceleration.
|
Bitmap |
getBitmap()
Returns the bitmap for this sprite as passed to the constructor.
|
ArrayList<Bitmap> |
getBitmaps()
Returns the list of bitmaps for this sprite.
|
float |
getBottomY()
Returns this sprite's bottom y-coordinate.
|
float |
getCollisionMarginBottom()
Returns this sprite's collision margin in the y direction.
|
float |
getCollisionMarginLeft()
Returns this sprite's collision margin in the x direction on the left side.
|
float |
getCollisionMarginRight()
Returns this sprite's collision margin in the x direction on the right side.
|
float |
getCollisionMarginTop()
Returns this sprite's collision margin in the y direction on top.
|
float |
getCollisionMarginX()
Returns this sprite's collision margin in the x direction.
|
float |
getCollisionMarginY()
Returns this sprite's collision margin in the y direction on top.
|
<T> T |
getExtra(String name)
Returns the extra property inside this sprite with the given name.
|
Bundle |
getExtras()
Returns all extras in this sprite as a Bundle.
|
int |
getFramesPerBitmap()
Returns the number of frames of animation that each bitmap
in the list should be displayed before automatically cycling
to the next frame bitmap.
|
float |
getHeight()
Returns this sprite's height.
|
float |
getLeftX()
Returns this sprite's leftmost x-coordinate.
|
float |
getRightX()
Returns this sprite's rightmost x-coordinate.
|
float |
getTopY()
Returns this sprite's top y-coordinate.
|
float |
getVelocityX()
Returns this sprite's velocity in the x direction.
|
float |
getVelocityY()
Returns this sprite's velocity in the y direction.
|
float |
getWidth()
Returns the sprite's width.
|
float |
getX()
Returns this sprite's leftmost x-coordinate.
|
float |
getY()
Returns this sprite's top y-coordinate.
|
boolean |
hasCollisionBoundary()
Returns true if you have set a collision margin.
|
boolean |
hasExtra(String name)
Returns whether this sprite has the given extra property inside it.
|
boolean |
intersects(GSprite otherSprite)
Returns true if this sprite collides with the given other sprite.
|
boolean |
isCollidable()
Returns whether this sprite can be collided with.
|
boolean |
isInBounds()
Returns whether this sprite is within the bounds of its canvas.
|
boolean |
isInBounds(Canvas canvas)
Returns whether this sprite is within the bounds of the given canvas.
|
boolean |
isInBounds(float leftX,
float topY,
float rightX,
float bottomY)
Returns whether this sprite is within the bounds of the given x/y area.
|
boolean |
isInBounds(RectF rect)
Returns whether this sprite is within the bounds of the given rectangle.
|
boolean |
isInBoundsHorizontal()
Returns whether this sprite is within the horizontal (x) bounds of its canvas.
|
boolean |
isInBoundsHorizontal(Canvas canvas)
Returns whether this sprite is within the horizontal (x) bounds of the given canvas.
|
boolean |
isInBoundsHorizontal(float leftX,
float rightX)
Returns whether this sprite is within the horizontal (x) bounds of the given x area.
|
boolean |
isInBoundsHorizontal(RectF rect)
Returns whether this sprite is within the horizontal (x) bounds of the given rectangle.
|
boolean |
isInBoundsVertical()
Returns whether this sprite is within the vertical (y) bounds of its canvas.
|
boolean |
isInBoundsVertical(Canvas canvas)
Returns whether this sprite is within the vertical (y) bounds of the given canvas.
|
boolean |
isInBoundsVertical(float topY,
float bottomY)
Returns whether this sprite is within the vertical (y) bounds of the given y area.
|
boolean |
isInBoundsVertical(RectF rect)
Returns whether this sprite is within the vertical (y) bounds of the given rectangle.
|
boolean |
isLoopBitmaps()
Returns whether the sequence of bitmaps (as passed to the constructor or setBitmaps)
should loop back to the first one after it ends.
|
boolean |
isMoving()
Returns whether this sprite has a non-zero velocity in any dimension.
|
boolean |
isVisible()
Returns whether the sprite is visible.
|
void |
paint(Canvas canvas)
Draws this sprite onto the given graphical canvas.
|
void |
putExtras(Bundle bundle)
Adds all extras from the given Bundle into this Sprite.
|
void |
removeExtra(String name)
Removes the extra property inside this sprite with the given name.
|
void |
rotateVelocity(float degrees)
Rotates this sprite's velocity in x and y dimension by the given angle in degrees, clockwise.
|
void |
setAcceleration(float accelerationX,
float accelerationY)
Sets the x and y acceleration of this sprite.
|
void |
setAccelerationX(float accelerationX)
Sets the x acceleration of this sprite.
|
void |
setAccelerationY(float accelerationY)
Sets the y acceleration of this sprite.
|
void |
setBitmap(Bitmap bitmap)
Sets this sprite to use the given bitmap to draw itself.
|
void |
setBitmaps(ArrayList<Bitmap> bitmaps)
Sets the list of bitmaps for this sprite.
|
void |
setBitmaps(Bitmap... bitmaps)
Sets the list of bitmaps for this sprite.
|
void |
setCollidable(boolean collidable)
Sets whether it is possible to collide with this sprite.
|
void |
setCollisionMargin(float px)
Sets a collision margin for this sprite.
|
void |
setCollisionMargin(float pxX,
float pxY)
Sets a collision margin for this sprite on all four sides.
|
void |
setCollisionMargin(float pxLeft,
float pxTop,
float pxRight,
float pxBottom)
Sets a collision margin for this sprite on all four sides.
|
void |
setCollisionMarginBottom(float pxBottom)
Sets a collision margin for this sprite on the bottom side.
|
void |
setCollisionMarginLeft(float pxLeft)
Sets a collision margin for this sprite on the left side only.
|
void |
setCollisionMarginRight(float pxRight)
Sets a collision margin for this sprite on the right side only.
|
void |
setCollisionMarginTop(float pxTop)
Sets a collision margin for this sprite on the top side.
|
void |
setCollisionMarginX(float pxX)
Sets a collision margin for this sprite on the left and right sides.
|
void |
setCollisionMarginX(float pxLeft,
float pxRight)
Sets a collision margin for this sprite on the left and right sides.
|
void |
setCollisionMarginY(float pxY)
Sets a collision margin for this sprite on the top and bottom sides.
|
void |
setCollisionMarginY(float pxTop,
float pxBottom)
Sets a collision margin for this sprite on the top and bottom sides.
|
int |
setCurrentBitmapIndex()
Returns the index of the bitmap currently being displayed
in this sprite's list of bitmaps.
|
void |
setCurrentBitmapIndex(int index)
Sets the index of the bitmap currently being displayed
in this sprite's list of bitmaps.
|
static void |
setDebug(boolean debug) |
void |
setExtra(String name,
Object value)
Sets the extra property inside this sprite with the given name.
|
void |
setFramesPerBitmap(int framesPerBitmap)
Sets the number of frames of animation that each bitmap
in the list should be displayed before automatically cycling
to the next frame bitmap.
|
void |
setLocation(float x,
float y)
Moves this sprite to the given x/y location.
|
void |
setLoopBitmaps(boolean loop)
Sets whether the sequence of bitmaps (as passed to the constructor or setBitmaps)
should loop back to the first one after it ends.
|
void |
setSize(float width,
float height)
Sets this sprite to use the given size.
|
void |
setVelocity(float dx,
float dy)
Sets this sprite to move with the given velocity.
|
void |
setVelocityX(float dx)
Sets this sprite to move with the given velocity in the x direction.
|
void |
setVelocityY(float dy)
Sets this sprite to move with the given velocity in the y direction.
|
void |
setVisible(boolean visible)
Sets whether this sprite is visible.
|
void |
stop()
Halts the object, setting its velocity and acceleration to 0.
|
String |
toString()
Returns a string representation of this sprite for debugging,
including its GObject if any, and its collision rectangle.
|
void |
update()
Called by SimpleCanvas every time the animation ticks.
|
contains, contains, getBounds, getCenterX, getCenterY, getColor, getGCanvas, getLocation, getPaint, getSize, intersects, moveBy, moveByPolar, moveTo, moveTo, moveToPolar, repaint, sendBackward, sendForward, sendToBack, sendToFront, setBottomY, setCanvas, setColor, setGCanvas, setLocation, setPaint, setRightX, setX, setY, translatepublic GSprite()
public GSprite(Bitmap bitmap)
public GSprite(ArrayList<Bitmap> bitmaps)
public GSprite(ArrayList<Bitmap> bitmaps, float x, float y)
public GSprite(Bitmap bitmap, float x, float y)
public GSprite(Bitmap bitmap, float x, float y, float width, float height)
public GSprite(GObject object)
public GSprite(float x,
float y,
float width,
float height)
public static void setDebug(boolean debug)
public boolean collidesWith(GSprite otherSprite)
public boolean intersects(GSprite otherSprite)
public boolean hasCollisionBoundary()
public void paint(Canvas canvas)
public void flipVelocityX()
public void flipVelocityY()
public void flipVelocity()
public void rotateVelocity(float degrees)
public float getAccelerationX()
public float getAccelerationY()
public float getX()
public float getLeftX()
public float getRightX()
public float getTopY()
public float getBottomY()
getBottomY in class GObjectpublic float getHeight()
public float getVelocityX()
public float getVelocityY()
public float getCollisionMarginX()
public float getCollisionMarginLeft()
public float getCollisionMarginRight()
public float getCollisionMarginY()
public float getCollisionMarginTop()
public float getCollisionMarginBottom()
public Bitmap getBitmap()
public ArrayList<Bitmap> getBitmaps()
public void setBitmaps(ArrayList<Bitmap> bitmaps)
public void setBitmaps(Bitmap... bitmaps)
public int setCurrentBitmapIndex()
public void setCurrentBitmapIndex(int index)
public int getFramesPerBitmap()
public void setFramesPerBitmap(int framesPerBitmap)
public boolean isCollidable()
public boolean isMoving()
public boolean isInBounds()
public boolean isInBounds(Canvas canvas)
public boolean isInBounds(RectF rect)
public boolean isInBounds(float leftX,
float topY,
float rightX,
float bottomY)
public boolean isInBoundsHorizontal()
public boolean isInBoundsHorizontal(Canvas canvas)
public boolean isInBoundsHorizontal(RectF rect)
public boolean isInBoundsHorizontal(float leftX,
float rightX)
public boolean isInBoundsVertical()
public boolean isInBoundsVertical(Canvas canvas)
public boolean isInBoundsVertical(RectF rect)
public boolean isInBoundsVertical(float topY,
float bottomY)
public void bound()
public void bound(Canvas canvas)
public void bound(RectF rect)
public void bound(float leftX,
float topY,
float rightX,
float bottomY)
public void boundHorizontal()
public void boundHorizontal(Canvas canvas)
public void boundHorizontal(RectF rect)
public void boundHorizontal(float leftX,
float rightX)
public void boundVertical()
public void boundVertical(Canvas canvas)
public void boundVertical(RectF rect)
public void boundVertical(float topY,
float bottomY)
public boolean isVisible()
public void setAccelerationX(float accelerationX)
public void setAccelerationY(float accelerationY)
public void setAcceleration(float accelerationX,
float accelerationY)
public void setCollisionMargin(float px)
public void setCollisionMargin(float pxX,
float pxY)
public void setCollisionMargin(float pxLeft,
float pxTop,
float pxRight,
float pxBottom)
public void setCollisionMarginX(float pxX)
public void setCollisionMarginLeft(float pxLeft)
public void setCollisionMarginRight(float pxRight)
public void setCollisionMarginX(float pxLeft,
float pxRight)
public void setCollisionMarginY(float pxY)
public void setCollisionMarginY(float pxTop,
float pxBottom)
public void setCollisionMarginTop(float pxTop)
public void setCollisionMarginBottom(float pxBottom)
public void setBitmap(Bitmap bitmap)
public void setCollidable(boolean collidable)
public void setLocation(float x,
float y)
setLocation in class GObjectpublic void setSize(float width,
float height)
public void setVisible(boolean visible)
setVisible in class GObjectpublic void setVelocity(float dx,
float dy)
public void setVelocityX(float dx)
public void setVelocityY(float dy)
public <T> T getExtra(String name)
public Bundle getExtras()
public void putExtras(Bundle bundle)
public boolean hasExtra(String name)
public void removeExtra(String name)
public void clearExtras()
public void setExtra(String name, Object value)
public void stop()
public String toString()
public void update()
public void setLoopBitmaps(boolean loop)
public boolean isLoopBitmaps()