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 or a GObject such as a rectangle/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(GCanvas canvas)
Constructs a new empty sprite at (0, 0).
|
GSprite(GCanvas canvas,
ArrayList<Bitmap> bitmaps)
Constructs a new sprite at (0, 0) that displays the given bitmaps.
|
GSprite(GCanvas canvas,
ArrayList<Bitmap> bitmaps,
float x,
float y)
Constructs a new sprite at the given x/y location that displays the given bitmaps.
|
GSprite(GCanvas canvas,
Bitmap bitmap)
Constructs a new sprite at (0, 0) that displays the given bitmap.
|
GSprite(GCanvas canvas,
Bitmap bitmap,
float x,
float y)
Constructs a new sprite at the given x/y location that displays the given bitmap.
|
GSprite(GCanvas canvas,
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(GCanvas canvas,
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(GCanvas canvas,
GObject object)
Constructs a new sprite at the given x/y location that displays the given graphical object.
|
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 |
|---|---|
GSprite |
addTo(GCanvas canvas)
Adds this sprite to the given graphical canvas.
|
GSprite |
addTo(GCanvas canvas,
float x,
float y)
Adds this sprite to the given graphical canvas.
|
GSprite |
addTo(GCanvas canvas,
GPoint location)
Adds this GObject to the given graphical canvas.
|
GSprite |
bound()
Moves this sprite so that it is within the bounds of its canvas.
|
GSprite |
bound(Canvas canvas)
Moves this sprite so that it is within the bounds of the given canvas.
|
GSprite |
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.
|
GSprite |
bound(RectF rect)
Moves this sprite so that it is within the bounds of the given rectangle.
|
GSprite |
boundHorizontal()
Moves this sprite so that it is within the horizontal (x) bounds of its canvas.
|
GSprite |
boundHorizontal(Canvas canvas)
Moves this sprite so that it is within the horizontal (x) bounds of the given canvas.
|
GSprite |
boundHorizontal(float leftX,
float rightX)
Moves this sprite so that it is within the horizontal (x) bounds of the given x area.
|
GSprite |
boundHorizontal(RectF rect)
Moves this sprite so that it is within the horizontal (x) bounds of the given rectangle.
|
GSprite |
boundVertical()
Moves this sprite so that it is within the vertical (y) bounds of its canvas.
|
GSprite |
boundVertical(Canvas canvas)
Moves this sprite so that it is within the vertical (y) bounds of the given canvas.
|
GSprite |
boundVertical(float topY,
float bottomY)
Moves this sprite so that it is within the vertical (y) bounds of the given y area.
|
GSprite |
boundVertical(RectF rect)
Moves this sprite so that it is within the vertical (y) bounds of the given rectangle.
|
GSprite |
clearExtras()
Removes all extra properties inside this sprite.
|
boolean |
collidesWith(GSprite otherSprite)
Returns true if this sprite collides with the given other sprite.
|
GSprite |
flipVelocity()
Inverts the sign of the sprite's velocity in both directions.
|
GSprite |
flipVelocityX()
Inverts the sign of the sprite's velocity in the x direction.
|
GSprite |
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 |
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.
|
int |
getCurrentBitmapIndex()
Returns the index of the bitmap currently being displayed
in this sprite's list of bitmaps.
|
<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.
|
<T extends GObject> |
getShape()
Returns the GObject that was passed to this GSprite's constructor, if any.
|
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.
|
GSprite |
moveTo(float x,
float y)
Moves the sprite on the screen to the given x/y position.
|
GSprite |
moveTo(GPoint point)
Moves the sprite on the screen to the given point's x/y position.
|
void |
paint(Canvas canvas)
Draws this sprite onto the given graphical canvas.
|
GSprite |
putExtra(String name,
Object value)
Sets the extra property inside this sprite with the given name.
|
GSprite |
putExtras(Bundle bundle)
Adds all extras from the given Bundle into this Sprite.
|
GSprite |
remove()
Removes this sprite from its graphical canvas, if it is added to one.
|
GSprite |
removeExtra(String name)
Removes the extra property inside this sprite with the given name.
|
GSprite |
rotateVelocity(float degrees)
Rotates this sprite's velocity in x and y dimension by the given angle in degrees, clockwise.
|
GSprite |
scale(float sf)
Scales the sprite on the screen by the scale factor
sf, which applies
in both dimensions. |
GSprite |
scale(float sx,
float sy)
Scales the object on the screen by the scale factors
sx and sy. |
GSprite |
setAcceleration(float accelerationX,
float accelerationY)
Sets the x and y acceleration of this sprite.
|
GSprite |
setAccelerationX(float accelerationX)
Sets the x acceleration of this sprite.
|
GSprite |
setAccelerationY(float accelerationY)
Sets the y acceleration of this sprite.
|
GSprite |
setBitmap(Bitmap bitmap)
Sets this sprite to use the given bitmap to draw itself.
|
GSprite |
setBitmaps(ArrayList<Bitmap> bitmaps)
Sets the list of bitmaps for this sprite.
|
GSprite |
setBitmaps(Bitmap... bitmaps)
Sets the list of bitmaps for this sprite.
|
GSprite |
setBounds(float x,
float y,
float width,
float height)
Changes the bounds of this object to the specified values.
|
GSprite |
setBounds(GPoint location,
GDimension size)
Changes the bounds of this object to the specified values.
|
GSprite |
setCollidable(boolean collidable)
Sets whether it is possible to collide with this sprite.
|
GSprite |
setCollisionMargin(float px)
Sets a collision margin for this sprite.
|
GSprite |
setCollisionMargin(float pxX,
float pxY)
Sets a collision margin for this sprite on all four sides.
|
GSprite |
setCollisionMargin(float pxLeft,
float pxTop,
float pxRight,
float pxBottom)
Sets a collision margin for this sprite on all four sides.
|
GSprite |
setCollisionMarginBottom(float pxBottom)
Sets a collision margin for this sprite on the bottom side.
|
GSprite |
setCollisionMarginLeft(float pxLeft)
Sets a collision margin for this sprite on the left side only.
|
GSprite |
setCollisionMarginRight(float pxRight)
Sets a collision margin for this sprite on the right side only.
|
GSprite |
setCollisionMarginTop(float pxTop)
Sets a collision margin for this sprite on the top side.
|
GSprite |
setCollisionMarginX(float pxX)
Sets a collision margin for this sprite on the left and right sides.
|
GSprite |
setCollisionMarginX(float pxLeft,
float pxRight)
Sets a collision margin for this sprite on the left and right sides.
|
GSprite |
setCollisionMarginY(float pxY)
Sets a collision margin for this sprite on the top and bottom sides.
|
GSprite |
setCollisionMarginY(float pxTop,
float pxBottom)
Sets a collision margin for this sprite on the top and bottom sides.
|
GSprite |
setColor(Paint paint)
Sets the color used to display this sprite.
|
GSprite |
setCurrentBitmapIndex(int index)
Sets the index of the bitmap currently being displayed
in this sprite's list of bitmaps.
|
static void |
setDebug(boolean debug)
Sets a global "debug" flag that will cause some log messages to be printed,
as well as drawing a light-green "collision rectangle" around all sprites
to help see when they will collide.
|
GSprite |
setExtra(String name,
Object value)
Sets the extra property inside this sprite with the given name.
|
GSprite |
setExtras(Bundle bundle)
Adds all extras from the given Bundle into this Sprite.
|
GSprite |
setFillColor(Paint color)
Sets the color used to display the filled region of this object.
|
GSprite |
setFilled(boolean fill)
Sets whether this object is filled.
|
GSprite |
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.
|
GSprite |
setGCanvas(GCanvas gcanvas)
Sets the canvas on which this GObject should be drawn.
|
GSprite |
setLocation(float x,
float y)
Moves this sprite to the given x/y location.
|
GSprite |
setLocation(GObject gobj)
Moves this GObject to have the same location as the given other GObject.
|
GSprite |
setLocation(GPoint point)
Sets the location of this object to the given point's (x, y) location.
|
GSprite |
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.
|
GSprite |
setPaint(Paint paint)
Sets the color used to display this object.
|
GSprite |
setShape(GObject object)
Sets the GObject for this GSprite to draw.
|
GSprite |
setSize(float width,
float height)
Sets this sprite to use the given size.
|
GSprite |
setSize(GDimension size)
Changes the size of this sprite to the specified
GDimension. |
GSprite |
setVelocity(float dx,
float dy)
Sets this sprite to move with the given velocity.
|
GSprite |
setVelocityX(float dx)
Sets this sprite to move with the given velocity in the x direction.
|
GSprite |
setVelocityY(float dy)
Sets this sprite to move with the given velocity in the y direction.
|
GSprite |
setVisible(boolean visible)
Sets whether this object is visible.
|
GSprite |
setX(float x)
Sets the x-location of this object.
|
GSprite |
setY(float y)
Sets the y-location of this object.
|
GSprite |
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.
|
GSprite |
translate(float dx,
float dy)
Moves the object on the screen using the displacements dx and dy.
|
void |
update()
Called by SimpleCanvas every time the animation ticks.
|
contains, contains, getBottomY, getBounds, getCenterX, getCenterY, getColor, getFillColor, getGCanvas, getLeftX, getLocation, getPaint, getRightX, getSize, getTopY, intersects, isFilled, isVisible, moveBy, moveByPolar, moveTo, moveToPolar, repaint, sendBackward, sendForward, sendToBack, sendToFront, setBottomY, setBounds, setCanvas, setRightXpublic 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 GSprite(GCanvas canvas)
public GSprite(GCanvas canvas, Bitmap bitmap)
public GSprite(GCanvas canvas, ArrayList<Bitmap> bitmaps)
public GSprite(GCanvas canvas, ArrayList<Bitmap> bitmaps, float x, float y)
public GSprite(GCanvas canvas, Bitmap bitmap, float x, float y)
public GSprite(GCanvas canvas, Bitmap bitmap, float x, float y, float width, float height)
public GSprite(GCanvas canvas, GObject object)
public GSprite(GCanvas canvas, float x, float y, float width, float height)
public static void setDebug(boolean debug)
public GSprite addTo(GCanvas canvas)
public GSprite addTo(GCanvas canvas, float x, float y)
public GSprite addTo(GCanvas canvas, GPoint location)
public GSprite bound()
public GSprite bound(Canvas canvas)
public GSprite bound(RectF rect)
public GSprite bound(float leftX, float topY, float rightX, float bottomY)
public GSprite boundHorizontal()
public GSprite boundHorizontal(Canvas canvas)
public GSprite boundHorizontal(RectF rect)
public GSprite boundHorizontal(float leftX, float rightX)
public GSprite boundVertical()
public GSprite boundVertical(Canvas canvas)
public GSprite boundVertical(RectF rect)
public GSprite boundVertical(float topY, float bottomY)
public GSprite clearExtras()
public boolean collidesWith(GSprite otherSprite)
public GSprite flipVelocity()
public GSprite flipVelocityX()
public GSprite flipVelocityY()
public float getAccelerationX()
public float getAccelerationY()
public Bitmap getBitmap()
public ArrayList<Bitmap> getBitmaps()
public float getCollisionMarginBottom()
public float getCollisionMarginLeft()
public float getCollisionMarginRight()
public float getCollisionMarginTop()
public float getCollisionMarginX()
public float getCollisionMarginY()
public int getCurrentBitmapIndex()
public <T> T getExtra(String name)
public Bundle getExtras()
public int getFramesPerBitmap()
public float getHeight()
public <T extends GObject> T getShape()
public float getVelocityX()
public float getVelocityY()
public float getX()
public boolean hasCollisionBoundary()
public boolean hasExtra(String name)
public boolean intersects(GSprite otherSprite)
public boolean isCollidable()
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 boolean isLoopBitmaps()
public boolean isMoving()
public GSprite moveTo(float x, float y)
public GSprite moveTo(GPoint point)
public void paint(Canvas canvas)
public GSprite putExtra(String name, Object value)
public GSprite putExtras(Bundle bundle)
public GSprite remove()
public GSprite removeExtra(String name)
public GSprite rotateVelocity(float degrees)
public GSprite scale(float sf)
sf, which applies
in both dimensions.scale in class GObjectsf - The factor used to scale all coordinates in both dimensionsIllegalArgumentException - if sf is not a positive numberpublic GSprite scale(float sx, float sy)
sx and sy.scale in class GObjectsx - The factor used to scale all coordinates in the x directionsy - The factor used to scale all coordinates in the y directionIllegalArgumentException - if sx or sy is not a positive numberpublic GSprite setAccelerationX(float accelerationX)
public GSprite setAccelerationY(float accelerationY)
public GSprite setAcceleration(float accelerationX, float accelerationY)
public GSprite setBitmap(Bitmap bitmap)
public GSprite setBitmaps(ArrayList<Bitmap> bitmaps)
public GSprite setBounds(float x, float y, float width, float height)
setBounds in class GObjectx - The new x-coordinate for the objecty - The new y-coordinate for the objectwidth - The new width of the objectheight - The new height of the objectIllegalArgumentException - if width or height is negativepublic GSprite setBounds(GPoint location, GDimension size)
setBounds in class GObjectlocation - The new x/y-coordinates for the objectsize - The new width/height of the objectIllegalArgumentException - if width or height is negativepublic GSprite setCollidable(boolean collidable)
public GSprite setCollisionMargin(float px)
public GSprite setCollisionMargin(float pxX, float pxY)
public GSprite setCollisionMargin(float pxLeft, float pxTop, float pxRight, float pxBottom)
public GSprite setCollisionMarginBottom(float pxBottom)
public GSprite setCollisionMarginLeft(float pxLeft)
public GSprite setCollisionMarginRight(float pxRight)
public GSprite setCollisionMarginTop(float pxTop)
public GSprite setCollisionMarginX(float pxX)
public GSprite setCollisionMarginX(float pxLeft, float pxRight)
public GSprite setCollisionMarginY(float pxY)
public GSprite setCollisionMarginY(float pxTop, float pxBottom)
public GSprite setColor(Paint paint)
setColor in class GObjectNullPointerException - if paint is null.public GSprite setCurrentBitmapIndex(int index)
public GSprite setExtra(String name, Object value)
public GSprite setExtras(Bundle bundle)
public GSprite setFillColor(Paint color)
setFillColor in class GObjectcolor - The color used to display the filled region of this objectNullPointerException - if color is nullpublic GSprite setFilled(boolean fill)
public GSprite setFramesPerBitmap(int framesPerBitmap)
public GSprite setGCanvas(GCanvas gcanvas)
setGCanvas in class GObjectpublic GSprite setLocation(float x, float y)
setLocation in class GObjectpublic GSprite setLocation(GObject gobj)
setLocation in class GObjectNullPointerException - if gobj is null.public GSprite setLocation(GPoint point)
setLocation in class GObjectpublic GSprite setLoopBitmaps(boolean loop)
public GSprite setPaint(Paint paint)
setPaint in class GObjectNullPointerException - if paint is null.public GSprite setSize(float width, float height)
public GSprite setSize(GDimension size)
GDimension.setSize in class GObjectsize - A GDimension object specifying the sizeNullPointerException - if size is nullpublic GSprite setVelocity(float dx, float dy)
public GSprite setVelocityX(float dx)
public GSprite setVelocityY(float dy)
public GSprite setVisible(boolean visible)
setVisible in class GObjectpublic GSprite setX(float x)
public GSprite setY(float y)
public GSprite stop()
public String toString()
public GSprite translate(float dx, float dy)
public void update()