我從我的第一款游戲開始,但無法弄清楚碰撞檢測應該如何工作。目標是玩家不應該能夠進入墻壁。我有以下類玩家(),墻(),級別()和游戲()玩家類是加載玩家并為玩家處理移動和碰撞。墻正在創建墻,我在這個類中有一個Arraylist,它正在為創建的每一面墻創建一個矩形。級別類正在加載創建和加載所有級別。游戲類是處理整個游戲循環等等。我試圖將x和y位置與玩家和墻壁進行比較,現在我正在嘗試使用尚未成功的.intersect類。玩家類的一部分:public void collision() {Rectangle r3 = getOffsetBounds();for (int i = 0; i < Wall.wallList.size(); i++) { Rectangle w1 = Wall.wallList.get(i); if (r3.intersects(w1)) { }}}public int moveUp(int velY) { collision(); y -= velY; return y;}public int moveDown(int velY) { collision(); y += velY; return y;}public int moveLeft(int velX) { collision(); x -= velX; return x;}public int moveRight(int velX) { collision(); x += velX; return x;}public Rectangle getOffsetBounds() { return new Rectangle(x + velX, y + velY, width, height);}public int getX() { return x;}public void setX(int x) { this.x = x;}public int getY() { return y;}public void setY(int y) { this.y = y;}public int getVelX() { return velX;}public void setVelX(int velX) { this.velX = velX;}public int getVelY() { return velY;}public void setVelY(int velY) { this.velY = velY;}}墻面類的一部分:static ArrayList<Rectangle> wallList = new ArrayList<Rectangle>();public int getX() { return x;}public void setX(int x) { this.x = x;}public int getY() { return y;}public void setY(int y) { this.y = y;}public void setVisisble(Boolean visible) { this.visible = visible;}public Rectangle getBounds() { return new Rectangle(x,y,width,height);}public Rectangle setBounds(int x,int y,int width,int height) { this.x = x; this.y = y; this.width = width; this.height = height; return new Rectangle(x,y,width,height);}}設置墻列表的“級別”類的一部分 if(level1[i][j]==1) { w = new Wall(j*25, i*25, width, height); w.paint(g); Wall.wallList.add(w.setBounds(j*25+10, i*25+10, width, height)); }
1 回答

守著星空守著你
TA貢獻1799條經驗 獲得超8個贊
這就是我總是如何處理任何碰撞
if (((x1<x2 && x1+w1>x2) || (x2<x1 && x2+w2>x1)) && ((y1<y2 && y1+h1>y2) || (y2<y1 && y2+h2>y1)))
添加回答
舉報
0/150
提交
取消