简体   繁体   中英

Spawn sprite at different intervals

Forget the previous one if you seen it.

public void createNormZomb(){ 

                normZombie.add(createSprite(R.drawable.zombie1));
                normZomb.postDelayed(this, 1000);
            }

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

}

So basically I have it doing what I want. Every 1 second it spawns another zombie. The only flaw is when it runs for like 30secs or something like that it force closes. Up until that time goes past it is perfectly fine. (FYI it is implementing Runnable at the top) If anyone has any ideas as to why it force closes after so much time goes by I would really appreciate it.

(As a side note if anyone knows what I should do about this cause I'm used to using xml. How do I display the score and a timer on the screen in code since I'm not using xml. And what be a good way to write a timer that counts up in seconds.)

Thanks all:)

I think the problem is setting time = 5 . If you do x % 5 , you can not get 5 as result. If I try to divive 23 by 5, the answer is 4 and the remainder is 3, 23 = 4*5 + 3 . Therefor, 23 % 5 = 3 . Hopefully this fixes your problem (and I changed a few things..):

protected void onDraw(Canvas canvas) {
  canvas.drawColor(Color.BLACK);
  for (int i = 0; i < temps.size(); i++) {
  //or was there a reason to do the loop backwards...?
    temps.get(i).onDraw(canvas);
  }

  for (Sprite sprite : normZombie) {//for each normZombie do:
    //for(int i=0; i<normZombie.size();i++) no need to loop again, right?

    long startTime=System.currentTimeMillis();                      
    long elapsed=(System.currentTimeMillis() - startTime) / 1000;
    int time = 3;//,0,1,2 or 4
    if(elapsed % 5 == time)
      sprite.onDraw(canvas);
    }
  }

private void createSpritesNorm(){         
  for (int i = 0; i < 12; i++) {
    normZombie.add(createSprite(R.drawable.zombie1));//do this 12 times
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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