I've created a game loop in Android by extending the SurfaceView
class and the Thread
class similar to the Lunar Lander example in the Android Samples. Right now I'm just trying to find a way to let users drag Bitmaps around the screen using onTouchEvent
to receive information on where was touched and then seeing if it was within the bounds of one of my Bitmaps and moving that Bitmap's X and Y position accordingly. This works sometimes although at other times it may move an image to where I touch even if it's well outside the bounds of it and so long as I pressed down close to other images.
I have no idea where this buggy behaviour is coming from, I'm yet to implement any way of slowing down the thread so that it doesn't use max CPU - could this be making it laggy which produces these results? If anyone could shed some light on this I'd really appreciate it.
I'm getting the user input and passing to a custom method handleAction'(actionName)'
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
thread.handleActionDown((int) event.getX(), (int)event.getY());
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (thread.isTouched()) {
thread.handleActionMove((int) event.getX(), (int) event.getY());
}
}
if (event.getAction() == MotionEvent.ACTION_UP) {
if (thread.isTouched()) {
thread.handleActionUp();
}
}
return true;
}
Here are my handler methods:
tizer
is a Bitmap and TizerX & Y are class variables for its X and Y position, it's the same for ball
.
public void handleActionDown(int eventX, int eventY) {
if (eventX >= (TizerX - tizer.getWidth() / 2) && (eventX <= (TizerX + tizer.getWidth() / 2))) {
if (eventY >= (TizerY - tizer.getHeight() / 2) && (TizerY <= (TizerY + tizer.getHeight() / 2))) {
setTouched(true);
if(getBitmapTouched() == "") {
setBitmapTouched("tizer");
}
} else {
setTouched(false);
setBitmapTouched(null);
}
} else if (eventX >= (BallX - ball.getWidth() / 2) && (eventX <= (BallX + ball.getWidth() / 2))) {
if (eventY >= (BallY - ball.getHeight() / 2) && (BallY <= (BallY + ball.getHeight() / 2))) {
setTouched(true);
if(getBitmapTouched() == "") {
setBitmapTouched("ball");
}
} else {
setTouched(false);
setBitmapTouched("");
}
} else {
setTouched(false);
setBitmapTouched("");
}
}
public void handleActionUp() {
// TODO Auto-generated method stub
setTouched(false);
setBitmapTouched("");
}
public void handleActionMove(int eventX, int eventY) {
if(isTouched()) {
if(getBitmapTouched() == "tizer") {
setTizerX(eventX);
setTizerY(eventY);
} else if (getBitmapTouched() == "ball") {
setBallX(eventX);
setBallY(eventY);
}
}
My thread constantly calls the doDraw
method:
private void doDraw(Canvas canvas) {
// Draw the background image. Operations on the Canvas accumulate
// so this is like clearing the screen.
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(testBackgroundImage, 0, 0, null);
canvas.drawBitmap(tizer, TizerX - (tizer.getWidth() / 2), TizerY - (tizer.getHeight() / 2), null);
canvas.drawBitmap(ball, BallX - (ball.getWidth() / 2), BallY - (ball.getHeight() / 2), null);
}
Despite looking through countless examples on the internet and Stack Overflow on the correct way to see if a Bitmap has been touched I still have yet to get it right, I don't receive an error in the LogCat so I don't have anything else to show, if you managed to spot something in the code that seems odd please let me know.