I’m trying to make a simple game (such as Arkanoid) on Android.
I used some elements from ping-pong-game pattern which I found on the internet.
So I was trying to change this class (GameObject) by using Bitmap instead of Drawable, but some problems appeared.
I have got some questions:
- The
Rectobject which I’ve got here used like field with it own resolution so how can make something like this usingBitmap? - Is something like
.getBounds()exist forBitmap? - Can I make animated objects (blinking, change colour and e.t.c) somehow with
DrawableorBitmapis better for animation?
Here’s my code:
package project.java.game.objects;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
public abstract class GameObject {
// Directions
public final static int DIR_LEFT = -1;
public final static int DIR_RIGHT = 1;
public final static int DIR_NONE = 0;
public final static int DIR_UP = 1;
public final static int DIR_DOWN = -1;
/** Higer border of object */
public int getTop() { return mPoint.y; }
/** Lower border */
public int getBottom() { return mPoint.y + mHeight; }
/** Left border */
public int getLeft() { return mPoint.x; }
/** right border */
public int getRight() { return mPoint.x + mWidth; }
public int returnCenter(){ return mPoint.x + mWidth/2; }
/** Central point */
public Point getCenter() { return new Point(mPoint.x + mWidth / 2, mPoint.y + mHeight / 2); }
/** Height of object */
public int getHeight() { return mHeight; }
/** Width */
public int getWidth() { return mWidth; }
/** @return Recatngle, which is limit objects */
public Rect getRect() {
return mImage.getBounds();
}
/** for intersection */
public static boolean intersects(GameObject obj1, GameObject obj2)
{
return Rect.intersects(obj1.getRect(), obj2.getRect());
}
/** for coordinates */
protected Point mPoint;
/** Height of image */
protected int mHeight;
/** Width */
protected int mWidth;
/** image */
private Drawable mImage;
/** speed */
protected int mSpeed;
/**Life level*/
protected int mLifeLvl;
/**
* Constructor
* @param image image which will use for object
*/
public GameObject(Drawable image)
{
mImage = image;
mPoint = new Point(0, 0);
mWidth = image.getIntrinsicWidth();
mHeight = image.getIntrinsicHeight();
}
/** point change position */
protected abstract void updatePoint();
/** object change position */
public void update()
{
updatePoint();
mImage.setBounds(mPoint.x, mPoint.y, mPoint.x + mWidth, mPoint.y + mHeight);
}
/**to draw object */
public void draw(Canvas canvas)
{
mImage.draw(canvas);
}
/** set Left bound */
public void setLeft(int value) { mPoint.x = value; }
/** set Right bound */
public void setRight(int value) { mPoint.x = value - mWidth; }
/** set top bound */
public void setTop(int value) { mPoint.y = value; }
/** set Lower bound */
public void setBottom(int value) { mPoint.y = value - mHeight; }
/** center of object on OX */
public void setCenterX(int value) { mPoint.x = value - mHeight / 2; }
/** center of object on OY */
public void setCenterY(int value) { mPoint.y = value - mWidth / 2; }
}
To get bitmap’s dimension use the
getWidth()andgetHeight()methods.To create bitmap from a bitmap of desired dimension, use the
createScaledBitmapmethod of theBitmapclassYou can use the Android
Animationclass to apply animation on bitmap through layout. Available in theAPIDemosapplication. (ApiDemos->Views->Animation->3D Transition)