简体   繁体   English

Sprite spawn 加倍并导致强制关闭

[英]Sprite spawn is doubling and causing a force close

Right now I have sprites that spawn at different times.现在我有在不同时间产生的精灵。 What I'm trying to figure out is why after so much time passes it suddenly starts spawning the zombie2 in larger and larger amounts.我想弄清楚的是,为什么经过这么长时间之后,它突然开始越来越多地产生僵尸2。 Like it goes from a couple spawning to like 3 than 6 and more spawning at the exact same time.就像它从一对夫妇产卵到像 3 比 6 和更多同时产卵。 I thought my postDelayed would have handled that just fine and make them at a constant rate not incrementing.我认为我的 postDelayed 会处理得很好,并使它们以恒定的速率而不是递增。 I'm not sure whether this is what is causing it or not but it seems that around the time it spawns a bunch of the zombie2's it force closes.我不确定这是否是导致它的原因,但似乎在它产生一堆僵尸 2 的时候它强制关闭。 (Could this possibly be related to the fact that I have the app set for 1.5? Should it be set to something higher like I have seen some others do?) Any help would be appreciated. (这可能与我将应用程序设置为 1.5 的事实有关吗?是否应该像我看到其他人那样将其设置为更高的值?)任何帮助将不胜感激。 Here is some code that is running the spawns.这是一些运行生成的代码。

    @Override
    public void run() {
        long  elapsed;
        elapsed=System.currentTimeMillis()-startTime;


        if (elapsed> 5000)
        {
            normZombie.add(createSprite(R.drawable.zombie1));
            normZomb.postDelayed(this, 3000);
        }
        else if (elapsed >15000)
        {
            normZombie.add(createSprite(R.drawable.zombie1));
            normZomb.postDelayed(this, 1500);
        }
        else
        {
            normZombie.add(createSprite(R.drawable.zombie1));
            normZomb.postDelayed(this, 1000);
        }

        if (elapsed >= 10000)
        {
            fastZombie.add(createZombie2(R.drawable.zombie2));
            fastZomb.postDelayed(this, 10000);
        }
//      else if(elapsed >25000)
//      {
//          fastZombie.add(createZombie2(R.drawable.zombie2));
//          fastZomb.postDelayed(this, 5000);
//      }
//      else if(elapsed >40000)
//      {
//          fastZombie.add(createZombie2(R.drawable.zombie2));
//          fastZomb.postDelayed(this, 3000);
//      }


    }

The section that is commented out I took out because I thought it had a part in it.被注释掉的部分我去掉了,因为我认为它有一部分。 I do think it accelerates it though.我确实认为它会加速它。 The if - elses are all there to set it up so after a certain amount of time the spawn rate increases. if - else 都在那里进行设置,因此在一定时间后生成率会增加。

Thanks谢谢

EDIT: Ok so from further toying around with it I believe that some how the postDelayeds are incrementing off of eachother.编辑:好的,所以从进一步玩弄它,我相信一些 postDelayeds 是如何彼此递增的。 I simply had everything but just one of them commented out and it repeated as it should ever so many seconds and it did it correctly continuously.我只是拥有了所有东西,但只有其中一个被注释掉了,它重复了这么多秒,并且它连续正确地执行了它。 When I have one of each posted together (not including the if if-elses ) the begin to happen faster and faster on their own.当我将其中的一个贴在一起时(不包括 if if-elses ),它们自己开始越来越快地发生。 Any idea why?知道为什么吗?

So if anyone else has this problem... I figured out what it was.所以如果其他人有这个问题......我想出了它是什么。 I needed two separate runnable because some how they were incrementing off of eachother each time 1 was called.我需要两个单独的可运行文件,因为每次调用 1 时它们会彼此递增。 Here is what the code looks like now.这是代码现在的样子。

Runnable norm = new Runnable(){

            @Override
            public void run() {
                normZombie.add(createSprite(R.drawable.zombie1));
                normZomb.postDelayed(this, 3000);

            }

           };

                 Runnable fast = new Runnable(){

                    @Override
                    public void run() {

                        fastZombie.add(createZombie2(R.drawable.zombie2));
                        fastZomb.postDelayed(this, 10000);
                    }

                };
                norm.run();
                fast.run();

It works well like this.它像这样工作得很好。

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

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