【问题标题】:How to use timer with libgdx?如何在 libgdx 中使用计时器?
【发布时间】:2015-05-15 00:40:23
【问题描述】:

我正在尝试使用 gdx Timer 重复增加一个值,但是当我在 Label 中使用该值时,似乎什么也没发生,并且该值停留在 0 ,这是我的计时器代码:

@Override
    public void show() {
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        Skin skin = new Skin(Gdx.files.internal("gui/uiskin.json"));
        table = new Table(skin);
        table.setFillParent(true);

        atlas = new TextureAtlas("gui/atlas.pack");
        skin2 = new Skin(Gdx.files.internal("gui/MenuSkin.json"), atlas);


        DragAndDrop dragAndDrop = new DragAndDrop();



        // My tables 
        inventoryActor = new InventoryActor(new Inventory(16, 3), dragAndDrop, skin, "Game Pad");
        inventoryActor2 = new InventoryActor(new Inventory(25), dragAndDrop, skin, "Inventory Pad");

        // bc image

        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("et2.jpg"));

        Timer.schedule(new Task(){
            @Override
            public void run() {
                timing++;
            }
        }, 1);

        heading = new Label("Good Luck"+(timing++), skin2, "big");

        //Timer task


        //timerCount = new Label("Time : "+timing+" s",skin2, "small");


        back = new TextButton("Back", skin2, "small");
        back.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                ((Game) Gdx.app.getApplicationListener()).setScreen(new GameOption());
            }
        });
        back.pad(10);

        table.add(heading).colspan(3).expandX().spaceBottom(50).row();
        table.add(inventoryActor2).uniformX().expandY().top().left().padLeft(30);
        table.add(inventoryActor).top().spaceLeft(30);
        table.add(back).bottom().right();
        stage.addActor(table);



    }


@Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

        /*if (Gdx.input.isKeyPressed(Input.Keys.ANY_KEY)) {

        }*/

            batch.begin();
            batch.draw(texture, 10, 10);
            batch.end();

        inventoryActor.setVisible(true);
        inventoryActor2.setVisible(true);

        stage.act(delta);
        stage.draw();

    }

【问题讨论】:

标签: java libgdx


【解决方案1】:

问题是,您从未在 show() 方法中设置您的 Labels 文本。 您只需在构造函数中将初始文本作为String 给它,因此Label 不知道timing 已更改。 所以你应该在调用stage.draw()之前在render()中设置Labels文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2019-05-18
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多