Libgdx:演员身体

时间:2014-04-13 18:18:02

标签: java android libgdx box2d actor

我是libgdx的初学者,我正在寻找答案如何将演员和身体(box2d)链接很长时间,所以请帮助我:(

我有以下代码:

/// CLASS ACTOR
public class MyActor extends Actor
    {
        Texture texture;
        float actorX = 0, actorY = 0;
        public boolean clicked = false;
        public String id;

        public MyActor(float x, float y, String id, String tekstura)
        {
            this.texture = new Texture(Gdx.files.internal(tekstura));
            this.id = id;
            actorX = x;
            actorY = y;

            setBounds(actorX, actorY, texture.getWidth(), texture.getHeight());
            addListener(new InputListener()
            {
                public boolean touchDown(InputEvent event, float x, float y,
                        int pointer, int button)
                {
                    MyActor co = ((MyActor) event.getTarget());
                    co.clicked = true;
                    System.out.println(co.id);
                    co.remove();
                    return true;
                }
            });
        }


        @Override
        public void draw(SpriteBatch batch, float alpha)
        {
            batch.draw(texture, actorX, actorY);
        }


    }

/*....................
.......................
........................
..........................*/
//CREATING SIMPLE OBJECT

        MyActor samolot1 = new MyActor(100, 300, "samolot1", "data/jet.png");
        samolot1.setTouchable(Touchable.enabled);
        stage.addActor(samolot1);


        // //////////////// WORLD /////////////////////////////////////////////

        // 1
        BodyDef bodydef_mojapostac = new BodyDef();
        bodydef_mojapostac.type = BodyType.DynamicBody;
        bodydef_mojapostac.position.set(400, 100);

        CircleShape shape_mojapostac = new CircleShape();
        shape_mojapostac.setRadius(30);

        FixtureDef fixturedef_mojapostac = new FixtureDef();
        fixturedef_mojapostac.density = 0.1f;
        fixturedef_mojapostac.friction = 0.8f;
        fixturedef_mojapostac.restitution = 0.7f;
        fixturedef_mojapostac.shape = shape_mojapostac;

        Body BodyMojaPostac = world.createBody(bodydef_mojapostac);
        BodyMojaPostac.createFixture(fixturedef_mojapostac);


        BodyMojaPostac.setUserData(samolot1);

和render()

......
batch.begin();
        world.getBodies(tmpBodies);
        for (Body body : tmpBodies)
            if (body.getUserData() != null)
            {
                System.out.println(body.getUserData());
                MyActor dupa = (MyActor) body.getUserData();
                batch.draw(dupa.texture, dupa.actorX, dupa.actorY);
            }
        batch.end();
.....

我可以用精灵链接身体,但我不知道如何与演员:(

1 个答案:

答案 0 :(得分:-1)

在遇到这个组合的麻烦之后我也想分享我的解决方案:

public class PhysicsActor extends Image {
private Body body;
private Fixture fixture;

public PhysicsActor(World world, BodyDef bodyDef, FixtureDef fixtureDef, TextureRegion textureRegion) {
    super(textureRegion);

    body = world.createBody(bodyDef);
    setFixture(getBody().createFixture(fixtureDef));
}

@Override
public void draw(Batch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
}

public Body getBody() {
    return body;
}

public Fixture getFixture() {
    return fixture;
}

我这样用:

bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(Toolkit.Screen2World(x), Toolkit.Screen2World(y));

pShape = new PolygonShape();
pShape.setAsBox(Toolkit.Screen2World(width/2), Toolkit.Screen2World(height/2)); //In Toolkit I have my methods to scale the world

fixtureDef = new FixtureDef();
fixtureDef.shape = pShape;
//Set some more attributes i.e. density

//Add the PhysicsActor
PhysicsActor leftBar = new PhysicsActor(world, bodyDef, fixtureDef, new TextureRegion(Resources.barLeftTexture, 0, 0, width, height));
leftUpperBar.setSize(width, height);
this.addActor(leftUpperBar);

您只需手动设置宽度和高度(在我的情况下,我使用纹理的分辨率)。

Resources.barLeftTexture:

public static Texture barLeftTexture;

barLeftTexture = new Texture(Gdx.files.internal("data/barLeft.png"));

我注意到你的问题可能已经过时(OP),但也许这有助于有人绊倒这个问题。