单击第一个屏幕中的下一个按钮,但在第二个屏幕中检测到

时间:2014-09-07 09:01:13

标签: android libgdx

我有问题。我用2 menujema和playfield做了一个游戏。当我按下我的感官旁边的第一个菜单按钮时,我按下其他菜单旁边的按钮(按钮具有相同的坐标)。现在我想知道这是怎么回事?

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class AddAtributesPerson implements Screen {
    OrthographicCamera guiCam;
    SpriteBatch batcher;
    Rectangle firstPersonCharacter;
    Rectangle secondPersonCharacter;
    Rectangle thirdPersonCharacter;
    Rectangle forthPersonCharacter;
    Rectangle nextBounds;
    Rectangle backBounds;
    Vector3 touchPoint;
    boolean firstPersonCharacterBoolean, secondPersonCharacterBoolean,thirdPersonCharacterBoolean,
    forthPersonCharacterBoolean;
    Game game;


    public AddAtributesPerson(Game game) {
        this.game = game;
    //  Gdx.app.getApplicationListener().setScreen(new Screen());
         Gdx.input.setInputProcessor(null);
    //  Gdx.input.setInputProcessor(new Stage());
        guiCam = new OrthographicCamera(320, 480);
        guiCam.position.set(320 / 2, 480 / 2, 0);
        batcher = new SpriteBatch();


    }


    public void update() {
        if (Gdx.input.isTouched()) {

        guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));

        if (firstPersonCharacter.contains(touchPoint.x, touchPoint.y)) {

                Assets.playSound(Assets.clickSound);

                firstPersonCharacterBoolean=true;
                return;
            }
        if (secondPersonCharacter.contains(touchPoint.x, touchPoint.y)) {
                Assets.playSound(Assets.clickSound);
                secondPersonCharacterBoolean=true;

                return;
            }
        if (thirdPersonCharacter.contains(touchPoint.x, touchPoint.y)) {
                Assets.playSound(Assets.clickSound);
                thirdPersonCharacterBoolean=true;

                return;
            }
        if (forthPersonCharacter.contains(touchPoint.x, touchPoint.y)) {
                Assets.playSound(Assets.clickSound);
                forthPersonCharacterBoolean=true;
                return;
            }


        if (nextBounds.contains(touchPoint.x, touchPoint.y)) {
            Assets.playSound(Assets.clickSound);
            game.setScreen(new GameScreen(game));
            return;
        }

        if (backBounds.contains(touchPoint.x, touchPoint.y)) {
            Assets.playSound(Assets.clickSound);
            game.setScreen(new ChoosePerson(game));
            return;
        }           


    }
    }


    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
    }

    @Override
    public void render(float delta) {
        update();
        draw();
        // TODO Auto-generated method stub

    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void show() {
        // TODO Auto-generated method stub

    }

    @Override
    public void hide() {
        // TODO Auto-generated method stub

    }


    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }   
}

1 个答案:

答案 0 :(得分:0)

因为您不断触摸屏幕。 你可以通过在第二个屏幕上添加一个布尔变量来解决这个问题。

private boolean hasBeenTouchedUp=false;

当你修饰时,将该值设为真。

并查看类似

的内容
  if (hasBeenTouchedUp && Gdx.input.isTouched()) { ... }

要检查修改时间,您需要使用输入过程。 http://www.gamefromscratch.com/post/2013/10/24/LibGDX-Tutorial-5-Handling-Input-Touch-and-gestures.aspx

更好的解决方案是使用justTouched而不是isTouched,因为justTouched仅在您第一次触摸屏幕的帧中为真。 但这取决于你的游戏:)

其他解决方案是检查新屏幕开启后是否经过了一段时间,也许是1秒后检查输入,但我不认为这是最好的想法。