0

I have a Main Activity and in the corresponding XML Layout I have a Custom View that draws game objects (my tank and 10 enemies), a few buttons to control my tank and fire bullets and a TextView to show my score. My custom view is a GameSurfaceView java class that is a half screen game board. Here is some of my code:

public class GameSurfaceView extends SurfaceView implements Runnable {
    private static Context gContext;
    public GameSurfaceView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
        resume();
        gContext = context;
    }
    public void resume() {
        isRunning = true;
        gameThread = new Thread(this);
        gameThread.start();
    }
    public void pause() {
        isRunning = false;
        boolean retry = true;
        while (retry) {
            try {
                gameThread.join();
                retry = false;
            } catch (InterruptedException e) {
                // try again shutting down the thread
            }
        }
    }
    @Override
    public void run() {
        while (isRunning) {
            // We need to make sure that the surface is ready
            if (!holder.getSurface().isValid()) {
                continue;
            }
            long started = System.currentTimeMillis();

            // update
            step();

            // draw
            Canvas canvas = holder.lockCanvas();
            if (canvas != null) {
                render(canvas);
                holder.unlockCanvasAndPost(canvas);
            }

            //detect all possible collisions
            detectCollisions();

            float deltaTime = (System.currentTimeMillis() - started);
            int sleepTime = (int) (FRAME_PERIOD - deltaTime);
            if (sleepTime > 0) {
                try {
                    gameThread.sleep(sleepTime);
                } catch (InterruptedException e) {
                }
            }
            while (sleepTime < 0) {
                step();
                sleepTime += FRAME_PERIOD;
            }
        }
    }

    //Called from MainActivity
    public void dispatchKey(int tDirection) {
        Toast.makeText(gContext, "Hi", Toast.LENGTH_LONG).show();
        gameStarted = true;
        if (tDirection == FIRE)
            Fire();
        else if (tDirection != tank.Direction)
            turnTankDirection = tDirection;
    }
    private void detectCollisions() {
        //Collision Detection between tank and enemy
        Toast.makeText(gContext, "Collision", Toast.LENGTH_LONG).show();
    }
}

My questions: 1- Why the Toast in dispatchKey() runs correctly but Toast in detectCollisions() makes a force close? 2- How to update TextView in detectCollisions() method? 3- How to show a DialogAlert when a collision detected in detectCollisions() method? My problem relates mainly to gContext variable. Thanks.

Ahmad
  • 1
  • 3

1 Answers1

0

Regarding question 1: Maybe this effects the second thread. While dispatchKey() is called from Activity, detectCollision() is invoked from the sureface-thread. Did you tried to call detectCollision() from activity?

Regarding question 3: let your activity implement a listener, which will be called if collision is detected. The same thing you could use as solution of question 1 and 2.


You should try:

MainActivity.this.runOnUiThread(new Runnable() {
    public void run() {
        //Make toast or manipulate TextView
    }
});
  • Yes, If I call detectCollision() from activity the Toast will be shown. So how can I solve the problems, because I want to do these works in GameSurfaceView class, not in activity. – Ahmad Jul 27 '16 at 20:39
  • Excuse me, where I should put this code? In my detectCollision() method it arises this error: "game.tank.MainActivity" is not an enclosing class. – Ahmad Jul 27 '16 at 21:11