【问题标题】:LibGDX: How to move a rectangle with a finger?LibGDX:如何用手指移动矩形?
【发布时间】:2015-02-07 15:45:05
【问题描述】:

我正在尝试使用 libgdx 实现用手指移动矩形,但是当我在手机上运行代码时它不起作用,矩形停留在左上角。任何想法如何解决这个问题?

public class Main extends ApplicationAdapter implements InputProcessor {
ShapeRenderer shape;
public int xpos;
    public int ypos;
@Override
public void create () {
    shape = new ShapeRenderer();
}

@Override
public void render () {
    Gdx.gl.glClearColor(0, 255, 255, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    shape.begin(ShapeType.Filled);
    shape.setColor(1, 1, 0, 1);
    shape.rect(xpos, ypos, 100, 100);
    shape.end();  
} 
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    xpos = Gdx.input.getX();
    ypos = Gdx.input.getY();
    return true;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    xpos = Gdx.input.getX();
    ypos = Gdx.input.getY();
    return true;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    xpos = Gdx.input.getX();
    ypos = Gdx.input.getY();
    return true;
} 

我还实现了InputProcessor 接口的所有其他方法,并为它们提供了return false 语句。但就像我说的那样,它不起作用......

【问题讨论】:

  • 别忘了给Gdx.input.setInputProcessor(this);打电话。
  • 给我们更多信息。你从 InputProcessor 实现的方法是否被调用了?
  • 我已经通过删除所有输入处理器的东西并将这个 if 语句添加到渲染方法来让它工作:if(Gdx.isTouched(){xpos = Gdx.input.getX()和 ypos 一样,然后你只需添加 ypos = height - ypos 或类似的东西我没有记住代码,但这对我有用,但感谢你的努力

标签: android events libgdx move drag


【解决方案1】:
if(Gdx.input.isTouched() &&rectangle.contains(Gdx.input.getX(),Gdx.graphics.getHeight() -Gdx.input.getY())) {
     rectangle.set(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY(), 200, 100);
} 
else {
     rectangle.set(rectangle.x, rectangle.y, 200, 100); 
}

【讨论】:

  • 欢迎来到 StackOverflow。请从您的代码中删除额外的缩进以使其更具可读性。最好的问候
猜你喜欢
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-26
相关资源
最近更新 更多