我正在Android Studio中进行一个简单的游戏,并且我正在调用GameOver活动,并且再次尝试按钮形式的游戏活动(它只是名为MainActivity)。当主角与其中一个怪物发生碰撞时被调用。我的问题是,当我尝试以((Activity)getContext()).finish();
完成时,我的应用程序关闭,而不是开始新的活动。这是启动它的方法:为什么我的应用程序在从自定义视图启动Activity后关闭?
public void isCharacterDead(){
for (Monster item : monsters) {
if (characters.get(0).deadCollision.contains(item.collision.centerX(), item.collision.centerY())) {
player.stop();
long gameOverDelayEnd = (System.nanoTime()/1000000)+1000;
long gameOverDelayStart = System.nanoTime()/1000000;
while(gameOverDelayStart < gameOverDelayEnd){
gameOverDelayStart = System.nanoTime()/1000000;
}
characters.remove(0);
Intent gameOver = new Intent(getContext(),GameOver.class);
gameOver.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity)getContext()).finish();
getContext().startActivity(gameOver);
}
}
}
而且我GAMEOVER活动是:
public class GameOver extends Activity {
private Button tryAgain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//removing the title from the screen in order to have empty screen
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setContentView(R.layout.activity_game_over);
tryAgain = (Button) findViewById(R.id.tryagain);
tryAgain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent game = new Intent(GameOver.this, MainActivity.class);
GameOver.this.finish();
startActivity(game);
}
});
}
}
现在,如果我不使用((Activity)getContext()).finish();
,它开始在GAMEOVER活动,当再次尝试点击我移动到游戏活动的新实例(MainActivity)。但它会通过GameOver中的“再试一次”按钮来降低每次新开始时的性能。我想这是因为在没有完成最后一场比赛的情况下开始新的比赛活动。有关如何避免这种情况的任何建议?作为例外,android监视器内没有任何内容(我的GPU监视器也被禁用)。
为什么要使用Intent.FLAG_ACTIVITY_NEW_TASK并在写你的代码? getContext()方法如何工作? – ammcom
isCharacterDead()位于自定义视图内。在每个精灵位置的帧更新后调用它。 –