2015-04-01 53 views
0

我正在開發Libgdx碰撞檢測機制,我嘗試使用聯繫人偵聽器來實現它,但它在我的代碼中沒有任何作用。聯繫人監聽器在Libgdx中沒有任何作用

這裏是我的代碼

public class FirstLevel implements Screen{ 
...... 
private Player player; 
...... 
... 
Gdx.input.setInputProcessor(player); 
world.setContactListener(player); 
.... 
} 

「玩家」 是實現contactListener和InputProcessor類。

這裏是球員類

公共類播放器實現屏幕,InputProcessor,ContactListener {

private Body polybody,polybodys; 
private Player player; 
private World world; 
boolean Colliding ; 
private Body enemybody; 
private Sprite polysprite; 
public final float width,height; 
private Rectangle rectangle; 
private Vector2 movement=new Vector2(); 
private float speed=580; 
private Body rec; 

    public Player(World world,float x,float y,float width) 
    { 
    this.width=width; //IMP 
    height=width*2; 
    BodyDef polygon=new BodyDef(); 
    polygon.type=BodyType.DynamicBody; 
    polygon.position.set(x,y); // 
    PolygonShape poly =new PolygonShape(); 
    poly.setAsBox(width/2,height/2); // 
    polygon.position.set(5,4); 
    FixtureDef polyfixture=new FixtureDef(); 
    polyfixture.shape=poly; 
    polyfixture.friction=0.8f; 
    polyfixture.restitution=0.1f; 
    polyfixture.density=3; 


    //creating actual body 
    polybody=world.createBody(polygon); 
    polybody.createFixture(polyfixture).setUserData(polygon); 
    polysprite=new Sprite(new Texture("img/car.jpg")); 
    polysprite.setSize(2, 3); //size of mario 
    polysprite.setOrigin(polysprite.getWidth()/2, polysprite.getHeight()/2); 
    polybody.setUserData(polysprite); 
    BodyDef polygons=new BodyDef(); 
    polygons.type=BodyType.DynamicBody; 
    PolygonShape polys=new PolygonShape(); 
    polys.setAsBox(2,2); 
    FixtureDef polyxfixture=new FixtureDef(); 
    polyxfixture.shape=polys; 
    polyxfixture.friction=0.8f; 
    polyxfixture.restitution=0.1f; 
    polyxfixture.density=3; 
    polybodys=world.createBody(polygons); 
    polybodys.createFixture(polyxfixture).setUserData(polygons); 
    poly.dispose();  
    } 
    public void update() 
     { 
    polybody.applyForceToCenter(movement, true); 
    polybodys.applyForceToCenter(movement,true); 
     } 

    public Body getBody(){ 
    { 
    return polybody; 
    } 
    } 

    @Override 
    public boolean keyDown(int keycode) { 
// TODO Auto-generated method stub 
    switch (keycode) { 
    case Keys.W: 
     movement.y=speed; 
    break; 
    case Keys.S: 
     movement.y=-speed; 
    break; 
    case Keys.D: 
     movement.x=speed; 
    break; 
    case Keys.A: 
     movement.x=-speed; 
    } 
    return true; 
    } 

    @Override 
    public boolean keyUp(int keycode) { 
// TODO Auto-generated method stub 
    switch (keycode) { 
    case Keys.W: 
     movement.y=0; 
    break; 
    case Keys.S: 
     movement.y=0; 
    break; 
    case Keys.D: 
     movement.x=0; 
    break; 
case Keys.A: 
    movement.x=0; 
} 
    return true; 
    } 

    @Override 
    public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
// TODO Auto-generated method stub 
movement.x =speed; 
Gdx.app.log("Example", "touch done "); 
    return true; 

    } 

    @Override 
    public void beginContact(Contact contact) { 
// TODO Auto-generated method stub 

Fixture fixtureA=contact.getFixtureA(); 
Fixture fixtureB=contact.getFixtureB(); 
if(fixtureA.getUserData() != null && fixtureA.getUserData().equals(polybody) && 
     fixtureB.getUserData() !=null && fixtureB.getUserData().equals(polybodys)){ 
    Gdx.app.log("Contact","1"); 
    System.out.println("its colliding"); 

} 

if(fixtureB.getUserData() !=null && fixtureB.getUserData().equals(polybodys) && 
     fixtureA.getUserData() !=null && fixtureA.getUserData().equals(polybody)) 
{ 
    System.out.println("its colliding"); 
} 

    } 
    } 

這是我的代碼和 「polybody和polybodys」 是兩個機構。我想檢測兩個物體之間的碰撞。當身體碰撞時,我無法檢測到任何碰撞。請幫忙。提前致謝。

回答

0

您設置的polybodyuserDatapolysprite

polybody.setUserData(polysprite); 

但是,在碰撞檢測代碼,你檢查不同類型的userData

if(fixtureB.getUserData() !=null && fixtureB.getUserData().equals(polybodys) && 
     fixtureA.getUserData() !=null && fixtureA.getUserData().equals(polybody)) 

所以條件將永遠是false。看來這個條件:

fixtureA.getUserData() !=null && fixtureA.getUserData().equals(polybody)) 

應該是這樣的:

fixtureA.getUserData() !=null && fixtureA.getUserData().equals(polysprite)) 
相關問題