Libgdx输入无法在Android

时间:2017-07-18 21:23:03

标签: java android libgdx

我是Libgdx的新手,我正在尝试制作一张可以使用Camera探索的地图。我在自己的GestureListener课程中实现了Map

public class Map extends Stage implements GestureListener {

    public String mapName;
    private Sprite background;
    public LocationPoint points[];
    private OrthographicCamera camera;
    private Batch batch;
    public Music anbientSound;
    public int numOfPoints;
    public int locationsX[];
    public int locationsY[];


    public Map(Sprite background) {
        this.background = background;
    }


    public Sprite getBackground() {
        return background;
    }


    public void activate() {

        InputMultiplexer inputChain = new InputMultiplexer();

        if(points==null) {
            points = new LocationPoint[numOfPoints];
            for(int i = 0; i < numOfPoints; i++) {
                points[i] = new LocationPoint(locationsX[i], locationsY[i]);
                addActor(points[i]);
            }
        }

        batch  = GameUtils.batch;
        camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0);
        camera.update();

        Music music = GameUtils.addMusic(anbientSound);
        music.setLooping(true);
        music.play();

        inputChain.addProcessor(this);
        inputChain.addProcessor(new GestureDetector(this));

        Gdx.input.setInputProcessor(inputChain);
    }


    public void draw() {

        Gdx.gl20.glClearColor(0, 0, 0, 1);
        Gdx.gl20.glClear(Gdx.gl20.GL_COLOR_BUFFER_BIT);

        Batch batch = this.batch;
        batch.setProjectionMatrix(camera.combined);

        batch.begin();
        background.draw(batch);
        batch.end();

        batch.begin();

        for(int i = 0; i < numOfPoints; i++) {
            points[i].draw(batch, 1);
            addActor(points[i]);
        }

        batch.end();
    }


    public void dispose() {
        GameUtils.stopMusic();
        background.getTexture().dispose();
        anbientSound.dispose();
    }


    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
    camera.translate(-deltaX, deltaY);

        float initialX = camera.viewportWidth / 2;
        float initialY = camera.viewportHeight / 2;

        GameUtils.limitBound(camera, initialX, initialY, background.getWidth(), background.getHeight());
    camera.update();

        return true;
    }


    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {
        return false;
    }


    @Override
    public boolean tap(float x, float y, int count, int button) {
        return false;
    }


    @Override
    public boolean longPress(float x, float y) {
        return false;
    }


    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
        return false;
    }


    @Override
    public boolean panStop(float x, float y, int pointer, int button) {
        return false;
    }


    @Override
    public boolean zoom(float initialDistance, float distance) {
        return false;
    }


    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, 
Vector2 pointer1, Vector2 pointer2) {
        return false;
    }


    @Override
    public void pinchStop() {}

}

方法activate()用于激活Map类的资源。类Map也有ImageButtons名为LocationPoints

public class LocationPoint extends ImageButton {

    private Monster monster;
    private Trap trap;
    public boolean occuped;
    public boolean isTrap;
    public int f = 20;
    public int k = 20;


    public LocationPoint(float x, float y) {
        super(GameUtils.getLocationDrawable());
        this.setSize(46, 46);
        setPosition(x, y);
        addListener(new InputListener(){
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                setPosition(f, k);
                f += 10;
                k += 10;
                return super.touchDown(event, x, y, pointer, button);
            }
        });
    }


    public void occup(Monster monster) {
        this.monster = monster;
        occuped = true;

        if(isTrap)
            captured();

    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
    }


    public void empty() {
        monster = null;
        occuped = false;
    }

    public void captured() {
        monster.capture();
    }


    public Monster getMonster() {
        return monster;
    }

}

在LocationPoint类中,我触摸了一个InputListener来制作一个东西。 当在Android上玩游戏时,LocationPoints事件和Map pan事件。但在我移动相机后,当我触摸LocationPoint时,它不会触发该点事件。

但是当我将相机返回到初始位置时,LocationPoints事件正常工作!你能帮助我吗? (抱歉我的英语不好......)

1 个答案:

答案 0 :(得分:1)

  • Actor已经是小孩,因此请从draw()方法移除冗余调用。

    for(int i = 0; i < numOfPoints; i++) {
      points[i].draw(batch, 1);
      addActor(points[i]);             // <-- Not Should be in draw() call
    }
    
  • 具有由默认构造函数创建的自己的SpriteBatch的阶段,使用那个或在Stage构造函数中传递自己的批处理。

    如果您想自己画一些东西,请使用getBatch() Stage方法。

  • Map类中创建相机也是多余的,使用返回舞台相机的getViewport().getCamera(),您可以将其转换为OrthographicCamera

  • super.dispose();方法

  • 中调用dispose()

自己画出所有演员?如果您没有超出范围执行某些操作,则无需覆盖draw() Stage方法。