0

I'm struggeling with following problem: I'm developing a small 2D Adroid game on Eclipse ( I'm a greenhorn). The basic idea is too keep a figure on the screen and when it leaves it , it's Game Over. The basic classes are the GameActivity, the GameView, the Figure and a GameOverActivity. Since my gaming-background is drawn on a simple RecF I check when the figure leaves the screen by

public class Figure{

public RectF getBounds(){
       return new RectF(x, y, x + width, y+ height);
     }
}
---------------------------------
public class GameView extends SurfaceView {

private GameActivity theGameActivity = new GameActivity();
private RectF figurebounds;

public void checkifout() {
 figurebounds = figure.getBounds();
    if (!(background.intersect(figurebounds))) {
            theGameActivity.GameOver();  
   }
}
------------------------------------------
public class GameActivity extends Activity {

public void GameOver() {

    Intent theNextIntent = new Intent(getApplicationContext(), GameOverActivity.class);
    startActivity(theNextIntent);
    this.finish();
 }
}

The GameOverActivity is just a Layout with 2 buttons which allows to get back to the menu or replay

public class GameOverActivity extends Activity implements OnClickListener {

Button bReplay;
Button bBack;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gameoverscreen);
    initialize();
}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bReplay:
        Intent newGameScreen= new Intent(this, GameActivity.class);
        startActivity(newGameScreen);
        this.finish();
        break;
    case R.id.bBack:
        Intent newGameScreen2 = new Intent(this, MenuActivity.class);
        startActivity(newGameScreen2);
        this.finish();
        break;
    }
}

public void initialize(){
    bReplay = (Button) findViewById(R.id.bReplay);
    bReplay.setOnClickListener(this);
    bBack = (Button) findViewById(R.id.bBack);
    bBack.setOnClickListener(this);

}

At first I had the checkifout()-method within my onTouch-method (which is used to create another figures which have no influence on this issue) in the GameView-class , so I had to touch the screen after the figure fell off to get to the Gameover-screen, but at least it worked all fine:

public boolean onTouchEvent(MotionEvent event) {
    if (System.currentTimeMillis() - lastClick > 300) {
        lastClick = System.currentTimeMillis();
        synchronized (getHolder()) {
            xcor = event.getX();
            ycor = event.getY();
            checkifout();

        }

    }
    return true;
}

But I'd like the GameOver-screen appear without another Touchevent. Now when I call the checkifout()-method within my draw-method (also within the GameView class):

public void draw(Canvas canvas) {
    drawBackGround(canvas);
    figure.draw(canvas);
    checkifout();
}

I get following problem: When my figure falls off the screen I do get the GameOver-screen, but after about a second it displays another time and when I go back to the main menu and try to exit the game I get back to the GameOverscreen, while replay stills works fine. I don't get those issues when I implement checkifout() in the OnTouchevent... I thought it might be a problem of calling the checkifout()-method multiple times, so I tried to fix the issue by using a boolean like this :

private boolean gameover = false;

public void draw(Canvas canvas) {
    drawBackGround(canvas);
    figure.draw(canvas);
    if (!gameover)
        checkifout();
}

public void checkifout() {
 figurebounds = figure.getBounds();
    if (!(background.intersect(figurebounds))) {
            theGameActivity.GameOver();
            gameover=true;  
   }

But that gave me a NullPointerException.I also tried to create the checkifout-method within the Figure class and call it in the draw-method of the Figure-class, but that caused the same errors. So I hope someone has got an idea or just knows the mistake I've done here. Thanks for taking the time.

PS: Here is the MenuActivity (Layout with 2 buttons) in case it's needed:

public class MenuActivity extends Activity implements OnClickListener {

ImageButton bPlay;
ImageButton bExit;

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.bPlay:
        Intent newGameScreen = new Intent(this, GameActivity.class);
        startActivity(newGameScreen);
        this.finish();
        break;
    case R.id.bExit:
        this.finish();
        break;
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);
    initialize();
}

public void initialize(){
    bPlay = (ImageButton) findViewById(R.id.bPlay);
    bPlay.setOnClickListener(this);
    bExit = (ImageButton) findViewById(R.id.bExit);
    bExit.setOnClickListener(this);

}

}

  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – donfuxx Mar 09 '14 at 18:19
  • I know what a NPE is, but since I only added a boolean to the method I wonder why it occurs. I already checked all reference types and they are all initiated :( –  Mar 10 '14 at 11:51
  • then please add the stacktrace to your question, thx! – donfuxx Mar 10 '14 at 12:33
  • which line the NPE is thrown? Is it this one: `theGameActivity.GameOver();`? – donfuxx Mar 10 '14 at 13:35
  • I just loaded the back up (the version which is in my post) because I tried to fixed it by myself this morning and messed it totally up, and it worked now ..no NPE , I dont know why, but a great thanks for trying to help me –  Mar 10 '14 at 14:01

1 Answers1

0

fixed, just seemed to be a temporal bug somewhere...