使用 Box2D 在 libgdx Actor 上输入监听器
Input Listener on libgdx Actor with Box2D
我想触摸一个 actor 并执行一个动作(例如射击子弹),但我的 Actor 没有响应 touchDown
。 InputListener
和 clickListener
我都试过了。此外,我正在使用 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);
}
}
舞台渲染 class :
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);
}
}
你的演员位置和你的 body 位置是两个不同的东西。
- 演员是一个 Scene2D 元素。您可以使用 setPosition method.All actors are on the stage
更新其位置
- A body 是一个 Box2D 元素,可以受重力等影响
当您想同时获得 Scene2D 和 Box2D 的好处时,您可以像您一样将 body 分配给演员,但是您必须使用 [=39= 更新演员位置] 位置,如果您与 body 交互(例如:重力)或更新 body 位置,如果您使用演员方法与演员互动(点击监听器)
您可以像 this example 中那样在 act 方法中执行此操作。在这种特殊情况下,它会根据受重力影响的 body 位置更新演员位置。
您可以在此处查看最终结果:https://libgdx.info/box2d-basic/
所以在你的特定情况下你遗漏了像
这样的东西
this.setPosition(body.getPosition().x-this.getWidth()/2,body.getPosition().y-this.getHeight()/2 );
和
this.setSize(actorWidth,actorHeigth)
将演员位置设置为body位置。
当前,您的 actor 可能已在位置 0,0 和大小 0
处初始化
希望对您有所帮助..
我想触摸一个 actor 并执行一个动作(例如射击子弹),但我的 Actor 没有响应 touchDown
。 InputListener
和 clickListener
我都试过了。此外,我正在使用 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);
}
}
舞台渲染 class :
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);
}
}
你的演员位置和你的 body 位置是两个不同的东西。
- 演员是一个 Scene2D 元素。您可以使用 setPosition method.All actors are on the stage 更新其位置
- A body 是一个 Box2D 元素,可以受重力等影响
当您想同时获得 Scene2D 和 Box2D 的好处时,您可以像您一样将 body 分配给演员,但是您必须使用 [=39= 更新演员位置] 位置,如果您与 body 交互(例如:重力)或更新 body 位置,如果您使用演员方法与演员互动(点击监听器)
您可以像 this example 中那样在 act 方法中执行此操作。在这种特殊情况下,它会根据受重力影响的 body 位置更新演员位置。 您可以在此处查看最终结果:https://libgdx.info/box2d-basic/
所以在你的特定情况下你遗漏了像
这样的东西this.setPosition(body.getPosition().x-this.getWidth()/2,body.getPosition().y-this.getHeight()/2 ); 和 this.setSize(actorWidth,actorHeigth)
将演员位置设置为body位置。 当前,您的 actor 可能已在位置 0,0 和大小 0
处初始化希望对您有所帮助..