0

I'm trying to make a simple game where there's a zombie horde coming from the top of the screen to the bottom. I want that a new zombie appears every 5 seconds, and it does, but every time a new zombie appears the previous one stops moving and the collision doesn't work on that one. Can someone help me understand this behaviour and what's the best way to make it work the way its supposed to? :) Here is my where I create the zombie:

private void CreateZombie()
{
    zombieSprite = new CCSprite ("zombie"); 
    zombieSprite.PositionX = CCRandom.GetRandomFloat (10, 600);
    zombieSprite.PositionY = 1055; 
    AddChild (zombieSprite);
}

and here's the code inside my gamelogic method:

void GameLogic (float frameTImeInSeconds) {
        zombieYVelocity += frameTImeInSeconds * -gravity; 
        zombieSprite.PositionY += zombieYVelocity * frameTImeInSeconds; 

        if (timer % 5 == 0) {

            CreateZombie ();
            zombieYVelocity = 0; 

        }   

    }

I attached a screenshot that shows whats happening Every 5 seconds when a new one is added the previous one stops, and the collision detection is no longer working with the ones that are stoped.

  • The problem is not the AddChild, the problem is that you have only one reference to a zombie (zombieSprite), so the new zombie replaces the old one. Store a list of zombies and update/check all of them. – Gusman May 12 '16 at 21:32
  • Oh ok it makes sense now. So the way I see it is that i should have like an array of zombies? and if I do that, i can , for example, make like zombie1, zombie2, zombie3, up to like 8 and them call them again from 1 to 8? and loop that? – silviaDynamite May 13 '16 at 00:08
  • In your case I would use a List as it will allow you to add and remove elements without resizing/sorting it, an array is better suited for things that you know it's fixed size, when it can grow and shrink then List<> is the way. – Gusman May 13 '16 at 00:10
  • oh nice thank you ! :) I'll try it that way. never use list before but i think i found an example. thank you very much ! – silviaDynamite May 13 '16 at 00:38

0 Answers0