Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

closed as too localized by Byte56, Jonathan Hobbs, John McDonald, Trevor Powell, Josh Petrie Oct 4 '12 at 16:03

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Can you show us the stack trace please –  Ray Britton Jul 3 '12 at 16:17

2 Answers 2

I imagine that since the only thing you're changing is if(steering.isTouched()){ then you're likely not instantiating your steering variable. I imagine your error is something to do with a null value?

share|improve this answer

I'm going to risk an answer based on my past experience with the platform.

Touch events come from the UI thread, and your GLSurfaceView is running its own renderer thread. This means that your touch handling code and rendering are clashing together, causing the app to crash immediately when both threads make concurrent access to a shared variable, in this case, the bitmap/sprite or what you're using.

To solve this, either use a mutex or lock or something like that to synchronize both threads, or cache the touch events, and handle them in the renderer thread instead. This last one is how I did it, without problems at all so far.

Hope it helps.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.