简体   繁体   English

Java Timer使我的libGdx游戏崩溃

[英]Java Timer crashes my libGdx game

I'm making match3 game on libGdx. 我正在libGdx上进行match3游戏。 After scanning for matches I have to wait for animations to complete and run scanning again until no more matches left. 扫描匹配项后,我必须等待动画完成并再次运行扫描,直到不再有匹配项为止。 I do it with Java Timer and when I run application on desktop it works fine but on Android device it crashes after one, two or few more iterations. 我使用Java Timer进行操作,当我在桌面上运行应用程序时,它可以正常运行,但在Android设备上,经过一,两次或几次迭代后崩溃。 Any ideas what is wrong? 任何想法有什么问题吗?

    Timer animationTimer;

    scanForMatches(){
        //do some stuff
        //...

        checkAnimationComplete();
    }

    checkAnimationComplete(){
        animationTimer = new Timer();

        animationTimer.scheduleAtFixedRate(new TimerTask(){
            @Override
            public void run() {
                boolean animDone = true;

                // do some stuff to
                // check if anim done

                if (animDone){
                    animationTimer.cancel();
                    scanForMatches();
                } 
            }
        }, 1000, 100);
    }

Without looking at the rest of your code, I would highly suggest dropping the timer altogether as it is almost certainly unnecessary in this case and not very efficient. 在不查看其余代码的情况下,我强烈建议完全放弃计时器,因为在这种情况下几乎肯定没有必要,而且效率也不高。 Are you using an Action to animate with a stage, or are you manually moving things based on position in draw()? 您是在使用动作为舞台制作动画,还是根据draw()中的位置手动移动事物? If you are just moving something in draw(), I would use a boolean flag to signal that it has reached it's destination, like if you are dropping down solved tiles or something. 如果您只是在draw()中移动某些内容,我将使用一个布尔标志来表明它已到达目的地,就像您放下已解决的图块或其他内容一样。 If you are using an Action, it is possible to use a new Action to act as a callback like the following... 如果您使用的是Action,则可以使用新的Action充当回调,如下所示:

     myGem.addAction(Actions.sequence(
                    Actions.moveTo(230f, 115f, 0.25f, Interpolation.linear),
                    new Action() {
                        public boolean act(float delta) {
                            System.out.println("Done moving myGem!");
                             scanForMatches();
                            return true;
                        }
                    }));

There are quite a few ways to do what you're looking for depending on how you have your grid set up. 有多种方法可以根据您的网格设置来完成所需的工作。 Post up how you are animating whatever it is so I can take a look. 发布您的动画效果,以便我看看。 Basically, you need to know exactly when it is done animating so that you can fire off your scan method again. 基本上,您需要确切地知道何时完成动画制作,以便您可以再次启动扫描方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM