Adding enemies dinamically

Adding enemies dinamically
0.0 0


Hello everyody, its me agian with a newbie question.
I’m trying to add the enemies to my game, are not enemies per se but kind of. The idea is having them moving from a side of the creen to the other but i can’t get to have more than one. I’m using the following code:

            var enemy;
            var rand = Math.random();
            if(rand > 0.5){
                enemy = new Shape("square");
            else {
                enemy = new Shape("circle");

            enemy.setPosition(this.winSize.width + 10, this.winSize.height / 2);
            //this.enemy[i].runAction(cc.MoveBy.create(this.speed, cc.p(-( this.winSize.width + 60), 0)));
            enemy.runAction(cc.MoveBy.create(this.speed, cc.p(-200, 0)));

And from my init function on the AnimationLayer i have this:

        var i = 0;
        while(i < 5){

What is happening with this is that it only crete the first one, in fact it run the 5 times but i do not create the 5 enemies and i can’t find a way. I’ve tried using a enemiers vector but the same was happening.

Just to give more information, later i will have to make the main character impact to this “enemies” and based on the type different things are going to happen. (Yes i know, it don’t make much sense but it just to uil my first app).

I’m sure there is a better way than the one i’m using, can someone help me?

BTW, there is any chat/irc?

Thanks again!


Hi, @santi87

From your code, I haven’t seen things strange, so I can’t tell what’s wrong.

To check what really happened, I suggest you use Chrome developer tool to inspect on run time.



Are you sure you are not just creating all of them in the exact same position? Have you tried randomizing the positions?

Also, I might be wrong but, shouldn’t enemy.setPosition(this.winSize.width + 10, this.winSize.height / 2); be enemy.setPosition(cc.p(this.winSize.width + 10, this.winSize.height / 2));? Is your code actually setting the sprites in the position you want?


The code is setting the sprite in the correct position, but if you see i’m moving the sprite from the initial position with the line
enemy.runAction(cc.MoveBy.create(this.speed, cc.p(-200, 0)));
It seems to be that its creating all the sprites and moving them all at the same time and what i want to achive is to have my hero and make the different enemies move toward him.
Maybe there is other solution and instead of moving the enemies to the hero move the hero to the enemies, what you think?
From the chrome console i can see that the While run the 5 times without any problem but maybe i need to add something to make them nos start at the same time but one before the other?

— Update----
I tried adding a rand of the speed of movement and in fact all the enemies are beignc reated at the same time, any suggestions to make them appear one after the other?



For making your enemies run towards the player character, you could use something like:

var playerPos = player.getPosition();
enemy.runAction(cc.MoveTy.create(this.speed, playerPos));

But you should be ware that telling a sprite that’s already moving with “MoveTo” to “MoveTo” another position can have some strange effects. If you are just making a shoot em’ up then you are just fine by making the enemies go away of the screen (note: you should remember to delete the enemies once they are off the screen if they are not coming back, so they don’t start stacking up on memory).

Secondly, for making them appear: your runEnemies method creates one enemy and moves it, so I’m assuming you are calling it several times in some loop? You should just add a delay to that in any way that you see fit. One alternative could be to schedule the periodic execution of that function, for example: (I’m assuming runEnemies is defined in your “gameLayer” class) you could add a startCreatingEnemies and a stopCreatingEnemies method to your gameLayer class and call them when you want to start or finish the game, and they would look something like this:

    this.schedule(this.runEnemies, 1/2); //creates an enemy every half a second (0.5 = 500 milliseconds)

    this.unschedule(this.runEnemies); //stops the periodic calls to "runEnemies"

I hope that helps :slight_smile:


Cool! it worked! Thank you very much! Now i will work on the collision detec of each “enemy” jeje. For sure you will be on the credits of this game jaja…


Neh, it’s not that big of a deal :stuck_out_tongue:

But do post a link whenever you have something to show!

PS: ea, copado ver compatriotas por acá :smiley:


I will post it whenever i finish it…
P/D: 100% de acuerdo!