6 Replies Latest reply on Jun 17, 2012 9:48 AM by Ned Murphy

    Doesn't work my addChild command on Class

    ceorgejakel

      My project running without problem, but when I'm click my object in the stage, I'm taking "TypeError: Error #1009: Cannot access a property or method." error. Here my project https://rapidshare.com/files/1556610915/Proje.rar

      my code in fla:

      import Gosterge;

      var gosterg:Gosterge = new Gosterge();

      gosterg.gostergeleriEkle(a,c,k1);

       

      my code in Gosterge.as:

      package

      {

                import flash.display.Sprite;

                import flash.events.MouseEvent;

       

       

                public class Gosterge extends Sprite

                {

                          public var cerceve:Sprite = new Sprite  ;

                          public function Gosterge():void

                          {

                                    // constructor code

                          }

                          private function tikla(evt:MouseEvent):void

                          {

                                    trace("tikla çalıştı");

                                    cerceve.graphics.lineStyle(1, 0x0000FF, 1);

                                    cerceve.graphics.lineTo(100,0);

                                    cerceve.graphics.lineTo(100,100);

                                    cerceve.graphics.lineTo(0,100);

                                    cerceve.graphics.lineTo(0,0);

                                    cerceve.x = evt.currentTarget.x - 5;

                                    cerceve.y = evt.currentTarget.y - 5;

                                    stage.addChild(cerceve);

                          }

                          public function gostergeleriEkle(... args):void

                          {

                                    for (var i:uint = 0; i < args.length; i++)

                                    {

                                              args[i].addEventListener(MouseEvent.CLICK, tikla);

                                    }

                          }

                }

      }

       

      Please help, I can't find problem.

        • 1. Re: Doesn't work my addChild command on Class
          Ned Murphy Legend

          Chances are the stage is not a defined entity for the class.  You might need to add an event listener for ADDED_TO_STAGE wherein the event handler establishes the stage for the class.  See if the following tutorial helps solve anything, and if niot, try searching Google using "AS3 ADDED_TO_STAGE"

           

          http://www.emanueleferonato.com/2009/12/03/understand-added_to_stage-event/

          1 person found this helpful
          • 2. Re: Doesn't work my addChild command on Class
            ceorgejakel Newcomer

            Thank you for your quick response. I will try now, I wasted 2 days for these problem. If I can solve problem, I will write here

            • 3. Re: Doesn't work my addChild command on Class
              ceorgejakel Newcomer

              I tried your advice, but I can't solve problem. Can you edit my project? if you help me, I would greatful.

              • 4. Re: Doesn't work my addChild command on Class
                Ned Murphy Legend

                Will itsolve your problem if you pass the stage to the function as an argument.  Since the class is not added to the stage, then it probably needs to be provided a reference to it.  See the bolded changes/additions to the code below...

                 

                import Gosterge;

                var gosterg:Gosterge = new Gosterge();

                gosterg.gostergeleriEkle(this.stage,a,c,k1);

                 

                package
                {
                          import flash.display.Sprite;
                          import flash.display.Stage;
                          import flash.events.MouseEvent;

                 

                          public class Gosterge extends Sprite

                          {

                                    public var cerceve:Sprite = new Sprite  ;
                                    public var _stage;


                                    public function Gosterge():void
                                    {
                                              // constructor code
                                    }

                                    private function tikla(evt:MouseEvent):void
                                    {
                                              trace("tikla çalıştı");
                                              cerceve.graphics.lineStyle(1, 0x0000FF, 1);
                                              cerceve.graphics.lineTo(100,0);
                                              cerceve.graphics.lineTo(100,100);
                                              cerceve.graphics.lineTo(0,100);
                                              cerceve.graphics.lineTo(0,0);
                                              cerceve.x = evt.currentTarget.x - 5;
                                              cerceve.y = evt.currentTarget.y - 5;
                                              _stage.addChild(cerceve);

                                    }

                                    public function gostergeleriEkle(stag:Stage,... args):void
                                    {
                                             _stage = args[0];


                                              for (var i:uint = 1; i < args.length; i++)
                                              {
                                                        args[i].addEventListener(MouseEvent.CLICK, tikla);
                                              }
                                    }
                          }
                }

                • 5. Re: Doesn't work my addChild command on Class
                  ceorgejakel Newcomer

                  Thank you very much, thank you very much, really thank you very much , I finally understood. It works now . My class couldn't reach the stage. Mr. Ned, you're the best , I'm so grateful