我遇到了以下问题:我正在Eclipse上开发一个小型2D Adroid游戏(我是新手)。基本的想法是在屏幕上保留一个数字,当它离开时,它就是Game Over。基本类是GameActivity,GameView,Figure和GameOverActivity。由于我的游戏背景是在简单的RecF上绘制的,所以当图形离开屏幕时,我会检查
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();
}
}
GameOverActivity只是一个带有2个按钮的布局,可以返回菜单或重播
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);
}
起初我在我的onTouch方法中有checkifout()
- 方法(用于创建另一个对此问题没有影响的数字)在GameView类中,所以我不得不触摸后面的屏幕这个数字下降到Gameover屏幕,但至少它工作得很好:
public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 300) {
lastClick = System.currentTimeMillis();
synchronized (getHolder()) {
xcor = event.getX();
ycor = event.getY();
checkifout();
}
}
return true;
}
但我想GameOver屏幕没有其他Touchevent。
现在当我在draw-method中调用checkifout()
- 方法时(也在GameView类中):
public void draw(Canvas canvas) {
drawBackGround(canvas);
figure.draw(canvas);
checkifout();
}
我遇到了以下问题:当我的身影从屏幕上掉下来时,我确实得到了GameOver屏幕,但是大约一秒后它显示另一个时间,当我回到主菜单并尝试退出游戏时我回来了到GameOverscreen,重播剧照工作正常。当我在OnTouchevent中实现checkifout()
时,我没有遇到这些问题......
我认为这可能是多次调用checkifout()
- 方法的问题,所以我试图通过使用这样的布尔值来解决问题:
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;
}
但是这给了我一个NullPointerException
。我也尝试在Figure类中创建checkifout-method并在Figure-class的draw-method中调用它,但是这会导致相同的错误。所以我希望有人有一个想法,或者只知道我在这里犯过的错误。感谢您抽出宝贵时间。
PS:如果需要,这是MenuActivity(带有2个按钮的布局):
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);
}
}
答案 0 :(得分:0)
已修复,似乎只是某个时间错误......