2 Replies Latest reply on Jan 16, 2011 12:09 PM by Proch92

    Give an instance name to addChilded object


      i am newbie of AS and i'm going in trouble with instance names, classes, methods, and addChilded object...


      that's my code:

      - i have a class that (everytime the function is called by the timer) add an object(class "Monster") to the stage.

      public function onTick(timerEvent:TimerEvent):void


           var newEnemy:Monster = new Monster();

           army.push( newEnemy );                              //army is an array

           addChild( newEnemy );

           for each ( var enemy:Monster in army )


             //move the object





      - and i have another class called "Base" that add to the stage an object(class "Weapon") everytime the function is called by a timer. "Weapon" has another function that is called by another timer, that moves the object in the stage.


      IN THE Base CLASS:

      public function Launch(timerEvent:TimerEvent):void


           var weapon = new Weapon(sinX,sinY,power,65,240);

           stage.addChild (weapon);



      IN THE Weapon CLASS:

      public function MoveWeapon(timerEvent:TimerEvent):void


           x = x + Vx;

           y = y - Vy;




      Ok... Probably i have posted the most ugly and incorrect code you've ever seen, but now i want to explain what i want to do:


      I want to instert the hitTestObject into the MoveWeapon function so that when one of the launched weapons hit one of the generated enemy the weapon and the enemy colliding goes removed from the stage.

      This seemed easy when i immaginated it, but when i went to write the code i didn't know what to pass to the method (this.hitTestObject (?????)).


      I only know that my enemies are collected inside an array, but how can i call them? which is their instance name?


      I also tried to avoid the problem changing my point of view; so i inserted the hitTestObject test into the loop for each ( var enemy:Monster in army ) but the same problem occurred, i have a lot of balls that doesn't have an instance name (or they have all the same one).


      This is what i created until now (if it can help you): http://www.box.net/files#/files/0/f/64218257/1/f_594818839


      Can anyone help me?

        • 1. Re: Give an instance name to addChilded object
          Ned Murphy Adobe Community Professional & MVP

          You can just use the army array to target the enemies.  You cannot assign instance names to dynamically added content.  You can add a value to their name property, but that is not an instance name, and it is unecessary since you do have direct access to the via the array.... as in..


          this.hitTestObject (army[index])

          • 2. Re: Give an instance name to addChilded object
            Proch92 Level 1

            thank you for the simple explanation; i really appreciate it, and i think i undrestood now how it works, but....

            i found this error using the function

            if ( this.hitTestObject(RAZ.army[0]) )

            ("RAZ" is the class where the "army" array is created and controlled)

            ...\Weapon.as, Line 41 1119: Access of possibly undefined property army through a reference with static type Class.


            how can i access to the array?


            P.S. i know this is a stupid question but i really don't know where to search to solve the problem...