我想触摸演员以执行某项动作(例如射击子弹),但是我的演员没有对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);
}
}
舞台渲染类:
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);
}
}
答案 0 :(得分:1)
您的演员位置和您的身体位置是两个不同的东西。
当您想同时获得Scene2D和Box2D的好处时,可以像您一样为主体分配一个主体,但是如果您与主体进行交互,则必须使用主体位置更新主体位置(例如, :重力),或者如果您使用actor方法(单击侦听器)与actor交互,则更新身体位置
您可以使用this example中的act方法执行此操作。在这种特殊情况下,它会根据受重力影响的身体位置来更新演员位置。 您可以在此处查看最终结果:https://libgdx.info/box2d-basic/
所以在您的特定情况下,您会缺少类似的东西
this.setPosition(body.getPosition()。x-this.getWidth()/ 2,body.getPosition()。y-this.getHeight()/ 2); 和 this.setSize(actorWidth,actorHeigth)
将演员位置设置为身体位置。 当前,您的actor可能已初始化为0,0且大小为0
我希望这会有所帮助。