public abstract class GameObject
extends java.lang.Object
This class is abstract so it is never used directly to create instances. The
Sprite
class is a subclass that also manages an image and is thus usable
in a game.
Modifier and Type | Field and Description |
---|---|
android.graphics.RectF |
boundingRect
Tracks the upper left and lower right coordinates of a box that surrounds this object
in world coordinates.
|
protected GameObjectManager |
manager
To be useful, every game object must register with the game object manager
|
java.lang.String |
name
The game object's name, used to look up an object in the game manager, or determine which
object something has collided with.
|
Constructor and Description |
---|
GameObject(java.lang.String name,
android.graphics.RectF extent)
Basic constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(float x,
float y)
Checks if this game object's bounding box includes a given point.
|
abstract void |
draw(android.graphics.Canvas c,
float xScale,
float yScale)
Must be implemented by any subclasses to do the work of actually drawing the game object
onto the screen.
|
long |
getTimeOnScreen()
Check how long a game object has been alive.
|
void |
hop(float distance,
float direction)
Instantaneously move the game object a given distance in the given direction.
|
void |
hopToward(float distance,
float destx,
float desty)
Instantaneously hop the sprite a given distance in the direction of another point in
world space.
|
boolean |
intersects(GameObject other)
Checks if this game object's bounding area intersects at all with another game object.
|
boolean |
isFullyOffScreen()
Returns true if this object is entirely outside the boundaries of the screen
at the object's current location.
|
boolean |
isFullyOnScreen()
Returns true if this game object is entirely within the boundaries of the screen at
the object's current location.
|
boolean |
isInside(GameObject other)
Checks if this game object's bounding rectangle is entirely inside of another object.
|
void |
moveBy(float dx,
float dy)
Adjust the game object's position by a given dx and dy offset
|
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
game object's bounding box.
|
void |
onTouch(float x,
float y)
Called when the user taps the screen at a location inside this game object's bounding box.
|
boolean |
removalRequested()
Check if this game object is scheduled for removal
|
void |
requestRemoval()
Request that the game object manager remove this sprite after this update loop is complete.
|
void |
setManager(GameObjectManager manager)
Called by the
GameObjectManager to be sure this game object knows who its
manager is. |
void |
setMaxTimeOnScreen(int msec)
Configure this game object to self-destruct after a given number of milliseconds on the
screen.
|
void |
update(int msec)
Called once per screen refresh by the
GameObjectManager to update the size,
location, or other attributes of this game object as needed by the game's logic. |
public java.lang.String name
public android.graphics.RectF boundingRect
protected GameObjectManager manager
public GameObject(java.lang.String name, android.graphics.RectF extent)
name
- name of this sprite (used to look it up later)extent
- initial area on the screen this sprite will occupy, in world unitspublic void setManager(GameObjectManager manager)
GameObjectManager
to be sure this game object knows who its
manager is. This method is called automatically - do not use.manager
- the current game object managerpublic void update(int msec)
GameObjectManager
to update the size,
location, or other attributes of this game object as needed by the game's logic.
If you are creating a subclass of GameObject
or Sprite
be sure to call
super.update()
in your update()
method so that the object removal
logic will work correctly.
msec
- number of millseconds since the previous update call.
Typically 16msec at 60Hz screen refresh.public void onTouch(float x, float y)
x
- horizontal coordinate in world unitsy
- vertical coordinate in world unitspublic void onFling(float x, float y, float dx, float dy)
x
- horizontal coordinate in world unitsy
- vertical coordinate in world unitspublic void setMaxTimeOnScreen(int msec)
When the game engine properly supports pausing/unpausing the game, this count should also "freeze" in place and resume counting again automatically.
msec
- number of milliseconds until this game object requests automatic removalpublic long getTimeOnScreen()
public abstract void draw(android.graphics.Canvas c, float xScale, float yScale)
c
- the Canvas
object the game is currently being drawn ontoxScale
- the horizontal scaling factor between world coordinates and screen coordinatesyScale
- the vertical scaling factor between world coordinates and screen coordinatespublic boolean contains(float x, float y)
x
- horizontal coordinate of the point to check, in world unitsy
- vertical coofdinate of the point to check, in world unitspublic void hop(float distance, float direction)
distance
- distance to hop, in world unitsdirection
- direction to hop in degrees counter-clockwise from the X axispublic void moveBy(float dx, float dy)
dx
- horizontal offset in world coordinatesdy
- vertical offset in world coordinatespublic void hopToward(float distance, float destx, float desty)
For instance, if you want bullets from a flying saucer to always fly towards your player's ship, this routine might be helpful.
distance
- distance in world units to move the game objectdestx
- horizontal coordinate of the point to move towardsdesty
- vertical coordinate of the point to move towardspublic final boolean isInside(GameObject other)
other
- the game object to test if this object is inside of.other
public final boolean intersects(GameObject other)
other
- the game object to test if this object is touchingother
public boolean isFullyOnScreen()
true
if the object is entirely on-screenpublic boolean isFullyOffScreen()
true
if the object is entirely off-screenpublic final void requestRemoval()
public final boolean removalRequested()