java - 使用 Box2D 在 libgdx Actor 上输入监听器

标签 java libgdx box2d scene2d

我想触摸一个 Actor 并执行一个 Action (例如射击子弹),但我的 Actor 没有响应 touchDown。我尝试了 InputListenerclickListener。另外,我使用 Box2D 位置来绘制我的 Actor 。因此,我无法确定 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);
        }
    }

最佳答案

你的 Actor 位置和你的 body 位置是两个不同的东西。

  • Actor 是一个 Scene2D 元素。您可以使用 setPosition 方法更新其位置。所有 Actor 都在舞台上
  • 主体是一个 Box2D 元素,可以受重力等影响。

当您想要同时获得 Scene2D 和 Box2D 的优势时,您可以像您所做的那样将 body 分配给 Actor ,但是如果您与 body 交互(例如, body 交互),则必须使用 body 位置更新 Actor 位置。 :gravity) 或如果您使用 actor 方法与 actor 交互(点击监听器),则更新 body 位置

您可以在 act 方法中执行此操作,如 this example 中所示。在这种特殊情况下,它根据受重力影响的 body 位置来更新 Actor 位置。 您可以在这里看到最终结果:https://libgdx.info/box2d-basic/

因此,在您的特定情况下,您缺少类似的内容

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

将 Actor 位置设置为 body 位置。 目前你的 actor 可能在位置 0,0 处初始化并且大小为 0

我希望这会有所帮助..

关于java - 使用 Box2D 在 libgdx Actor 上输入监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51408661/

相关文章:

java - Box2d 设置变换

java - 需要帮助来杀死窗口进程,使用 selenium-webdriver

java - JSONParser 类型没有可用的源代码

java - LibGDX - 使用 InputProcessor 进行多点触控处理

java - libGDX - while 循环是否会导致挂起

python - 重置后世界崩溃了

c++ - 仅在我的 2d 引擎中集成 Box2D 碰撞检测?

java - Java/Swing 中的高级 RTFEditorKit

java - 如何执行 Carrot2 文档聚类服务器

java - jlink命令行参数--strip-debug是什么意思?