Libgdx sceene2d和输入处理器未更新屏幕

时间:2015-04-16 13:15:21

标签: java android libgdx

我对libgdx框架比较陌生,所以我希望我不要问任何愚蠢的事情,但我在toucheEvent上更新屏幕时遇到问题。似乎触发事件触发,但阶段未更新,因此屏幕始终相同。这是代码

MainClass

public class MainGame implements Screen {
public LabirintGame game;
public Stage stage;
public OrthographicCamera camera;
public ActorM rigth;
public ActorM wrong;

public MainGame(LabirintGame game) {
    this.game = game;
    this.camera = new OrthographicCamera();

}


@Override
public void show() {
    this.camera.setToOrtho(false, 800, 480);
    stage = new Stage(new ScreenViewport());
    stage.clear();
    Words group = new Words(stage);
    InputMultiplexer inputMultiplexer = new InputMultiplexer();

    inputMultiplexer.addProcessor(stage);
    inputMultiplexer.addProcessor(new MyInputProcessor(stage, camera));
    Gdx.input.setInputProcessor(inputMultiplexer);
    //Add wrong and rigth boxes
    rigth = new ActorM("box", 0, 0, 200,200);
    wrong = new ActorM("box",(game.width - 230), 0, 200, 200);
    wrong.moveBy(200,200);

    Button createButtons = new Button();
    createButtons.setStyle("atlas-besede/besede.atlas", "buttonOff", "buttonOn");
    TextButton ValidationButton = createButtons.createButton("Validate", (game.width/2), 0, 150, 150);
    ValidationButton.addListener(new InputListener() {
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            game.setScreen(new Labirint(game));
            return true;
        }

    });


    stage.addActor(ValidationButton);
    stage.addActor(rigth);
    stage.addActor(wrong);


    List<String> backgrounds = Arrays.asList("s", "z");
    for (int i = 0; i < backgrounds.size(); i++) {
        Word  actor = new Word(backgrounds.get(i),(i + 1) * 300, 300, 100, 100);
        actor.setPosition((i + 1) * 300, 300);
        actor.setName(backgrounds.get(i));
        group.addActor(actor);
    }
    stage.addActor(group);
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(Gdx.graphics.getDeltaTime());

    stage.draw();
    game.batch.begin();
    game.batch.setProjectionMatrix(camera.combined);
    game.batch.end();

}

ActorM

package com.mygdx.game;

public class ActorM extends Actor {
public SpriteBatch batch;
public TextureAtlas atlas;
public TextureAtlas.AtlasRegion region;
Sprite sprite;
public int x;
public int y;
public int width;
public int height;

public ActorM(String actorName, int x, int y, int width, int height) {
    //this.region = region;
    super();
    batch = new SpriteBatch();
    atlas = new TextureAtlas(Gdx.files.internal("atlas-start/atlas-start.atlas"));
    sprite = atlas.createSprite(actorName);
    this.width = width;
    this.height = height;
    this.x = x;
    this.y = y;
    this.setBounds(0, 0, sprite.getWidth(), sprite.getHeight());
    setTouchable(Touchable.disabled);
    setName(actorName);
    setPosition(x,y);
}

@Override
public void draw (Batch batch, float parentAlpha) {
    batch.draw(sprite, x,y, width, height);
}
public void move(int posX){
    this.x = this.x +  posX;
}

}

MyInputProcessor

public class MyInputProcessor implements InputProcessor {
private OrthographicCamera camera;
private Stage stage;
private Vector2 coordinates;
private Music sound;
public MyInputProcessor( Stage stage, OrthographicCamera camera) {
    this.stage = stage;
    this.camera = camera;
}

@Override
public boolean keyDown(int keycode) {
    return false;
}

@Override
public boolean keyUp(int keycode) {
    return false;
}

@Override
public boolean keyTyped(char character) {
    return false;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)        

    {
        //Gdx.app.log("", "x " + screenX + " y " + screen`enter code here`Y + " pointer " + pointer);
        Vector2 coordinates = stage.screenToStageCoordinates(new Vector2((float)screenX,(float)screenY));
        Actor  hitactor = stage.hit(coordinates.x, coordinates.y, true);
        Gdx.app.log("", coordinates.toString());
        if (hitactor != null){
            //Gdx.app.log("", "HIT" + hitactor.getName());
            Gdx.app.log("", "HIT" + hitactor.getRotation());
            hitactor.setRotation(hitactor.getRotation() + 1f);
            hitactor.setPosition(5,5);
            Gdx.gl.glClearColor(1, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        }
        return true;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        coordinates = stage.screenToStageCoordinates(new Vector2((float)screenX,(float)screenY));
        Actor hitactor = stage.hit(coordinates.x, coordinates.y, true);

        if (hitactor != null){
            Gdx.app.log("", "Drag");
            hitactor.setRotation(hitactor.getRotation() + 1f);
        }
        camera.update();
        return true;

    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

你的“ValidationButton”使用一个总是返回true的InputProcessor,它是舞台上的第一个演员,所以舞台上没有其他任何东西都有机会回应触地事件。此外,由于您的舞台是InputMultiplexer中的第一个输入处理器,因此您的其他输入处理器永远不会有机会响应触地事件。

您应该在按钮上使用EventListener而不是InputListener,因此将为您处理大部分逻辑。

顺便说一下,你的ActorM类正在产生一个它从未使用过的SpriteBatch。 SpriteBatch占用大量内存,并且游戏中不需要有多个内存。舞台已经有一个SpriteBatch的引用,它被传递到你的Actor的draw方法中,因此Actor不需要创建甚至引用SpriteBatch。

此外,您的ActorM类正在为自身加载TextureAtlas的完整副本,因此会为ActorM的每个实例加载重复的Textures,并且您将失去TextureAtlas的所有好处,因为您不会将它用于精灵批处理。您只需要加载TextureAtlas一次,并将它的引用传递给ActorM类的构造函数,这样它们就可以共享相同的纹理。