2013-12-20 4 views
0

저는 커브 열과 같은 게임을 만들기 위해 libGDX를 사용하지만 안드로이드는 사용하고 있습니다.
사실 나는 충돌을 위해 노력하고 있지만 시도하는 모든 것은 작동하지 않습니다. 나는 Rectangle 클래스의 overlaps 메서드를 사용합니다.
항상 false을 반환합니다. 여기 오버랩 방법이 작동하지 않습니다

는 충돌이 있는지 확인하는 코드입니다 :

public void collision() { 
    if(head.getAlive()) { 
     for (TailSnake tail : getTail()) { 
      if (tail.lifeTime > 2) 
       if (head.bounds.overlaps(tail.bounds)) 
        head.dead(); 
     } 
     for (Object wallObj : getWall()) { 
      Wall wall = (Wall) wallObj; 
      if (head.bounds.overlaps(wall.bounds)) 
       head.dead(); 
     } 
    } 
} 

TailSnake의 코드 :

package com.me.mygdxgame; 

import com.badlogic.gdx.math.Rectangle; 
import com.badlogic.gdx.math.Vector2; 

public class TailSnake { 

    static final float SIZE = 0.1f; 

    Vector2 position = new Vector2(); 
    Rectangle bounds = new Rectangle(); 
    float lifeTime = 0; 

    public TailSnake(Vector2 position) { 
     this.position = position; 
     this.bounds.height = SIZE; 
     this.bounds.width = SIZE; 
    } 

    public Rectangle getBounds() { 
     return bounds; 
    } 

    public Vector2 getPosition() { 
     return position; 
    } 

    public void update(float delta) { 
     lifeTime += delta; 
    } 
} 

그리고 마지막으로 HeadSnake의 코드 :

package com.me.mygdxgame; 

import com.badlogic.gdx.math.Rectangle; 
import com.badlogic.gdx.math.Vector2; 

public class HeadSnake extends TailSnake{ 

    static final float SPEED = 1f; 
    static final double ROTATE_SPEED = 200; 

    Rectangle bounds = new Rectangle(); 
    Vector2 velocity = new Vector2(); 
    boolean alive = true; 

    public HeadSnake(Vector2 position) { 
     super(position); 
     this.velocity.x = 0; 
     this.velocity.y = SPEED; 
    } 

    public Vector2 getVelocity() { 
     return velocity; 
    } 

    public boolean getAlive() { 
     return alive; 
    } 

    public void update(float delta) { 
     if (alive) 
      position.add(velocity.cpy().scl(delta));   
    } 

    public void dead() { 
     this.alive = false; 
    } 
} 

답변

0

당신은 업데이트되지 않습니다 경계. 업데이트 방법에서 위치를 설정 한 후 범위를 업데이트하십시오. 좋아요 -

bounds.x = position.x; 
bounds.y = position.y; 
bounds.x -= bounds.width/2f; 
bounds.y -= bounds.height/2f; 
+0

감사합니다. 왜 내가 이것에 대해 생각하지 않는지 모르겠다. – Poroing

+0

@ user2984573 도와 드리겠습니다. :) –

관련 문제