android.gameengine.icadroids.objects
Class GameObject

java.lang.Object
  extended by android.gameengine.icadroids.objects.GameObject
Direct Known Subclasses:
MoveableGameObject

public class GameObject
extends java.lang.Object

GameObject is a (not moving) object with a lot of useful methods in it. You can bind a sprite on it, activate alarms, get the size, position, etc. Don't forget to add the GameObject to the game with 'addGameObject'!

Author:
Bas van der Zandt

Field Summary
 Rect position
          The bounding rectangle that contains the sprite on the screen
protected  double xlocation
          The exact x position of the upper left corner of the object.
protected  double ylocation
          the exact y position of the upper left corner of the object.
 
Constructor Summary
GameObject()
           
 
Method Summary
 void clearActive()
          Mark an object as 'dead', to be removed at end of cycle.
 void deleteThisGameObject()
          Delete the GameObject.
 void drawCustomObjects(Canvas canvas)
          Override this method to draw your own text, rectangle and other objects on the screen (like a game dashboard).
 void drawGameObject(Canvas canvas)
          Override this method to implement your own rendered canvas objects like text, rectangles or colors.
 double getAngle(GameObject object)
          Use this function to get the angle between you and another object.
 float getCenterX()
          Get the center x of the GameObject
 float getCenterY()
          Get the center y of the GameObject
 float getDepth()
          Get the depth of the object in rendering.
 int getFrameHeight()
          Get the frame height of the object's sprite, this will be also the height of the object in the game.
 int getFrameWidth()
          Get the frame width of the object's sprite, this will be also the width of the object in the game.
 double getFullX()
          Get the exact x position of the GameObject
 double getFullY()
          Get the exact y position of the GameObject
 Rect getPosition()
          Get the sprite position as rectangle
 AnimatedSprite getSprite()
          Get the sprite Object of the GameObject
 int getX()
          Get the x position on the screen, rounds the exact X-pos to an int.
 int getY()
          Get the y position on the screen, rounds the exact Y-pos to an int.
 void intialize()
          Called when the application starts.
 void intializeGameObject()
          Initialize resources.
 boolean isActive()
          Ask if an object is still alive, that is: it hasn't been deleted from the game or added in this cycle of the game.
 boolean isVisible()
          Ask if an object is visible
 void jumpToStartPosition()
          Jump to the object's start position Note: this only works when a start position is set, this happens automatically when 'addGameObject' is called.
 void setAnimationSpeed(int speed)
          Set the speed of the sprite animation.
 void setDepth(float depth)
          Set the depth of the object in rendering.
 void setFrameNumber(int number)
          Set the frame number of the sprite.
 void setPosition(double x, double y)
          Set the position on the screen.
 void setSprite(AnimatedSprite theSprite)
          Set a sprite for the GameObject.
 void setSprite(java.lang.String resourceName)
          Set a sprite for the GameObject.
 void setSprite(java.lang.String resourceName, int numberOfFrames)
          Set a sprite for the GameObject.
 void setStartPosition(int x, int y)
          Set a (new) start position which the objects jumps to when using 'jumpToStartPosition()'.
 void setVisibility(boolean visible)
          Set the visibility of the GameOject.
 void setX(double x)
          Set x position of the GameObject WARNING: Do NOT use this method to move the player or a lot of methods will behave incorrectly.
 void setY(double y)
          Set y position of the GameObject.
 void startAnimate()
          Start animating the sprite, from the current frame
 void stopAnimate()
          Stop animating the sprite.
 void update()
          The update-method will be called every cycle of the game loop.
protected  void updatePlayerFramePosition()
          Update position rectangle used for drawing the sprite on the right position with the right size on the screen.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

xlocation

protected double xlocation
The exact x position of the upper left corner of the object.


ylocation

protected double ylocation
the exact y position of the upper left corner of the object.


position

public Rect position
The bounding rectangle that contains the sprite on the screen

Constructor Detail

GameObject

public GameObject()
Method Detail

intializeGameObject

public final void intializeGameObject()
Initialize resources.


intialize

public void intialize()
Called when the application starts. You can override this method to do initialization at the very start of the game. Therefore it is only useful for objects that are always present in the game, from start of the game. These objects must be created in the constructor of your game.


isActive

public boolean isActive()
Ask if an object is still alive, that is: it hasn't been deleted from the game or added in this cycle of the game.

Returns:
boolean indicating if this object still is in the game

isVisible

public boolean isVisible()
Ask if an object is visible

Returns:
boolean indicating if this object still is visible

clearActive

public void clearActive()
Mark an object as 'dead', to be removed at end of cycle.
Note: don't use this method to delete objects, use deleteGameObject instead. This method will call clearActive() and it will also do other necessary actions, like removing Alarms belonging to the object.
Note: this method should be invisible to game programmers.


update

public void update()
The update-method will be called every cycle of the game loop. Override this method to give an object any time driven behaviour.
Note: Always call super.update() first in your overrides, because the default update does some important actions, like sprite animation.


drawGameObject

public void drawGameObject(Canvas canvas)
Override this method to implement your own rendered canvas objects like text, rectangles or colors. When you are using a viewport, the rendering will be relative to the screen. If you want absolute rendering, use 'drawCustomObjects'. Don't forget to call super(canvas)!! Draw the GameObject on the screen, called by game renderer

Parameters:
canvas - The Android canvas. Use this provided object to draw for example text, rectangles and colors. For the api see: http://developer.android.com/reference/android/graphics/Canvas.html

drawCustomObjects

public void drawCustomObjects(Canvas canvas)
Override this method to draw your own text, rectangle and other objects on the screen (like a game dashboard). The objects will be rendered on the absolute x,y position.
Note: This method was used to paint things on the screen. In the cuurent version you can use the Dashboard instead.

Parameters:
canvas - The Android canvas. Use this provided object to draw for example text, rectangles and colors. For the api see: http://developer.android.com/reference/android/graphics/Canvas.html

updatePlayerFramePosition

protected void updatePlayerFramePosition()
Update position rectangle used for drawing the sprite on the right position with the right size on the screen. This rectangle is also used for collision detection between GameObjects.


setSprite

public final void setSprite(java.lang.String resourceName)
Set a sprite for the GameObject. Sprite images must be stored in the res/drawable folders of your Android project. If you have only one image, store it in the 'nodpi' folder. If you have various versions (hi-res and lo-res) for rendering on devices of varying screen sizes, use the lo- & hi-dpi folders. Note: Extremely small sprites don't work well with Collision Detection!

Parameters:
resourceName - The name of the resource in the /res/drawable folder without extension , so when your picture in the /res/drawable is named 'picture.jpg', this parameter should be "picture" .

setSprite

public final void setSprite(java.lang.String resourceName,
                            int numberOfFrames)
Set a sprite for the GameObject. Sprite images must be stored in the res/drawable folders of your Android project. If you have only one image, store it in the 'nodpi' folder. If you have various versions (hi-res and lo-res) for rendering on devices of varying screen sizes, use the lo- & hi-dpi folders. Note: Extremely small sprites don't work well with Collision Detection!

Parameters:
resourceName - The name of the resource in the /res/drawable folder without extension , so when your picture in the /res/drawable is named 'picture.jpg', this parameter should be "picture" .
numberOfFrames - The number of frames in the strip

setSprite

public final void setSprite(AnimatedSprite theSprite)
Set a sprite for the GameObject. Use this method if you create sprites yourself, for instance if you create subclasses of AnimatedSprite that have special animations.

Parameters:
theSprite - AnimatedSprite that has been created earlier

startAnimate

public final void startAnimate()
Start animating the sprite, from the current frame


stopAnimate

public final void stopAnimate()
Stop animating the sprite. The animation will stop at the current frame and will not go back to the first frame


getX

public final int getX()
Get the x position on the screen, rounds the exact X-pos to an int.

Returns:
x position on the screen as an int

getY

public final int getY()
Get the y position on the screen, rounds the exact Y-pos to an int.

Returns:
the y position on the screen as an int

getCenterX

public final float getCenterX()
Get the center x of the GameObject

Returns:
the center x coordinate of the GameObject

getCenterY

public final float getCenterY()
Get the center y of the GameObject

Returns:
the center y coordinate of the GameObject

getFullX

public final double getFullX()
Get the exact x position of the GameObject

Returns:
the exact x position, as a double

getFullY

public final double getFullY()
Get the exact y position of the GameObject

Returns:
the exact y position, as a double

setX

public final void setX(double x)
Set x position of the GameObject WARNING: Do NOT use this method to move the player or a lot of methods will behave incorrectly.

Parameters:
x - The x position

setY

public final void setY(double y)
Set y position of the GameObject. WARNING: Do NOT use this method to move the player or a lot of methods will behave incorrectly.

Parameters:
y - The y position

setPosition

public final void setPosition(double x,
                              double y)
Set the position on the screen. WARNING: Do NOT use this method to move the player or a lot of methods will behave incorrectly.

Parameters:
x - The x position
y - The y position

getSprite

public final AnimatedSprite getSprite()
Get the sprite Object of the GameObject

Returns:
Sprite Object

getFrameWidth

public final int getFrameWidth()
Get the frame width of the object's sprite, this will be also the width of the object in the game.

Returns:
The width of the frame in pixels

getFrameHeight

public final int getFrameHeight()
Get the frame height of the object's sprite, this will be also the height of the object in the game.

Returns:
The height of the frame in pixels

getPosition

public final Rect getPosition()
Get the sprite position as rectangle

Returns:
Rectangle with the current position + size of the GameObject

setAnimationSpeed

public final void setAnimationSpeed(int speed)
Set the speed of the sprite animation.

Parameters:
speed - The number of game loops that must occur before the next frame, so higher speed is slower animation. 0 stops animation.

setFrameNumber

public final void setFrameNumber(int number)
Set the frame number of the sprite. This method can be used both with animated sprites and non-animated sprites (for instance when there are frames for looking left, right, ...).
Of course, sprite must be a film strip.

Parameters:
number - The frame number, frame numbers range from 0 to NrOfFrames-1

deleteThisGameObject

public final void deleteThisGameObject()
Delete the GameObject. This will also delete the Alarms of this object.


setVisibility

public final void setVisibility(boolean visible)
Set the visibility of the GameOject. The GameObject will still exist.

Parameters:
visible - True for visible, false for invisible

jumpToStartPosition

public void jumpToStartPosition()
Jump to the object's start position Note: this only works when a start position is set, this happens automatically when 'addGameObject' is called.


setStartPosition

public void setStartPosition(int x,
                             int y)
Set a (new) start position which the objects jumps to when using 'jumpToStartPosition()'.

Parameters:
x - The start X position
y - The start Y position

getAngle

public final double getAngle(GameObject object)
Use this function to get the angle between you and another object. For example: You can use this function to check if you approaching another object from the left or right. Direction 0 points up, directions go clockwise, so 90 is right, etc.

Parameters:
object - an instance of another object to calculate the angle for.
Returns:
the angle of the object or 0 if the object is null.

getDepth

public float getDepth()
Get the depth of the object in rendering. Depth is a number between 0 and 1. 1 means at the top (foreground) 0 means at the bottom (background)

Returns:
depth, a float

setDepth

public void setDepth(float depth)
Set the depth of the object in rendering. Number between 0 and 1. 1 means at the top (foreground), 0 means at the bottom (background).
Setting the depth is only useful before you add the Object to the game, because it influences its position in the list of Objects. Using this method on Objects that have already been added has no effect. Instead of using this method, it is recommended to use the addGameObject method with depth-parameter in GameEngine

Parameters:
depth - the depth, a float
See Also:
GameEngine.addGameObject(GameObject gameObject, float layerposition)