3 Replies Latest reply on May 30, 2010 9:29 PM by kglad

    Placing an array of objects onto stage

    HandofNerds

      I am trying to place an array of objects onto a stage where they drop vertically. I have been able to do it by placing the array objects into an object called "bag" but have found that the bag object only contains the last array object id so I can not apply actions to various array objects.

      Also my objects do not loop and only appear once.

       

      Below is the code so far.

       

      var tempArray:Array = new Array(); // Stores randomised values

       

      var bagArray:Array = new Array("gslow_id","gshigh_id","glow_id","ghigh_id","gdiv_id",

        "gcall_id","gbust_id","gbull_id","gboom_id","gbear_id"); // List of bag names

       

      function createBags() {

       

      if (tempArray.length == 0) { // check if array is empty

           for (var i:Number = 0; i < targets; i++) { // for loop - creat variable bagName to the value of targets

       

           var bagName: String = bagArray[i];

       

           var depthLevel:Number = this.getNextHighestDepth();

           // attach movies to time line

           this.attachMovie(bagName, bagName, depthLevel, {_x:random(350) + 100, _y:random(0) + spacing});

       

           spacing += 45;

       

           tempArray.push(bagName);

           //trace(tempArray);

       

           bag = _root[bagName]; // create a bag object contain the bagName variable

       

           //trace (bag);

       

           bag.onEnterFrame = function() {

       

                if (this._x < 570 && this._y > 0) {

                this._y += speed * random(8);

                } else {

                this._x = random(500);

                } // end if (this._x < 570 && this._y > 0)

           } // end bag.onEnterFrame ()

       

      bag.onPress = function() {

       

      // if bag is pressed select action

      //trace (bag); 

       

      xPos = this._x

      yPos = this._y

      unloadMovie(this);

       

      score += 1000

      scoreText_txt.text = score;

       

      //trace(score);

       

      checkScore();

       

       

      } // end bag.onPress ()

       

      //trace(targets);

      //trace(bagName);

       

      } // end for (var i:Number = 0; i < targets; i++)

      } // end if (tempArray.length == 0)

      } // End createBags()

        • 1. Re: Placing an array of objects onto stage
          kglad Adobe Community Professional & MVP

          try:

           

          var bagA:Array=[];

           

           

          var bagArray:Array = new Array("gslow_id","gshigh_id","glow_id","ghigh_id","gdiv_id",

            "gcall_id","gbust_id","gbull_id","gboom_id","gbear_id"); // List of bag names

           

          function createBags() {

           

          if (tempArray.length == 0) { // check if array is empty

               for (var i:Number = 0; i < targets; i++) { // for loop - creat variable bagName to the value of targets

           

               var bagName: String = bagArray[i];

           

               var depthLevel:Number = this.getNextHighestDepth();

               // attach movies to time line

               var bag:MovieClip = this.attachMovie(bagName, bagName, depthLevel, {_x:random(350) + 100, _y:random(0) + spacing});

            bagA.push(bag);

               spacing += 45;


           

               //trace (bag);

           

               bag.onEnterFrame = function() {

           

                    if (this._x < 570 && this._y > 0) {

                    this._y += speed * random(8);

                    } else {

                    this._x = random(500);

                    } // end if (this._x < 570 && this._y > 0)

               } // end bag.onEnterFrame ()

           

          bag.onPress = function() {

           

          // if bag is pressed select action

          //trace (bag); 

           

          xPos = this._x

          yPos = this._y

          unloadMovie(this);

           

          score += 1000

          scoreText_txt.text = score;

           

          //trace(score);

           

          checkScore();

           

           

          } // end bag.onPress ()

           

          //trace(targets);

          //trace(bagName);

           

          } // end for (var i:Number = 0; i < targets; i++)

          } // end if (tempArray.length == 0)

          } // End createBags()

          • 2. Re: Placing an array of objects onto stage
            HandofNerds Level 1

            Thanks but it did not work

             

            I decided to create a movie clip call bag with an identifier bag_mc in my library and added the 10 different movie clips to on each frame.

             

            Screen shot 2010-05-31 at 12.59.27 PM.png

             

            and have changed my code to this but now I only get the same movie clip each time. Plus it only rotates once

             

            function createBags() {

             

            var counter:Number = 0;

             

            if (tempArray.length == 0) { // check if array is empty

            for (var i:Number = 0; i < targets; i++) { // for loop - creat variable bagName to the value of targets

             

            var bagName: String = "bag_mc" + (String)(counter);

            //trace(bagName);

            var depthLevel:Number = this.getNextHighestDepth();

            // attach movies to time line

            this.attachMovie("bag_mc", bagName, depthLevel, {_x:random(350) + 100, _y:random(0) + spacing});

             

            spacing += 45;

             

            _root[bagName].bagNbr = counter;

             

            counter++;

             

            _root[bagName].gotoAndStop(1);

             

             

             

            bag = _root[bagName]; // create a bag object contain the bagName variable

             

            //trace (bag);

             

            bag.onEnterFrame = function() {

             

            if (this._x < 570 && this._y > 0) {

            this._y += speed * random(8);

            } else {

            this._x = random(500);

            } // end if (this._x < 570 && this._y > 0)

            } // end bag.onEnterFrame ()

             

            bag.onPress = function() {

             

            // if bag is pressed select action

            //trace (bag);

             

            xPos = this._x

            yPos = this._y

            unloadMovie(this);

             

            score += 1000

            scoreText_txt.text = score;

             

            //trace(score);

             

            checkScore();

             

             

            } // end bag.onPress ()

             

            //trace(targets);

            //trace(bagName);

             

            } // end for (var i:Number = 0; i < targets; i++)

            } // end if (tempArray.length == 0)

            } // End createBags()

            • 3. Re: Placing an array of objects onto stage
              kglad Adobe Community Professional & MVP

              the code i gave works.

               


              but, you're welcome.