【问题标题】:Body in Box2d wont moveBox2d中的身体不会移动
【发布时间】:2016-12-17 14:58:09
【问题描述】:

我制作了这个敌方蜘蛛类,我可以用它来创建敌方蜘蛛。该类确实创建了蜘蛛,但当我调用 move 方法时不会移动......有什么想法吗?我从 Map 类中调用 move 方法。创建了主体和精灵,但由于某种原因它再次不会移动。任何帮助都将不胜感激。

public  SpiderEnemy(World world, float xposition, float yposition)
{
    bdef=new BodyDef();
    fdef=new FixtureDef();
    shape=new PolygonShape();

    xPosition=xposition;
    yPosition=yposition;
    texture=new Texture("spider.png");
    textureRegion=new TextureRegion[1];
    textureRegion[0]=new TextureRegion(texture,55,55,60,60);
    HP=20;
    HitDmg=10;
    body=createBody(world);
}

public Body createBody(World world)
{
    bdef.type=BodyDef.BodyType.KinematicBody;
    bdef.position.set(xPosition+.5f,yPosition+.5f);
    body=world.createBody(bdef);
    shape.setAsBox(textureRegion[0].getRegionWidth()/Mango.PPM/10f,textureRegion[0].getRegionHeight()/Mango.PPM/10f);
    fdef.shape=shape;
    fdef.density=0;
    body.createFixture(fdef);
    fdef.filter.categoryBits=4;
    circle=new CircleShape();
    fdef.shape=circle;
    circle.setRadius(0.5f);
    fdef.isSensor=true;
    body.createFixture(fdef).setUserData("SpiderSensor");
    return body;
}

public void moveUp()
{
    body.applyLinearImpulse(new Vector2(0,speed), body.getWorldCenter(), true);
}

public void moveDown()
{
    System.out.println("MOVE");
    body.applyLinearImpulse(new Vector2(0,-speed), body.getWorldCenter(), true);
}

public void moveForward()
{
    body.applyLinearImpulse(new Vector2(speed,0), body.getWorldCenter(), true);
}

public void moveBack()
{
    body.applyLinearImpulse(new Vector2(-speed,0), body.getWorldCenter(), true);
}

public void stopMoving()
{
    body.setLinearVelocity(0, 0);
}

public float GetHitDmg()
{
    return HitDmg;
}

public void calculateHealth(float Dmg)
{
    HP=HP-Dmg;
}

public float getHP()
{
    return HP;
}

public void killed()
{
    for(int i=0;i<textureRegion.length;i++)
    {
        textureRegion[i]=null;
    }
    fdef.isSensor=false;
}

public void killSpider(World world)
{
    world.destroyBody(body);
}


public void draw(SpriteBatch batch)
{
    if(textureRegion[0]!=null)
    {
        batch.draw(textureRegion[0], xPosition, yPosition,texture.getWidth()/Mango.PPM/2,texture.getHeight()/Mango.PPM/2);
    }
}

【问题讨论】:

    标签: libgdx box2d


    【解决方案1】:

    不要使用body.applyLinearImpulse(new Vector2(-speed,0), body.getWorldCenter(), true);

    使用body.setLinearVelocity(0, -speed);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多