Skip to main content
Known Participant
December 22, 2013
Question

How to create a virtual pool of movie clips that will be used in the future?

  • December 22, 2013
  • 1 reply
  • 516 views

I am creating a game where enemies are spawned from the library onto the stage. Instead of spawning new enemies, I want to be able to have 20 enemies already made in a "virtual" pool. These enemies will get spawned randomly, and when the player clicks on the enemies, they die and get recycled instead.  This is my loop code to create enemies:

function makeEnemies():void

{

          var chance:Number = Math.floor(Math.random() * 150);

          if (chance <= level && enemies.length < 4)

          {

                    tempEnemy = new Enemy();

                    tempEnemy.x = Math.round(Math.random() * 480);

                    addChild(tempEnemy);

                    tempEnemy.scaleX = 1.5;

                    tempEnemy.scaleY = 1.5;

                    tempEnemy.cacheAsBitmapMatrix=identityMatrix;

                    enemies.push(tempEnemy);

                    tempEnemy.speed = enemyBaseSpeed + ((level - 1) * speedLevelInc);

                    if (tempEnemy.speed > MAX_SPEED)

                    {

                              tempEnemy.speed = MAX_SPEED;

                    }

          }

}

function moveEnemies():void

{

          var tempEnemy:MovieClip;

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

          {

                    tempEnemy = enemies;

                    if (tempEnemy.dead)

                    {

                              score++;

                              score++;

                              roachLevel.score_txt.text = String(score);

                              enemies.splice(i,1);

                    }

                    else

                    {

                              tempEnemy.rotation += (Math.round(Math.random()*.4));

                              tempEnemy.y +=  (Math.cos((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;

                              if (tempEnemy.x < 10)

                              {

                                        tempEnemy.x = 11;

                              }

                              if (tempEnemy.x > stage.stageWidth - offset)

                              {

                                        tempEnemy.x = stage.stageWidth - offset;

                              }

                              if (tempEnemy.y > stage.stageHeight)

                              {

                                        removeEnemy(i);

                                        lives--;

                                        roachLevel.lives_txt.text = String(lives);

                              }

                    }

          }

}

I don't want to use classes. Just the code inside the layer.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
December 22, 2013

why are you posting duplicate messages when i just showed in a duplicate post about object pooling?

razer65Author
Known Participant
December 22, 2013

I don't really understand classes. Can I just make a virtual pool inside the layer I am working with?

kglad
Community Expert
Community Expert
December 23, 2013

check your duplicate thread where i gave your the code for an enemy_pool class.