Skip navigation
ceorgejakel
Currently Being Moderated

Doesn't work my addChild command on Class

Jun 16, 2012 3:02 AM

Tags: #problem #error #flash #3.0 #action #3 #actionscript #ac3 #actionscript3 #actionscript_3.0

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.

 
Replies
  • Currently Being Moderated
    Jun 16, 2012 4:23 AM   in reply to ceorgejakel

    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-e vent/

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 16, 2012 3:29 PM   in reply to ceorgejakel

    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);
                                  }
                        }
              }
    }

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 17, 2012 9:48 AM   in reply to ceorgejakel

    You're welcome

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points