public abstract class Sprite extends VisibleComponent implements AlarmHandler, OnDestroyListener, Deleteable
Modifier and Type | Field and Description |
---|---|
protected Canvas |
canvas |
protected double |
heading
The angle, in degrees below the positive x-axis, specified by the
user.
|
protected double |
headingCos |
protected double |
headingRadians |
protected double |
headingSin |
protected boolean |
initialized |
protected int |
interval |
protected float |
speed |
protected double |
userHeading
The angle, in degrees above the positive x-axis, specified by the user.
|
protected boolean |
visible |
protected double |
xLeft |
protected double |
yTop |
protected double |
zLayer |
ACCELEROMETER_SENSITIVITY_MODERATE, ACCELEROMETER_SENSITIVITY_STRONG, ACCELEROMETER_SENSITIVITY_WEAK, ALIGNMENT_CENTER, ALIGNMENT_NORMAL, ALIGNMENT_OPPOSITE, ASSET_DIRECTORY, BUTTON_SHAPE_DEFAULT, BUTTON_SHAPE_OVAL, BUTTON_SHAPE_RECT, BUTTON_SHAPE_ROUNDED, COLOR_BLACK, COLOR_BLUE, COLOR_CYAN, COLOR_DEFAULT, COLOR_DKGRAY, COLOR_GRAY, COLOR_GREEN, COLOR_LTGRAY, COLOR_MAGENTA, COLOR_NONE, COLOR_ORANGE, COLOR_PINK, COLOR_RED, COLOR_WHITE, COLOR_YELLOW, DEFAULT_VALUE_COLOR_BLACK, DEFAULT_VALUE_COLOR_BLUE, DEFAULT_VALUE_COLOR_CYAN, DEFAULT_VALUE_COLOR_DEFAULT, DEFAULT_VALUE_COLOR_DKGRAY, DEFAULT_VALUE_COLOR_GRAY, DEFAULT_VALUE_COLOR_GREEN, DEFAULT_VALUE_COLOR_LTGRAY, DEFAULT_VALUE_COLOR_MAGENTA, DEFAULT_VALUE_COLOR_NONE, DEFAULT_VALUE_COLOR_ORANGE, DEFAULT_VALUE_COLOR_PINK, DEFAULT_VALUE_COLOR_RED, DEFAULT_VALUE_COLOR_WHITE, DEFAULT_VALUE_COLOR_YELLOW, DIRECTION_EAST, DIRECTION_MAX, DIRECTION_MIN, DIRECTION_NONE, DIRECTION_NORTH, DIRECTION_NORTHEAST, DIRECTION_NORTHWEST, DIRECTION_SOUTH, DIRECTION_SOUTHEAST, DIRECTION_SOUTHWEST, DIRECTION_WEST, FONT_DEFAULT_SIZE, LAYOUT_ORIENTATION_HORIZONTAL, LAYOUT_ORIENTATION_VERTICAL, LENGTH_FILL_PARENT, LENGTH_PREFERRED, LENGTH_UNKNOWN, SLIDER_MAX_VALUE, SLIDER_MIN_VALUE, SLIDER_THUMB_VALUE, TOAST_LENGTH_LONG, TOAST_LENGTH_SHORT, TYPEFACE_DEFAULT, TYPEFACE_MONOSPACE, TYPEFACE_SANSSERIF, TYPEFACE_SERIF
Modifier | Constructor and Description |
---|---|
protected |
Sprite(ComponentContainer container)
Creates a new Sprite component.
|
protected |
Sprite(ComponentContainer container,
android.os.Handler handler)
Creates a new Sprite component.
|
Modifier and Type | Method and Description |
---|---|
void |
alarm()
Moves and redraws sprite, registering changes.
|
void |
Bounce(int edge)
Makes this sprite bounce, as if off of a wall by changing the
heading (unless the sprite is not traveling toward the specified
direction). |
void |
CollidedWith(Sprite other)
Handler for CollidedWith events, called when two sprites collide.
|
static boolean |
colliding(Sprite sprite1,
Sprite sprite2)
Determines whether two sprites are in collision.
|
boolean |
CollidingWith(Sprite other)
Indicates whether a collision has been registered between this sprite
and the passed sprite.
|
boolean |
containsPoint(double qx,
double qy)
Indicates whether the specified point is contained by this sprite.
|
void |
Dragged(float startX,
float startY,
float prevX,
float prevY,
float currentX,
float currentY)
Handler for Dragged events.
|
void |
EdgeReached(int edge)
Event handler called when the sprite reaches an edge of the screen.
|
boolean |
Enabled()
Enabled property getter method.
|
void |
Enabled(boolean enabled)
Enabled property setter method: starts or stops the timer.
|
void |
Flung(float x,
float y,
float speed,
float heading,
float xvel,
float yvel)
When a fling gesture (quick swipe) is made on the sprite: provides
the (x,y) position of the start of the fling, relative to the upper
left of the canvas.
|
BoundingBox |
getBoundingBox(int border)
Provides the bounding box for this sprite.
|
HandlesEventDispatching |
getDispatchDelegate()
Returns the dispatch delegate that is responsible for dispatching events
for this component.
|
double |
Heading()
Returns the heading of the sprite.
|
void |
Heading(double userHeading)
Sets heading in which sprite should move.
|
protected int |
hitEdge()
Specifies which edge of the canvas has been hit by the Sprite, if
any, moving the sprite back in bounds.
|
protected int |
hitEdge(int canvasWidth,
int canvasHeight) |
void |
Initialize() |
boolean |
intersectsWith(BoundingBox rect)
Determines whether this sprite intersects with the given rectangle.
|
int |
Interval()
Interval property getter method.
|
void |
Interval(int interval)
Interval property setter method: sets the interval between timer events.
|
void |
MoveIntoBounds()
Moves the sprite back in bounds if part of it extends out of bounds,
having no effect otherwise.
|
protected void |
moveIntoBounds(int canvasWidth,
int canvasHeight)
Moves the sprite back in bounds if part of it extends out of bounds,
having no effect otherwise.
|
void |
MoveTo(double x,
double y)
Moves sprite directly to specified point.
|
void |
NoLongerCollidingWith(Sprite other)
Handler for NoLongerCollidingWith events, called when a pair of sprites
cease colliding.
|
void |
onDelete() |
void |
onDestroy() |
protected abstract void |
onDraw(android.graphics.Canvas canvas)
Draws the sprite on the given canvas
|
void |
PointInDirection(double x,
double y)
Turns this sprite to point towards a given point.
|
void |
PointTowards(Sprite target)
Turns this sprite to point towards a given other sprite.
|
protected void |
postEvent(Sprite sprite,
java.lang.String eventName,
java.lang.Object... args)
Posts a dispatch for the specified event.
|
protected void |
registerChange()
Responds to a move or change of this sprite by redrawing the
enclosing Canvas and checking for any consequences that need
handling.
|
float |
Speed()
Gets the speed with which this sprite moves.
|
void |
Speed(float speed)
Sets the speed with which this sprite should move.
|
void |
TouchDown(float x,
float y)
When the user begins touching the sprite (places finger on sprite and
leaves it there): provides the (x,y) position of the touch, relative
to the upper left of the canvas
|
void |
Touched(float x,
float y)
When the user touches the sprite and then immediately lifts finger: provides
the (x,y) position of the touch, relative to the upper left of the canvas
|
void |
TouchUp(float x,
float y)
When the user stops touching the sprite (lifts finger after a
TouchDown event): provides the (x,y) position of the touch, relative
to the upper left of the canvas
|
protected void |
updateCoordinates()
Updates the x- and y-coordinates based on the heading and speed.
|
boolean |
Visible()
Gets whether sprite is visible.
|
void |
Visible(boolean visible)
Sets whether sprite should be visible.
|
double |
X() |
void |
X(double x) |
double |
Y() |
void |
Y(double y) |
double |
Z() |
void |
Z(double layer)
Sets the layer of the sprite, indicating whether it will appear in
front of or behind other sprites.
|
Height, Height, Width, Width
protected final Canvas canvas
protected boolean initialized
protected int interval
protected boolean visible
protected double xLeft
protected double yTop
protected double zLayer
protected float speed
protected double userHeading
heading
, headingRadians
, headingCos
, and
headingSin
.protected double heading
protected double headingRadians
protected double headingCos
protected double headingSin
protected Sprite(ComponentContainer container, android.os.Handler handler)
container
- where the component will be placedhandler
- a scheduler to which runnable events will be postedprotected Sprite(ComponentContainer container)
Ball
and ImageSprite
.container
- where the component will be placedpublic void Initialize()
public boolean Enabled()
true
indicates a running timer, false
a stopped
timerpublic void Enabled(boolean enabled)
enabled
- true
starts the timer, false
stops itpublic void Heading(double userHeading)
userHeading
and heading
, this
sets headingCos
, headingSin
, and headingRadians
.userHeading
- degrees above the positive x-axispublic double Heading()
public int Interval()
public void Interval(int interval)
interval
- timer interval in mspublic void Speed(float speed)
speed
- the magnitude (in pixels) to move every interval
millisecondspublic float Speed()
interval
milliseconds.public boolean Visible()
true
if the sprite is visible, false
otherwisepublic void Visible(boolean visible)
visible
- true
if the sprite should be visible; false
otherwise.public double X()
public void X(double x)
public void Y(double y)
public double Y()
public void Z(double layer)
layer
- higher numbers indicate that this sprite should appear
in front of ones with lower numbers; if values are equal for
sprites, either can go in front of the otherpublic double Z()
protected void postEvent(Sprite sprite, java.lang.String eventName, java.lang.Object... args)
sprite
- the instance on which the event takes placeeventName
- the name of the eventargs
- the arguments to the event handlerpublic void CollidedWith(Sprite other)
other
- the other sprite in the collisionpublic void Dragged(float startX, float startY, float prevX, float prevY, float currentX, float currentY)
startX
- the starting x-coordinatestartY
- the starting y-coordinateprevX
- the previous x-coordinate (possibly equal to startX)prevY
- the previous y-coordinate (possibly equal to startY)currentX
- the current x-coordinatecurrentY
- the current y-coordinatepublic void EdgeReached(int edge)
public void NoLongerCollidingWith(Sprite other)
registeredCollisions
so that
CollidedWith(Sprite)
and this event are only raised once per
beginning and ending of a collision.other
- the sprite formerly colliding with this spritepublic void Touched(float x, float y)
x
- x-coordinate of touched pointy
- y-coordinate of touched pointpublic void Flung(float x, float y, float speed, float heading, float xvel, float yvel)
x
- x-coordinate of touched pointy
- y-coordinate of touched point
* @param speed the speed of the fling sqrt(xspeed^2 + yspeed^2)heading
- the heading of the flingxvel
- the speed in x-direction of the flingyvel
- the speed in y-direction of the flingpublic void TouchUp(float x, float y)
x
- x-coordinate of touched pointy
- y-coordinate of touched pointpublic void TouchDown(float x, float y)
x
- x-coordinate of touched pointy
- y-coordinate of touched pointpublic void Bounce(int edge)
heading
(unless the sprite is not traveling toward the specified
direction). This also calls MoveIntoBounds()
in case the
sprite is out of bounds.edge
- the direction of the object (real or imaginary) to bounce off
of; this should be one of
Component.DIRECTION_NORTH
,
Component.DIRECTION_NORTHEAST
,
Component.DIRECTION_EAST
,
Component.DIRECTION_SOUTHEAST
,
Component.DIRECTION_SOUTH
,
Component.DIRECTION_SOUTHWEST
,
Component.DIRECTION_WEST
, or
Component.DIRECTION_NORTHWEST
.public boolean CollidingWith(Sprite other)
other
- the sprite to check for collision with this spritetrue
if a collision event has been raised for the pair of
sprites and they still are in collision, false
otherwise.public void MoveIntoBounds()
public void MoveTo(double x, double y)
x
- the x-coordinatey
- the y-coordinatepublic void PointTowards(Sprite target)
target
- the other sprite to point towardspublic void PointInDirection(double x, double y)
x
- parameter of the point to turn toy
- parameter of the point to turn toprotected void registerChange()
EdgeReached(int)
event if the Sprite has reached the edge of the
Canvas.protected int hitEdge()
Component.DIRECTION_NONE
if no edge has been hit, or a
direction (e.g., Component.DIRECTION_NORTHEAST
) if that
edge of the canvas has been hitprotected final void moveIntoBounds(int canvasWidth, int canvasHeight)
protected void updateCoordinates()
registerChange()
.protected int hitEdge(int canvasWidth, int canvasHeight)
public BoundingBox getBoundingBox(int border)
border
- the number of pixels outside the sprite to include in the
bounding boxpublic static boolean colliding(Sprite sprite1, Sprite sprite2)
sprite1
- one spritesprite2
- another spritetrue
if they are in collision, false
otherwisepublic boolean intersectsWith(BoundingBox rect)
rect
- the rectangletrue
if they intersect, false
otherwisepublic boolean containsPoint(double qx, double qy)
qx
- the x-coordinateqy
- the y-coordinatepublic void alarm()
alarm
in interface AlarmHandler
public HandlesEventDispatching getDispatchDelegate()
Component
getDispatchDelegate
in interface Component
public void onDestroy()
onDestroy
in interface OnDestroyListener
public void onDelete()
onDelete
in interface Deleteable
protected abstract void onDraw(android.graphics.Canvas canvas)
canvas
- the canvas on which to draw