1 回答
TA贡献1895条经验 获得超3个赞
万一读到这篇文章的人很好奇,我创建了一个新函数来处理运动并将碰撞转换为布尔值,如果玩家与块(墙)碰撞,则返回 true,如果与其他任何东西碰撞,则返回 false。
我还更改了对块碰撞的检查,如代码中所示
private boolean collision(double vx, double vy) {
for(int i = 0; i< handler.object.size(); i++) {
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ID.Block) {
if(new Rectangle((int)x+(int)vx,(int)y+(int)vy,31,31).intersects(tempObject.getBounds())) {
return true;
}
}
if(tempObject.getId() == ID.Crate) {
if(getBounds().intersects(tempObject.getBounds())) {
main.ammo+=50;
handler.removeObject(tempObject);
}
}
if(tempObject.getId() == ID.Enemy) {
if(getBounds().intersects(tempObject.getBounds())) {
main.hp--;//TODO make it so you cant get stuck in enemies and they drain all your health
}
}
}
return false;
}
和移动功能
public void Move(double vx, double vy) {
if(!collision(vx,vy)) {
x+=vx;
y+=vy;
}
}
tick 方法也略有改变
public void tick() {
collision(vx,vy);
vx = 0;
vy = 0;
//movement
if(handler.isUp()) vy -=5;
//else if(!handler.isDown()) vy = 0;
if(handler.isDown()) vy += 5;
//else if(!handler.isUp()) vy = 0;
if(handler.isRight()) vx += 5;
//else if(!handler.isLeft()) vx = 0;
if(handler.isLeft()) vx -=5;
//else if(!handler.isRight()) vx = 0;
if(vx!=0||vy!=0){
Move(vx,0);
Move(0,vy);
}
//anim.runAnimation();
}
添加回答
举报
