1.添加子弹类继承FlySprite 实现ActionSprite
public class Bullet extends FlySprite implements ActionSprite {
public Bullet(int x,int y){<br /> this.init();<br /> super.x = x;<br /> super.y = y;<br /> }
@Override<br /> public void init() {<br /> super.image = ImageLoaderUtil._load_("bullet2.png");<br /> super.width = image.getWidth()/2;<br /> super.height = image.getHeight()/2;<br /> }
@Override<br /> public void destroy() {<br /> //移除集合当中越界子弹对象<br /> FlyPanel._bulletList_.remove(this);<br /> }
int SPEED = 5;<br /> _/**<br /> * 子弹向上运行<br /> */<br /> _@Override<br /> public void up() {<br /> //y 垂直方向 移动, 减 向上<br /> super.y=y-SPEED;<br /> if(super.y<0){<br /> this.destroy();<br /> }<br /> }<br />}
2.英雄类中添加发射子弹方法
FlyPanel类中添加公共的静态子弹列表
//创建子弹集合
public static CopyOnWriteArrayList
Hero玩家类中添加发射子弹的方法
public void shoot() {
int xStep = this.width / 4;
Bullet bullet = new Bullet(this.x + 2 * xStep, this.y - 20);
FlyPanel.bulletList.add(bullet);
}
3.把子弹渲染到页面上
ShootTask shootTask;
class ShootTask extends TimerTask {
@Override
public void run() {
hero.shoot();
//重绘
repaint();
}
}
@Override
public void paint(Graphics g) {
//画背景图片
g.drawImage(background, x, y, bgWidth, bgHeight, null);
paintState(g);
hero.draw(g);
//绘制子弹
for (Bullet b : bulletList) {
b.draw(g);
b.up();
}
}
4.删除越界子弹
@Override
public void up() {
this.y=y-5;
if (this.y < 0) {
this.destroy();
}
}
@Override
public void destroy() {
FlyPanel.bulletList.remove(this);
}
case START:
gameState = GameState.RUNNING;
bgRoll = new BgRoll();
shootTask = new ShootTask();
timer.schedule(bgRoll,0,50);
timer.schedule(shootTask,0,200);
break;
case RUNNING:
gameState= GameState.PAUSE;
bgRoll.cancel();
shootTask.cancel();
repaint();
break;
case PAUSE:
gameState = GameState.RUNNING;
bgRoll = new BgRoll();
shootTask = new ShootTask();
timer.schedule(bgRoll,0,50);
timer.schedule(shootTask,0,200);
break;