Skip to main content
Known Participant
March 10, 2016
Question

Hello! I have 2 bullets and 1 enemy.Each bullet do different damage to the enemy.My code is working like that but when i have on stage multiple enemies the game is getting low.I make something wrong in my code or i draw heavy enemies?

  • March 10, 2016
  • 3 replies
  • 686 views

var enemy1Array:Array = new Array();

var shotA:Array=[];

var shotB:Array=[];

var damage:int = 4;

var damage1:int = 2;

for (var f1:int = numChildren - 1; f1 >= 0; f1--)

  {

         var child1:DisplayObject = getChildAt(f1);

         if (child1.name.indexOf("enemy")>-1)

     {

       enemy1Array.push(MovieClip(child1));

       MovieClip(child1).hitPoints=4;

    MovieClip(child1).gotoAndStop(+MovieClip(child1).hitPoints);

     }

  }

stage.addEventListener(Event.ENTER_FRAME,gameloop);

function gameloop(event:Event):void

{

for(var i:int=shotA.length-1;i>=0;i--) 

  {

      shotA.x +=20;

      for(var q:int=enemy1Array.length-1;q>=0;q--)

      {

            if (enemy1Array.hitTestPoint(shotA.x , shotA.y-30))

              {

                     enemy1Array.hitPoints-=damage;

                      if (enemy1Array.hitPoints <= 0)

                          {

                              enemy1Array.parent.removeChild(enemy1Array);

                              enemy1Array.splice(q,1);

                          }

               }

     }

  }

for(var b:int=shotB.length-1;b>=0;b--) 

  {

      shotB.x +=20;

      for(var qb:int=enemy1Array.length-1;qb>=0;qb--)

      {

            if (enemy1Array[qb].hitTestPoint(shotB.x , shotB.y-30))

              {

                     enemy1Array[qb].hitPoints-=damage1;

                      if (enemy1Array[qb].hitPoints <= 0)

                          {

                              enemy1Array[qb].parent.removeChild(enemy1Array[qb]);

                              enemy1Array.splice(qb,1);

                          }

               }

     }

  }

}

This topic has been closed for replies.

3 replies

Inspiring
March 10, 2016

How many enemies do you have on screen? How big are they?

Your code could be optimized (put enemy1Array into a local variable instead of calling it every time), but I really doubt it's what makes your game run low...

Known Participant
March 10, 2016

The enemy its 10KB with the animation...Can i have an example to optimized my code?Because i try to put the enemy1Array into a local var but i cant make it!

Inspiring
March 10, 2016

for(var b:int=shotB.length-1;b>=0;b--)

  {

      shotB.x +=20;

      for(var qb:int=enemy1Array.length-1;qb>=0;qb--)

      {

              var enemy:MovieClip = enemy1Array[qb];
              if (enemy.hitTestPoint(shotB.x , shotB.y-30))

              {

                     enemy.hitPoints-=damage1;

                      if (enemy.hitPoints <= 0)

                          {

                              enemy.parent.removeChild(enemy);

                              enemy1Array.splice(qb,1);

                          }

               }

     }

  }

}

It's as simple as that...

The memory used by your enemies are irrelevant to how much CPU they take.

I can ruin your CPU with a 500B enemy by making it a giant vector monster that will take half of the screen, with tons of gradients and alpha, and then apply 10 filters to it, and animate it with tons of rotations, and scaling...

Why?
- Alpha eats up CPU.
- Gradients eat up CPU.
- Filters eat up CPU, and force Flash to create a bitmap cache of you movie clip, which will eat up memory.
- Rotations and scaling force Flash to redraw the bitmap cache at each frame, which will eat up even more CPU.

- Making it huge will decimate your CPU, as the area to redraw at each frame gets bigger.

You have to understand that Flash really sucks at using your computers resources effectively.

Multi-threading is a pain to implement, and unless you use Stage3D, your GPU will not be involved either.

So basically, your whole Flash application is handled by a single core of your CPU.

So tell me more about your enemies... Do they have alpha? Gradients? Filters? Are they big? Do you use scaling and rotations in your animations?

Known Participant
March 10, 2016

Yes my enemies have animation but not a big deal,only the legs and the arms for walking.And my player have animations for walking jumping and shooting.So what can i do for that? My code its ok?

Ned Murphy
Legend
March 10, 2016

Part of the problem could be the amount of processing for individual enemies.  If each one has some animations going on, that requires a share of the processing.