第一步优化定时器
public void startGameTask(){
bgRoll= new BgRoll();
shootTask = new ShootTask();
enemyTask =new EnemyTask();
hitTask = new HitTask();
timer.schedule(bgRoll, 0, 50);
timer.schedule(shootTask, 0, 500);
timer.schedule(enemyTask, 0, 1000);
timer.schedule(hitTask,0,50);
}
public void pauseGameTask(){
bgRoll.cancel();
shootTask.cancel();
enemyTask.cancel();
hitTask.cancel();
}
//添加事件,改变游戏状态
super.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
switch (gameState) {
//当游戏状态是START 点击 变成RUNNING
case START:
case PAUSE:
gameState = GameState.RUNNING;
startGameTask();
break;
case RUNNING:
gameState = GameState.PAUSE;
pauseGameTask();
repaint();
break;
case GAME_OVER:
gameState = GameState.START;
break;
}
}
});
第二步,优化击中敌机效果
class EnemyAirplaneAnimation extends TimerTask{
int num;
@Override
public void run() {
//如果死亡,就播放动画
if(isDead()){
num++;
BufferedImage image = ImageLoaderUtil.load(“enemy”+type+”_down”+num+”.png”);
//EnemyAirplane.super.image = image;
if((type!=4&&num==4)||(type==2&&num==6)){
num = 0;
destroy();
}
setImage(image);
}
}<br />}
Timer timer = new Timer();
timer.schedule(new EnemyAirplaneAnimation(),0, 200);
// 检测敌机是否与子弹碰撞
for(Bullet bullet:bullets){
for(FlySprite enemy:enemys){
//PhysicsManager.isHit(bullet,enemy)
if(bullet.isHit(enemy)){
bullet.destroy();
if(enemy instanceof EnemyAirplane){
enemy.setDead(true);
}
if(enemy instanceof BuffAward){<br /> enemy.destroy();<br /> }<br /> }<br /> }<br />}