1 回答

TA貢獻1895條經驗 獲得超3個贊
萬一讀到這篇文章的人很好奇,我創(chuàng)建了一個新函數來處理運動并將碰撞轉換為布爾值,如果玩家與塊(墻)碰撞,則返回 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();
}
添加回答
舉報