Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Dec 22, 2013 Dec 22, 2013

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.

TOPICS
ActionScript
494
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2013 Dec 22, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 22, 2013 Dec 22, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2013 Dec 22, 2013
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines