1
9 package version26;
10
11 public class Monster extends Actor {
12 protected int vx;
13 protected static final double FIRING_FREQUENCY = 0.01;
14
15 public Monster(Stage stage) {
16 super(stage);
17 setSpriteNames( new String[] {"bicho0.gif","bicho1.gif"});
18 setFrameSpeed(35);
19 }
20
21 public void act() {
22 super.act();
23 x+=vx;
24 if (x < 0 || x > Stage.WIDTH)
25 vx = -vx;
26 if (Math.random()<FIRING_FREQUENCY)
27 fire();
28 }
29
30 public int getVx() { return vx; }
31 public void setVx(int i) {vx = i; }
32
33 public void collision(Actor a) {
34 if (a instanceof Bullet || a instanceof Bomb) {
35 remove();
36 spawn();
37 stage.getPlayer().addScore(20);
38 }
39 }
40
41 public void spawn() {
42 Monster m = new Monster(stage);
43 m.setX( (int)(Math.random()*Stage.WIDTH) );
44 m.setY( (int)(Math.random()*Stage.PLAY_HEIGHT/2) );
45 m.setVx( (int)(Math.random()*20-10) );
46 stage.addActor(m);
47 }
48
49 public void fire() {
50 Laser m = new Laser(stage);
51 m.setX(x+getWidth()/2);
52 m.setY(y + getHeight());
53 stage.addActor(m);
54
55 }
56 }
57