public class Sprite extends GameObject
GameObject
that has an image associated with it, and a
location on the screen to draw that image (left x, upper y, width, height).
A Sprite has no behaviors on its own. Rather than using a Sprite directly, in most cases you will create a subclass of Sprite that has the behaviors you need.
All coordinates and width/height are in world units.
Modifier and Type | Field and Description |
---|---|
protected android.graphics.Bitmap |
mImage
Stores the bitmap image to be displayed by this sprite
|
protected int |
mImageId
Holds the resource ID (R.drawable.* or R.raw.*) for this sprite's image
|
boundingRect, manager, name
Constructor and Description |
---|
Sprite(java.lang.String name,
float x,
float y,
float width,
float height)
Returns a Sprite game object with the given name and location/size on the screen.
|
Sprite(java.lang.String name,
float x,
float y,
float width,
float height,
int image_id)
Returns a Sprite game object with the given name and location/size on the screen and
the given image.
|
Modifier and Type | Method and Description |
---|---|
void |
draw(android.graphics.Canvas c,
float xScale,
float yScale)
Called by the Game Engine to draw this sprite onto the screen every frame.
|
void |
loadImage(int id)
Configures the Sprite to display the image with the given id.
|
void |
onFling(float x,
float y,
float dx,
float dy)
Called when the user makes a fling motion on the screen, starting at a location inside this
sprite's bounding box.
|
void |
onTouch(float x,
float y)
Called when the user taps the screen at a location inside this sprite's bounding box.
|
void |
update(int msec)
Called once per screen refresh to do whatever modifications are desired to the sprite's
location, size, etc.
|
contains, getTimeOnScreen, hop, hopToward, intersects, isFullyOffScreen, isFullyOnScreen, isInside, moveBy, removalRequested, requestRemoval, setManager, setMaxTimeOnScreen
protected int mImageId
protected android.graphics.Bitmap mImage
public Sprite(java.lang.String name, float x, float y, float width, float height)
loadImage(int)
is called.name
- GameObject name for this spritex
- left edge of the spritey
- top edge of the spritewidth
- width of the spriteheight
- height of the spritepublic Sprite(java.lang.String name, float x, float y, float width, float height, int image_id)
name
- GameObject name for this spritex
- left edge of the spritey
- top edge of the spritewidth
- width of the spriteheight
- height of the spriteimage_id
- the ID of the image to display (eg R.id.my_sprite
)public void loadImage(int id)
id
- the ID of the image to display (eg R.id.my_sprite)public void onTouch(float x, float y)
onTouch
in class GameObject
x
- horizontal coordinate in world unitsy
- vertical coordinate in world unitspublic void onFling(float x, float y, float dx, float dy)
onFling
in class GameObject
x
- horizontal coordinate in world unitsy
- vertical coordinate in world unitspublic void update(int msec)
By default, sprites have no behavior - they just sit in one place. Override this method if desired to make your sprite move.
update
in class GameObject
msec
- number of milliseconds since the last screen update.public void draw(android.graphics.Canvas c, float xScale, float yScale)
draw
in class GameObject
c
- Canvas object provided by the operating systemxScale
- horizontal scale factor between world and screen coordinatesyScale
- horizontal scale factor between world and screen coordinates