代码之家  ›  专栏  ›  技术社区  ›  LoneCuriousWolf

带box2d的libgdx actor上的输入侦听器

  •  1
  • LoneCuriousWolf  · 技术社区  · 7 年前

    我想触摸一个演员并执行一个动作(如射击子弹),但我的演员没有反应 touchDown 是的。我两个都试过了 InputListener 以及 clickListener 是的。另外,我正在使用box2d位置来绘制我的演员。所以,我无法确定 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 回复  |  直到 7 年前
        1
  •  1
  •   Julien    6 年前

    你的演员位置和你的身体位置是两码事。

    • 演员是一种场景元素。你可以用setPosition方法更新它的位置。所有的演员都在舞台上
    • 物体是受重力等影响的box2d单元。

    当你想同时拥有场景2d和box2d的优点时,你可以像你所做的那样,给一个演员分配一个身体,但是如果你和身体交互(例如重力),你必须用身体位置更新演员的位置,或者如果你用演员的方法和一个演员交互(点击监听器)。

    你可以用表演的方法 this example 是的。在这种特殊情况下,它会根据受重力影响的身体位置更新演员的位置。 你可以在这里看到最终结果: https://libgdx.info/box2d-basic/

    所以在你的特殊情况下

    this.setposition(body.getposition().x-this.getwidth()/2,body.getposition().y-this.getheight()/2); 和 这个.setSize(actorWidth,actorHeigth)

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

    我希望这能帮上忙。