使用Box2D的libgdx Actor上的输入监听器

3

我想在演员身上触摸并执行某些操作(比如射击子弹),但我的演员没有响应touchDown。我尝试了InputListenerclickListener。此外,我使用Box2D位置来绘制我的演员。因此,我无法确定setBounds中的正确值。请查看以下代码,告诉我是否有任何错误以及如何设置setBounds中的值:

public class Jet extends Actor{

        World world;
        Body planeBody;
        MyGdxGame game;
        Texture plane;
        Animation<TextureRegion> jet;
        float planeWidth, planeHeight, stateTime = 0f, PIXELS_TO_METRES = 100;

        public Jet(World world, MyGdxGame game) {
            this.world = world;
            this.game = game;

            TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("plane.atlas"));
            jet = new Animation<TextureRegion>(0.01f, textureAtlas.findRegions("Fly"), Animation.PlayMode.LOOP);

            planeWidth = 100f;
            planeHeight = 80f;

            BodyDef bodyDef = new BodyDef();
            bodyDef.type = BodyDef.BodyType.DynamicBody;
            bodyDef.position.set((game.V_WIDTH/8) / PIXELS_TO_METRES, (game.V_HEIGHT/2) / PIXELS_TO_METRES);

            PolygonShape polygonShape = new PolygonShape();
            polygonShape.setAsBox((planeWidth/2)/PIXELS_TO_METRES , (planeHeight/2)/PIXELS_TO_METRES);

            FixtureDef fixtureDef = new FixtureDef();
            fixtureDef.shape = polygonShape;
            fixtureDef.density = 1f;

            planeBody = world.createBody(bodyDef);
            planeBody.createFixture(fixtureDef);
            planeBody.setGravityScale(0f);
     // I don't know what to set here, should it box.getPosition()?
    //        this.setBounds(game.V_WIDTH/8, game.V_HEIGHT/2, planeWidth, planeHeight);

            this.addListener(new ClickListener(){
                @Override
                public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                    //This isn't logged
                    Gdx.app.log("Inside touchDown", "just touched down");
                    return true;
                }
            });

        }

        @Override
        public void draw(Batch batch, float parentAlpha) {
            stateTime += Gdx.graphics.getDeltaTime();

            TextureRegion currentFrame = jet.getKeyFrame(stateTime);
            batch.draw(currentFrame, planeBody.getPosition().x * PIXELS_TO_METRES - planeWidth/2,
                    planeBody.getPosition().y * PIXELS_TO_METRES - planeHeight/2, planeWidth/2,
                    planeHeight/2, planeWidth, planeHeight, 1, 1,
                    planeBody.getAngle() * MathUtils.radiansToDegrees);
        }

    }

舞台渲染类:

    public class GameScreen implements Screen {

        MyGdxGame game;
        Stage stage;
        World world;

        public GameScreen(MyGdxGame game) {
            this.game = game;
            camera = new OrthographicCamera();
            stage = new Stage(new FitViewport(game.V_WIDTH, game.V_HEIGHT, camera));

            Jet jet = new Jet(world, game);
            stage.addActor(jet);
            Gdx.input.setInputProcessor(stage);
        }

        @Override
        public void render (float delta) {
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

            game.batch.setProjectionMatrix(camera.combined);

            game.batch.begin();
            stage.act();
            stage.draw();
            game.batch.end();

            world.step(1/60f, 6, 2);
        }
    }
1个回答

3
您的演员位置和身体位置是两个不同的概念。
  • 演员是Scene2D元素。您可以使用setPosition方法更新其位置。所有演员都在舞台上。
  • 身体是Box2D元素,可以受到重力等影响。

如果您想同时享受Scene2D和Box2D的优势,可以将身体分配给演员,但这时必须根据需要更新演员位置与身体位置之间的关系(例如:重力)。如果使用演员方法(单击监听器)与演员交互,则必须更新身体位置。

您可以在act方法中执行此操作,如此示例所示。在这种特殊情况下,它基于受重力影响的身体位置更新演员位置。您可以在此处查看最终结果:https://libgdx.info/box2d-basic/

因此,在您的特定情况下,您需要添加以下代码:

this.setPosition(body.getPosition().x-this.getWidth()/2,body.getPosition().y-this.getHeight()/2); and this.setSize(actorWidth,actorHeigth)

以将演员位置设置为身体位置。当前,您的演员可能在位置0,0处初始化,并且大小为0。

希望这可以帮助到您。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接