Java:简单变量问题

时间:2017-01-02 11:21:58

标签: java variables if-statement libgdx

我的代码在我看来很清楚,我不明白它为什么不能工作。

有一个按钮,它应该更改你点击它时画出的精灵(这个步骤有效),当你点击它时它也应该为我的变量planetCount添加+1 ,这一步的目的是得到行星精灵:

right.addListener(new ClickListener() {
   public void clicked(InputEvent event, float x, float y) {
       sound.play(0.2f);

       if (planetCount <= 4) {
           choixRegion.setRegion(choix2Region);
           choix2Region.setRegion(choix3Region);
           choix3Region.setRegion(choix4Region);
           choix4Region.setRegion(choix5Region);
           choix5Region.setRegion(choixRegion);

        } else if(planetCount < 4){
            planetCount = planetCount + 1;
        } else if(planetCount > 4){
            planetCount = 4;
        }
    }
});

并且存在问题,因为它给出的所有行星选择都返回相同的屏幕(StartGame)来总结如果我有planetCount = 1,它应该返回我Itryon的屏幕,但不管planetCount值是多少它总是返回StartGame屏幕:

oui.addListener(new ClickListener() {
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        if(planetCount == 0){
            game.setScreen(new StartGame(game));
        } else if(planetCount == 1){
            game.setScreen(new Itryon(game));
        } else if (planetCount == 2){
            game.setScreen(new Nernel(game));
        } else if(planetCount == 3){
            game.setScreen(new Abela(game));
        } else if(planetCount == 4){
            game.setScreen(new StartGame(game));
        } 
        sound.play(0.2f);
        music.stop();
        return true;
    }

那我的错误在哪里?

2 个答案:

答案 0 :(得分:0)

        public void clicked(InputEvent event, float x, float y) {

            // ...

            if (planetCount <= 4) {
                // ...
                // when true, planetCount is not increased.
                // You can try setting planetCount = 1 at start,
                // And you'll get only Itryon's screen.
            }
            else if(planetCount < 4){
                planetCount = planetCount + 1;
            }
            else if(planetCount > 4){
                planetCount = 4;
            }

您可以简单地修改方法如下:

     public void clicked(InputEvent event, float x, float y) {

           sound.play(0.2f);

           choixRegion.setRegion(choix2Region);
           choix2Region.setRegion(choix3Region);
           choix3Region.setRegion(choix4Region);
           choix4Region.setRegion(choix5Region);
           choix5Region.setRegion(choixRegion);

           // You should think about below logic.

           planetCount++;

           if (planetCount > 4) {
               planetCount = 4;
           }
    }

答案 1 :(得分:0)

问题与第一个if else if结构的使用有关。

在您的代码中,您永远无法运行第一个else if语句,因此计数器永远不会递增。

你可以这样写:

right.addListener(new ClickListener() {
        public void clicked(InputEvent event, float x, float y) {

            sound.play(0.2f);

            if (planetCount < 4) {
                choixRegion.setRegion(choix2Region);
                choix2Region.setRegion(choix3Region);
                choix3Region.setRegion(choix4Region);
                choix4Region.setRegion(choix5Region);
                choix5Region.setRegion(choixRegion);
                planetCount = planetCount + 1;

            }
        }
    });