Enemy
Enemy.java
package character;
import managers.InputManager;
import sprite.Animator;
import sprite.Transform;
import java.awt.*;
import java.awt.event.KeyEvent;
public class Enemy extends AttackAbleObject{
private Animator _animator;
public Enemy(Animator animator, int hurtx, int hurty, int hurtscale_x, int hurtscale_y
, int hitoffset_x1, int hitoffset_y1, int hitoffset_x2, int hitoffset_y2){
super(animator,hurtx,hurty,hurtscale_x,hurtscale_y
,hitoffset_x1,hitoffset_y1,hitoffset_x2,hitoffset_y2);
_animator = animator;
_animator.setAnimation("idle");
}
public void move(double x, double y){
}
@Override
public void update(Graphics2D g){
_animator.show(g);
}
public Transform getTransform(){
return _animator;
}
}
因为Enemy能攻击player,也能受到攻击,所以咱们让他继承AttackAbleObject
在GameApp中初始化一个Enemy对象如下:
private Enemy _enemy;
private Player _player;
public GameApp(){
try{
background = new Background();
_resLoader.loadAnimations();
Animator animatorEnemy = new Animator("enemy3");
_enemy = new Enemy(animatorEnemy,88, 38, 18, 88, // HurtBox
93, 54, 50, 11);
_enemy.getTransform().setOrigin(97, 110);
_enemy.getTransform().scale(2,2);
g.setColor(Color.red);
g.draw(_enemy.getHurtBox());
g.draw(_enemy.getHitBox());
_enemy.update(g);
同时我们在Animationlist.xml中新增enemy配置信息:
<enemy3>
<walk>
<image>zombie4/02.png</image>
<image>zombie4/03.png</image>
<image>zombie4/04.png</image>
<image>zombie4/05.png</image>
<image>zombie4/06.png</image>
<image>zombie4/07.png</image>
<image>zombie4/08.png</image>
<image>zombie4/09.png</image>
</walk>
<idle>
<image>zombie4/01.png</image>
<image>zombie4/01.png</image>
<image>zombie4/01.png</image>
<image>zombie4/22.png</image>
<image>zombie4/22.png</image>
<image>zombie4/22.png</image>
</idle>
<attk>
<image>zombie4/10.png</image>
<image>zombie4/11.png</image>
<image>zombie4/12.png</image>
<image>zombie4/13.png</image>
<image>zombie4/14.png</image>
</attk>
<block>
<image>zombie4/23.png</image>
<image>zombie4/23.png</image>
<image>zombie4/23.png</image>
<image>zombie4/23.png</image>
<image>zombie4/23.png</image>
<image>zombie4/23.png</image>
</block>
<die>
<image>zombie4/22.png</image>
<image>zombie4/23.png</image>
<image>zombie4/36.png</image>
<image>zombie4/37.png</image>
<image>zombie4/38.png</image>
<image>zombie4/39.png</image>
<image>zombie4/40.png</image>
<image>zombie4/41.png</image>
<image>zombie4/41.png</image>
<image>zombie4/41.png</image>
</die>
<dead>
<image>zombie4/41.png</image>
</dead>
</enemy3>
如果您迷路了,请参考完整源码: