2 Replies Latest reply on Oct 17, 2014 12:47 PM by kglad

    Duplicated MovieClips disappearing during play


      am making my own version of a spaceship fighting game. At one point I want to randomly spawn enemies. Here is what I am doing.

      I have a movieclip in the library called enemyShip, AS-Linkage is normalEnemy. Object not placed on the scene. On the actionScipt on that frame, I have the code bellow, which does that for me. What is happening is this: when I publish and run the swf file, the first two enemies spwan properly, starting from the third each new spawn 'hides', but does not delete, the one before it. the second enemy is the first to disappear. The first enemy stays visible. Ideas please? I have tried duplicateMovieClip and attachMovieClip and they both have the same result. Sorry for the long question!


      var origEnemy:MovieClip = _root.attachMovie("normalEnemy", "origEnemy",  this.getNextHighestDepth();
      var numberOfEnemies:Number = 1;
      var enemyIndex:Number = 0;

      = setInterval(spawnEnemy,1000 + Math.random() * 2000);
      // Spawns new enemies
      function spawnEnemy()
      if (numberOfEnemies > 10)
      // Randomly calculate the starting coordinates of newly spawned enemy 
      var x_position = Math.random() * 50;
      var y_position = Math.random() * 700;

      // spawn a new enemy and assign random location 
      .push(origEnemy.duplicateMovieClip("enemy" + enemyIndex, this.getNextHighestDepth()));

      //_root.attachMovie("normalEnemy","enemy" + enemyIndex,this.getNextHighestDepth());

      // assign random location
      [enemyIndex]._x = x_position;
      [enemyIndex]._y = y_position;

      // incerement number of enemies
      // Increment enemy Index

        • 1. Re: Duplicated MovieClips disappearing during play
          Ned Murphy Adobe Community Professional & MVP

          If newly created objects are replacing objects, then it sounds like you are repreatedly placing them in the same level.  When you place something at a certain level it replaces whatever is in that level.


          Try using the trace() function to see what your this.getNextHIghestDepth() is generating in your loop.  You will probably find it is not what you think.  If you are adding the items to the _root, then you probably want to get the next highest depth for the _root, not 'this'. (  _root.getNextHIghestDepth();  )

          • 2. Re: Duplicated MovieClips disappearing during play
            kglad Adobe Community Professional & MVP

            if 'this' is not the same as _root, the following line is problematic:


            var origEnemy:MovieClip = _root.attachMovie("normalEnemy", "origEnemy",  this.getNextHighestDepth();

            ie, use:

            var origEnemy:MovieClip = _root.attachMovie("normalEnemy", "origEnemy",  _root.getNextHighestDepth();

            1 person found this helpful