开发者

Sprite spawn is doubling and causing a force close

开发者 https://www.devze.com 2023-03-08 11:31 出处:网络
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. Like it go

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. Like it goes from a couple spawning to like 3 than 6 and more spawning at the exact same time. I thought my postDelayed would have handled that just fine and make them at a constant rate not incrementing. 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. (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. 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 a开发者_Go百科mount of time the spawn rate increases.

Thanks

EDIT: Ok so from further toying around with it I believe that some how the postDelayeds are incrementing off of eachother. 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. 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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消