我想在演员身上触摸并执行某些操作(比如射击子弹),但我的演员没有响应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);
}
}