Updating sprite location with controls
Posted
by
iQue
on Game Development
See other posts from Game Development
or by iQue
Published on 2012-07-03T14:58:07Z
Indexed on
2012/07/03
15:25 UTC
Read the original article
Hit count: 271
So ive 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 any1 know why this is or how to fix it? I cannot figure it out. Im using MotionEvent.ACTION_DOWN to check if the user if pressing the screen.
}
© Game Development or respective owner