Crash when trying to detect touch
- by iQue
I've got a character in a 2D game using surfaceView that I want to be able to move using a button (eventually a joystick), but my game crashes as soon as I try to move my sprite.
This is my onTouch-method for my steering button:
public void handleActionDown(int eventX, int eventY) {
if (eventX >= (x - bitmap.getWidth() / 2) && (eventX <= (x + bitmap.getWidth()/2))) {
if (eventY >= (y - bitmap.getHeight() / 2) && (y <= (y + bitmap.getHeight() / 2))) {
setTouched(true);
} else {
setTouched(false);
}
} else {
setTouched(false);
}
And if I try to put this in my update-method:
public void update() {
x += (speed.getXv() * speed.getxDirection());
y += (speed.getYv() * speed.getyDirection());
}
The sprite moves on its own just fine, but as soon as I add:
public void update() {
if(steering.isTouched()){
x += (speed.getXv() * speed.getxDirection());
y += (speed.getYv() * speed.getyDirection());
}
the game crashes.
Does anyone know why this is or how to fix it? I cannot figure it out. I'm using MotionEvent.ACTION_DOWN to check if the user if pressing the screen.