2 Replies Latest reply on Aug 5, 2006 5:22 AM by RajeshatOinam

    creating dynamic objects AS 3

    RajeshatOinam
      hey i want to create dynamic objects at runtime but in AS 3...
      i am not able to find a solution...
      suppose there is one class named Box.i want to create 25 objects of Box class.i tried by doing this
      public var i:Number;
      for(i=0; i<25; i++)
      {
      this["box" +1] = new Box(a,b,"str"+i);
      }


      where a and b are the x and y co-ordinates of the newly created object...It throws me this error
      Error: Property box0 not found on Trial and there is no default value.
      at Trial/drawShape()
      at Trial$iinit()

      Trial.as is the name of the file

      Can anyone help me in that.
        • 1. Re: creating dynamic objects AS 3
          Level 7
          public var i:Number;
          for(i=0; i<25; i++)
          {
          var box:Box = new Box(a,b,"str"+i);
          box.name = "box"+i;
          this.addChild(box);

          var target:DisplayObject = this.getChildByName("box"+i);
          trace(target)
          trace(this.getChildIndex(target)); // 0
          }


          • 2. Re: creating dynamic objects AS 3
            RajeshatOinam Level 1
            hey thanks man ...it worked.......thank you very much... and ya one more thing
            how do we change color of those box objects ...i mean i created those objects using yr technique...
            the box classes possess following properties and functions
            Box.as
            ------------
            1) xCord:Number
            2) yCord:Number
            3) strName:String
            4) boxColor:uint
            5) b:MovieClip

            public function changeColor(uint);void
            {

            b.graphics.beginFill(newColor);
            b.graphics.drawRect(this.xc,this.yc,heightRect,widthRect);
            bs.graphics.endFill();
            }


            what i want to do is in ma class where i created objects i want to change color of those objects so i added EventListener there itself..here what the code looks like

            for(i=0; i<5; i++)
            {
            var box:Box = new Box(startGridX,startGridY,"box"+i);
            box.addEventListener(MouseEvent.MOUSE_DOWN,mouseHandler);
            box.name = "box"+i;
            shape1.addChild(box);
            startGridX = startGridX + 52;
            }
            ......
            .........
            public function mouseHandler():void
            {
            .changeColor(0xCCCCFA);
            }

            what i want is when i click on any of the box objects created i want to change the color of those objects

            but i dont know what to write in that mouseHandler function....
            please help