2016-03-08 125 views
0

所以我有這個問題,Rectangle.overlaps不工作出於某種原因,或者我不知道。所有這一切是它繪製一架噴氣機(MyActor)和一顆子彈演員,並觸摸它使子彈移向藍色敵人的演員,我想它到System.out.println(「被觸摸」);如果子彈擊中敵人。我如何檢測2個矩形之間的碰撞? LIBGDX

我只是粘貼整個代碼。

public class MyGdxGame implements ApplicationListener{ 

private Texture texture; 
private Texture bulletTexture; 
private Texture enemyTexture; 
private MyActor myActor; 
private BulletActor bulletActor; 
private EnemyActor enemyActor; 
private Stage stage; 
private BackGroundActor backGroundActor; 

private Rectangle bulletRectangle; 

private Rectangle enemyRectangle; 
float X=500,Y =600; 

float bulletX = X, bulletY = Y+43; 
float enemyX =0, enemyY = 615; 
boolean started; 
int touched = 0; 

public class EnemyActor extends Actor{ 

    @Override 
    public void draw(Batch batch, float parentAlpha) { 
     batch.draw(enemyTexture,enemyX,enemyY); 
    } 

    @Override 
    public void act(float delta) { 
     if(bulletRectangle.overlaps(enemyRectangle)){ 
      System.out.println("Overlaps"); 
     } 
    } 
} 

public class BackGroundActor extends Actor{ 
    public BackGroundActor() { 
     setBounds(0,0,1280,720); 
     addListener(new InputListener(){ 
      @Override 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
       started = true; 
       touched++; 
       System.out.println(touched); 
       return true; 
      } 
     }); 
    } 
} 

public class MyActor extends Actor{ 
    @Override 
    public void draw(Batch batch, float parentAlpha) { 
     batch.draw(texture,X,Y); 
    } 
} 

public class BulletActor extends Actor{ 

    @Override 
    public void draw(Batch batch, float parentAlpha) { 
     batch.draw(bulletTexture,bulletX,bulletY); 
    } 

    @Override 
    public void act(float delta) { 
     if(started) { 
      bulletX -= 3; 
     } 
    } 
} 
@Override 
public void create() { 

    texture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\0001.png"); 
    bulletTexture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\bullet.png"); 
    enemyTexture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\fuel.png"); 

    bulletRectangle = new Rectangle(bulletX,bulletY,bulletTexture.getWidth(),bulletTexture.getHeight()); 
    enemyRectangle = new Rectangle(enemyX,enemyY,enemyTexture.getWidth(),enemyTexture.getHeight()); 

    stage = new Stage(); 
    myActor = new MyActor(); 
    enemyActor = new EnemyActor(); 
    bulletActor = new BulletActor(); 
    backGroundActor = new BackGroundActor(); 
    backGroundActor.setTouchable(Touchable.enabled); 

    stage.addActor(bulletActor); 
    stage.addActor(myActor); 
    stage.addActor(backGroundActor); 
    stage.addActor(enemyActor); 

    Gdx.input.setInputProcessor(stage); 

} 

@Override 
public void dispose() { 
} 

@Override 
public void render() { 
    Gdx.gl.glClearColor(1, 1, 1, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    stage.act(Gdx.graphics.getDeltaTime()); 
    stage.draw(); 

    if(enemyRectangle.overlaps(bulletRectangle)){ 
     System.out.println("HIT I SAY"); 
    } 

} 

@Override 
public void resize (int width, int height){ 

} 

@Override 
public void pause() { 

} 

@Override 
public void resume() { 

} 

}

回答

0

這裏的問題是,bulletRectangle沒有更新與新的位置的項目符號,bulletX不直接鏈接到bulletRectangle

你可以做

if(started) { 
    bulletX -= 3; 
    bulletRectangle.setX(bulletX); 
} 

你可能想也更新與敵人

+0

Th男人!忘記所有關於更新的事情。 – WannabeHacker

0

按照java documentation,有一種方法intersects(Rectangle r)。您可以使用它來確定兩個矩形是否相交。看看here

+2

添加此代碼是不與libGDX [Rectangle]相同的矩形(https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Rectangle.html#overlaps-com.badlogic.gdx.math.Rectangle-) – Hacketo

0
if(enemyRectangle.overlaps(bulletRectangle)){ 
    System.out.println("HIT I SAY"); 
} 

的適當位置的其他矩形的更新方法