【发布时间】:2019-10-25 11:16:55
【问题描述】:
尝试制作游戏 Tank Trouble 的副本,但我不知道如何对子弹和墙壁/玩家进行碰撞检测。按下鼠标时会生成子弹,但我不知道如何让它们在击中物体后消失。
试图让它们在接触时融入背景,但游戏开始严重滞后。还尝试了人们用来从 arrayLists 中删除项目的向后循环,但无济于事。
PVector player = new PVector (300, 400);
ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
float maxSpeed = 3; //speed of bullets
void setup() {
size(800, 600);
fill(0);
}
void draw() {
background(255);
line(20, 200, 400, 200);
rect(300, 400, 50, 50);
//creates an aiming tool for the players
PVector mouse = new PVector(mouseX, mouseY);
fill(255);
ellipse(mouse.x, mouse.y, 8, 8);
if (frameCount%5==0 && mousePressed) {
PVector dir = PVector.sub(mouse, player);
dir.normalize();
dir.mult(maxSpeed*3);
Bullet b = new Bullet(player, dir);
bullets.add(b);
}
for (Bullet b : bullets) {
b.update();
b.display();
}
}
class Bullet extends PVector {
PVector vel;
Bullet(PVector loc, PVector vel) {
super(loc.x, loc.y);
this.vel = vel.get();
}
void update() {
add(vel);
}
void display() {
fill(0);
ellipse(x, y, 5, 5);
}
float bulletX() {
return x;
}
}
基本上希望子弹在最后一次触摸消失之前弹跳 3-4 次。如果它在任何时候接触到玩家,他们都应该消失。
【问题讨论】:
-
看看下面的链接---stackoverflow.com/questions/5737484/…
-
欢迎来到 SO。您的问题是碰撞检测还是从列表中删除项目?
标签: java arraylist processing collision-detection bullet