4.1 rect多次创建问题

master
terry 3 years ago
parent 6a5500d82f
commit 8a420a013d

Binary file not shown.

@ -11,6 +11,7 @@ public class Bullet {
private boolean live = true; private boolean live = true;
private Group group = Group.BAD; private Group group = Group.BAD;
private TankFrameV4 tf; private TankFrameV4 tf;
Rectangle rect = new Rectangle();
public Bullet(int x, int y, Direction direction, Group group, TankFrameV4 tf) { public Bullet(int x, int y, Direction direction, Group group, TankFrameV4 tf) {
this.x = x; this.x = x;
@ -18,6 +19,11 @@ public class Bullet {
this.direction = direction; this.direction = direction;
this.group = group; this.group = group;
this.tf = tf; this.tf = tf;
rect.x = this.x;
rect.y = this.y;
rect.width = Bullet.WIDTH;
rect.height = Bullet.HEIGHT;
} }
public void paint(Graphics g){ public void paint(Graphics g){
@ -57,15 +63,15 @@ public class Bullet {
if(x < 0 || y < 0 || x > TankFrameV4.GAME_WIDTH || y > TankFrameV4.GAME_HEIGHT){ if(x < 0 || y < 0 || x > TankFrameV4.GAME_WIDTH || y > TankFrameV4.GAME_HEIGHT){
live = false; live = false;
} }
rect.x = this.x;
rect.y = this.y;
} }
//检测是否跟坦克碰撞 //检测是否跟坦克碰撞
public void collideWith(Tank tank){ public void collideWith(Tank tank){
//关闭队友伤害 //关闭队友伤害
if(this.group == tank.getGroup()) return; if(this.group == tank.getGroup()) return;
Rectangle bulletRect = new Rectangle(x, y, WIDTH, HEIGHT); if(rect.intersects(tank.rect)){
Rectangle tankRect = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
if(bulletRect.intersects(tankRect)){
tank.die(); tank.die();
this.die(); this.die();
//爆炸 //爆炸

@ -14,6 +14,7 @@ public class Tank {
public static final int WIDTH = ResourceManager.tankD.getWidth(); public static final int WIDTH = ResourceManager.tankD.getWidth();
public static final int HEIGHT = ResourceManager.tankD.getHeight(); public static final int HEIGHT = ResourceManager.tankD.getHeight();
private Random random = new Random(); private Random random = new Random();
Rectangle rect = new Rectangle();
public Tank(int x, int y, Direction dir, Group group, TankFrameV4 tankFrame) { public Tank(int x, int y, Direction dir, Group group, TankFrameV4 tankFrame) {
this.x = x; this.x = x;
@ -21,6 +22,11 @@ public class Tank {
this.dir = dir; this.dir = dir;
this.group = group; this.group = group;
this.tankFrame = tankFrame; this.tankFrame = tankFrame;
rect.x = this.x;
rect.y = this.y;
rect.width = Tank.WIDTH;
rect.height = Tank.HEIGHT;
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -69,6 +75,9 @@ public class Tank {
//边界检测 //边界检测
boundsCheck(); boundsCheck();
rect.x = this.x;
rect.y = this.y;
} }
private void boundsCheck() { private void boundsCheck() {

Loading…
Cancel
Save