AddChild problem

silviaDynamitesilviaDynamite USMember
edited May 2016 in CocosSharp

Hello. 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 screen shot that shows what happens.
image

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.

Best Answers

Answers

  • silviaDynamitesilviaDynamite USMember

    omg it makes a lot of sense now, thank you very much ! let me try it our right away

  • silviaDynamitesilviaDynamite USMember

    ok Um it worked in a way :tongue: now the zombies do go all the way the bottom of the screen and they work great with collision, the problem is that there's like a bunch of zombies at a time xD it's not only one it's just like an invasion :tongue:
    let me show you the code, I thought it was because of the random, that maybe it's adding a zombie in all the random positions, but I changed it for a constant value and it was the same thing.
    this is the code:

        public class Zombie : CCSprite {
            public Zombie(string file) : base(file){}
            public float zombieYVelocity = 0;
    
        }
    
        List<Zombie> Zombies = new List<Zombie>();
        private void CreateZombie()
        {
            var zombieSprite = new Zombie ("zombie"); 
            zombieSprite.PositionX = CCRandom.GetRandomFloat (10, 760);
            zombieSprite.PositionY = 1055; 
            AddChild (zombieSprite);
            Zombies.Add(zombieSprite);
        }
    
    
        /// logic of the game 
        void GameLogic (float frameTImeInSeconds)
        {
            // timer set up 
            time += frameTImeInSeconds;
            timer = (int)Math.Floor (time); 
            timeLabel.Text = "Time = " + timer; 
    
    
            // zombie actions // move // collision
            var zombieYVelocityIncrease = frameTImeInSeconds * -gravity; 
            foreach(var zombie in Zombies){
                zombie.zombieYVelocity += zombieYVelocityIncrease;
                zombie.PositionY += zombie.zombieYVelocity;
                zombie.zombieYVelocity = 0; 
    
                bool collision = pugSprite.BoundingBoxTransformedToParent.IntersectsRect (zombie.BoundingBoxTransformedToParent);
    
                // if colision 
                if (collision) {
                RemoveChild (zombie); 
                zombieYVelocity = 0; 
                score ++; 
                scoreLabel.Text = "Score = " + score;
                } // if collision 
    
    
            }// foreach
    
            // every 5 seconds new zombie 
            if (timer % 5 == 0) {
    
                CreateZombie ();
                zombieYVelocity = 0; 
    
            } }
    

    I'll attach a picture that shows the result xD its funny

  • silviaDynamitesilviaDynamite USMember

    thank you very much ! it never occurred me xD and it really works :)

Sign In or Register to comment.