2015-10-16 91 views
1

enter image description herelibGDX Box2D的停止移動時觸摸牆

當我Body對象與牆壁接觸。我認爲,右鍵這一點。所以只要我按住右鍵就會停止。

當我釋放我的右鍵時,它下降了。如何解決這個問題?

這裏我的腳本:

World定義:

this.world = new World(new Vector2(0, -9.8f), true); 

從tilemap的創建ground體:

//create body and fixture variables 
BodyDef bdef = new BodyDef(); 
PolygonShape shape = new PolygonShape(); 
FixtureDef fdef = new FixtureDef(); 
Body body; 

//create ground bodies/fixtures 
for(MapObject object : tileMap.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)){ 
    rect = ((RectangleMapObject) object).getRectangle(); 

    x = Static.toMeter(rect.getX() + rect.getWidth()/2); 
    y = Static.toMeter(rect.getY() + rect.getHeight()/2); 
    bdef.type = BodyDef.BodyType.StaticBody; 
    bdef.position.set(x, y); 

    body = world.createBody(bdef); 

    x = Static.toMeter(rect.getWidth()/2); 
    y = Static.toMeter(rect.getHeight()/2); 
    shape.setAsBox(x, y); 
    fdef.shape = shape; 
    fdef.filter.categoryBits = HookaHookaGame.GROUND_BIT; 
    body.createFixture(fdef); 
} 

Player主體定義:

BodyDef bodyDef = new BodyDef(); 
bodyDef.position.set(Static.toMeter(128), Static.toMeter(HookaHookaGame.HEIGHT)); 
bodyDef.type = BodyDef.BodyType.DynamicBody; 
body = world.createBody(bodyDef); 

// Define mario shape 
PolygonShape shape = new PolygonShape(); 
shape.setAsBox(Static.toMeter(32)/2, Static.toMeter(32)/2); 

FixtureDef fixture = new FixtureDef(); 
fixture.shape = shape; 

body.createFixture(fixture); 

// Define foot shape 
shape = new PolygonShape(); 
shape.setAsBox(Static.toMeter(32/4)/2, Static.toMeter(32/4)/2, new Vector2(0, Static.toMeter((-32 + 8)/2)), 0); 

fixture = new FixtureDef(); 
fixture.shape = shape; 
// Create filter 
fixture.filter.categoryBits = HookaHookaGame.MARIO_BIT; 
fixture.filter.maskBits = HookaHookaGame.GROUND_BIT; 

body.createFixture(fixture).setUserData(this); 

ContactListener這裏我beginContact()方法:

int cDef; 

Fixture a, b; 
MySprite spriteA, spriteB; 

a = contact.getFixtureA(); 
b = contact.getFixtureB(); 

cDef = a.getFilterData().categoryBits | b.getFilterData().categoryBits; 

switch (cDef) { 
    case HookaHookaGame.GROUND_BIT | HookaHookaGame.MARIO_BIT: 
     System.out.println("Foot with ground"); 
     if(a.getUserData() != null) { 
      spriteA = (MySprite) a.getUserData(); 
      spriteA.onHit(); 
     } 

     if(b.getUserData() != null) { 
      spriteB = (MySprite) b.getUserData(); 
      spriteB.onHit(); 
     } 
     break; 
} 

處理用戶輸入:

private void handleInput() { 
    //control our player using immediate impulses 
    if (Gdx.input.isKeyPressed(Input.Keys.D)) 
     player.moveRight(); 
    else if (Gdx.input.isKeyPressed(Input.Keys.A)) 
     player.moveLeft(); 
    else 
     player.stopMove(); 

    if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) 
     player.jump(); 
} 

我看過他的教程,它似乎他沒有做任何事情,它工作正常。他的源代碼GitHub

+0

你如何處理輸入(移動)? –

+0

@PinkieSwirl更新。只需使用Gdx.input.isKeyPressed()。 – Kenjiro

回答

2

問題是你的線性衝動很強。

你可以用Mario項目測試這個,如果你增加衝動,你也會卡在牆上。

爲了解決這個問題,要麼降低脈衝,降低摩擦力(但是你可能需要停止馬里奧的代碼),或者不使用線性衝動,而是應用火炬並使用圓形讓玩家「滾動」 。

這些解決方案都不是完美的,但您可以嘗試去看哪一個最適合您。

+0

我已經在方向上(左/右)嘗試過'isKeyJustPressed()'。當我實現這一點時,我需要保持迅速行動。所以,這不適用於我握住鑰匙的時候。這個解決方案適用於'jump()'動作。爲了圈子。我已經嘗試過這種形狀並得到了相同的結果。 :) – Kenjiro

+0

對不起,答案確實是錯的。相應地更新答案。 –

+1

編輯摩擦解決我的問題。謝謝@Pinkie Swirl:D – Kenjiro