我目前正在阅读有关Android开发和libgdx / scene2d atm的一些教程,但却被困在了touchevents:
对于初学者我正在实施棋盘游戏。到目前为止,我设法绘制了板和一些meeples(玩家令牌),并将其位置调整到正确的坐标。当我开始使用libgdx时,我也成功实现了第一个测试,看看touchinput是否正常工作。测试看起来像这样:
if(Gdx.input.isTouched()){
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
stone.x=touchPos.x - stone.width/2;
stone.y=touchPos.y - stone.height/2;
到目前为止,由于一些非常方便的功能,我重新安排了我的代码以使用scene2d。然而,尽管我遵循了一些相当简单的教程,但似乎我无法设法使用scene2d。由于其中一些引用了最近对scene2d的一些重大更改,我想知道这些教程是否可能已过时并希望你们可以帮助我解决我的问题:)我会尝试只将我的代码的相关部分粘贴到这里以呈现最小化我的非工作代码的示例。 当然,我也很高兴有关如何“改进”我的代码的任何提示,因为我还在学习,可能在这里打破一些约定^^
让我们从我的演员级开始:
//deleted: imports
public class Player extends Actor{
private int xval; //x Position of the Player on the board
private int yval; //y Position of the Player on the board
private Color color;
private TextureRegion playerTexture;
//deleted: some variables that are unimportant for touchevents
public Player(int x, int y, TextureRegion texture){
//initializing some Variables with default values
this.xval = x;
this.yval = y;
color = new Color(1,1,1,1);
playerTexture = texture;
this.setTouchable(Touchable.enabled); //this should be set by default, but i added it just to be sure.
//setBounds seems to be necessary in addition to the "touchable" flag to be able to "grab" or "touch" Actors.
//getX and getY are methods of the actor class which i do not overwrite. I use distinct getters and setters to modify the Variables xval and yval.
setBounds(getX(), getY(), playerTexture.getRegionWidth(), playerTexture.getRegionHeight());
//now i implement the InputListener. This doesnt seem to get called, since the "touch started" message doesn't appear in LogCat.
//In Theory the testevent should move the meeple one space to the right.
//To do that i call event.getTarget() which returns the actor of which the TouchDown is called. I then cast it to my Actor class (Player) and set xval to xval+1.
this.addListener(new InputListener(){
public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){
Gdx.app.log("Example", "touch started at (" + x + ", " + y + ")");
((Player)event.getTarget()).setXf(((Player)event.getTarget()).getXf()+1);
return true;
}
});
}
//This is my draw method, which just sets the color of my meeple and then draws it to its current position.
public void draw(Batch batch, float alpha){
batch.setColor(color);
batch.draw(playerTexture, getX(), getY());
}
//deleted: several getters and setters
}
因此,如果我做对了,gdx inputProcessor正在管理所有输入。如果我将Gdx InputProcessor设置为包含actor的阶段,它将在事件(例如touchDown)的情况下调用舞台上所有actor的inputProcessors。因此,我刚刚在我的类中添加了一个包含touchDown的类,因此在实际触摸actor的情况下应该处理touchDown事件。用于验证的hitbox是由我的Player类中的语句“setBounds”设置的。
我在ApplicationListener类中实现了它:
//deleted: imports
public class QuoridorApplicationListener implements ApplicationListener{
Stage stage;
OrthographicCamera camera;
//deleted: several variable declarations.
//deleted: constructor - this only fills some of my variables with values depending on game settings.
@Override
public void create() {
//set stage - since "stage = new Stage(800,400,false)" doesn't seem to work anymore, i did set up a viewport manually. If you got suggestions how to this straightforward, please leave a note :)
Gdx.app.log("Tag", "game started");
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
Viewport viewport = new Viewport() {};
viewport.setCamera(camera);
stage = new Stage(viewport);
Gdx.input.setInputProcessor(stage); //like i described above, this should forward incoming touchevents to the actors on the stage.
//deleted: setting textures, music, setting up the board (the boardtiles are actors which are added to the stage)
//deleted: setting TextureRegion, player colors, player positions and player attributes.
for (int i = 0; i < playercount; i++){
stage.addActor(players[i]);
}
}
//deleted: dispose(), pause(), resume(), resize() methods.
@Override
public void render() {
camera.update();
for (int i=0; i< playercount; i++){
players[i].setX(/*Formula to determine x-coordinates from Player.xval*/);
players[i].setY(/*Formula to determine x-coordinates from Player.yval*/);
}
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
}
我无法弄清楚我缺少什么来让touchevents工作。所以我希望你能帮助我:)提前多多感谢!
答案 0 :(得分:1)
我认为您的问题源于手动创建一个Viewport对象,该对象在内部处理unprojecting。 而不是
stage = new Stage(800,400,false);
你应该使用
stage = new Stage();
要在调整大小事件发生后将视口大小调整为正确大小,您需要调用
stage.getViewport().update( newWidth, newHeight )
在ApplicationListener的resize方法中。