2015-10-16 63 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