Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sprite在libgdx中未附加实体_Libgdx_Sprite_Box2d - Fatal编程技术网

Sprite在libgdx中未附加实体

Sprite在libgdx中未附加实体,libgdx,sprite,box2d,Libgdx,Sprite,Box2d,我想让精灵附在一个多边形的身体上。当我运行代码时,精灵和身体都会显示出来,但它们没有相互链接。我看了所有的堆栈溢出问题,但没有任何帮助。 我在一个类中创建它,并从主类中实例化它。 这是我的密码 public SpriteButtons(PlayScreen screen) { this.world = screen.getWorld(); batch = new SpriteBatch(); region = new TextureRegion(screen.getAt

我想让精灵附在一个多边形的身体上。当我运行代码时,精灵和身体都会显示出来,但它们没有相互链接。我看了所有的堆栈溢出问题,但没有任何帮助。 我在一个类中创建它,并从主类中实例化它。 这是我的密码

public SpriteButtons(PlayScreen screen) {

    this.world = screen.getWorld();
    batch = new SpriteBatch();
    region = new TextureRegion(screen.getAtlas().findRegion("fireball"), 0, 0, 16, 16);
    sprite = new Sprite(region);
    this.gamecame = new OrthographicCamera();
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;

    bodyDef.position.set(0, 0);

    body = world.createBody(bodyDef);

    PolygonShape shape = new PolygonShape();

    shape.setAsBox(0.2f, 0.2f);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1f;
    sprite.setSize(25, 25);
    Fixture fixture = body.createFixture(fixtureDef);

}

public OrthographicCamera getGamecame() {
    return gamecame;
}

public void render(float dt) {
    sprite.setPosition(body.getPosition().x - sprite.getWidth()/2f,
            body.getPosition().y - sprite.getHeight()/2f );
    Gdx.app.log("" + body.getPosition().x, "");
    batch.begin();
    sprite.draw(batch);
    setRegion(region);
    batch.end();
}
我的输出如下:


badlogic精灵没有连接多边形主体,我正在从main类实例化这个类,我将从main类render方法调用这个类render方法。对不起,如果这个问题看起来很愚蠢。我不知道我在哪里错过了这个。请帮忙。。!!提前谢谢

必须根据
渲染
方法中的主体位置更新精灵的位置-如果不这样做,精灵如何知道它已连接到主体

请注意,当libgdx的精灵的原点位于botto左角时,box2d主体的原点位于主体的中心。然后你需要减去身体位置宽度和高度的一半

这就是你要做的:

    //render()
    sprite.setPosition(body.getPosition().x - sprite.getWidth()/2f,
                       body.getPosition().y - sprite.getHeight()/2f );

    ...
    sprite.draw(batch);
    ...
试着这样做
batch.draw(sprite,sprite.getX(),sprite.getY())并将物体的初始位置更改到上面的某个位置,这样它就会下落,您可以通过初始化world来检查重力,如
world=new world(new Vector2(0,-98f),true)