带有Box2D的libgdx-Actor上的Input Listener



我想触摸一个演员并执行一个动作(如射击子弹(,但我的演员没有响应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);
}
}

你的演员姿势和你的身体姿势是两回事。

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

当你想同时享受Scene2D和Box2D的好处时,你可以像以前一样,将身体分配给演员,但如果你与身体互动(例如:重力(,你必须用身体位置更新演员位置,或者如果你使用演员方法(点击收听者(与演员互动,你必须更新身体位置

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

所以在你的特殊情况下,你错过了像这样的东西

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

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

我希望这能有所帮助。。

相关内容

最新更新