攻击范围
AttackAbleObject.java
package character; import sprite.Transform; import java.awt.*; import java.awt.geom.AffineTransform; public abstract class AttackAbleObject extends GameObject{ private Transform _transform; private int _cx1; private int _cx2; private int _cy1; private int _cy2; public AttackAbleObject(Transform trans, int hurt_x, int hurt_y, int hurtscale_x, int hurtscale_y, int hitoffset_x1, int hitoffset_y1, int hitoffset_x2, int hitoffset_y2) { super(trans,hurt_x,hurt_y,hurtscale_x, hurtscale_y); _transform = trans; this.setHitBoxOffsets(hitoffset_x1,hitoffset_y1,hitoffset_x2,hitoffset_y2); } public Shape getHitBox(){ AffineTransform as = new AffineTransform(); as.setTransform(_transform.getTransform()); return as.createTransformedShape(new Rectangle(_cx1,_cy1,_cx2,_cy2)); } public void setHitBoxOffsets(int x1,int y1, int x2, int y2){ _cx1 = x1; _cx2 = x2; _cy1 = y1; _cy2 = y2; } }
Player.java继承AttackAbleObject,在构造函数中传入player的攻击范围参数(最后4个参数)
public class Player extends AttackAbleObject{ public Player(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); }
最后GameApp.java中,player的构造方式改为如下:
_player = new Player(animator,60,44,18,98 ,65,60,50,11);
我们可以通过以下调用以下方法,可视化player的攻击范围
g.draw(_player.getHitBox());
如果您迷路了,请参考完整源码: