6 Replies Latest reply: Aug 24, 2011 7:55 AM by kennethkawamoto2 RSS

    Object follows mouse after clicked

    he11f1re Community Member

      The title says what I want to achieve.

      My code is this :

      package
      {
          import flash.display.*;
          import flash.events.*;
          public class Sample extends MovieClip {
              public function Sample():void {
                  var c:Rreth = new Rreth  ;
                  c.x = 275;
                  c.y = 205;
                  c.buttonMode = true;
                  c.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
                  addChild(c);
              }
              public function MouseDown(e:MouseEvent):void {
                  c.addEventListener(Event.ENTER_FRAME, moveMyRreth);
              }
              public function moveMyRreth(event:Event):void {
                  var thisRreth:Rreth = (event.target as Rreth);
                  thisRreth.x -=  thisRreth.x - mouseX;
                  thisRreth.y -=  thisRreth.y - mouseY;
              }
          }
      }

      The error thrown is :

      C:\Users\Adi\Desktop\Sample\Sample.as, Line 15    1120: Access of undefined property c.

      Who can help me achieve the result I want ?

        • 1. Re: Object follows mouse after clicked
          smitchell94 Community Member

          The error is telling you. c is a undefined property. Try capitalise c.

          so :

           

          package
          {
              import flash.display.*;
              import flash.events.*;
              public class Sample extends MovieClip {
                  public function Sample():void {
                      var c:Rreth = new Rreth  ;
                      C.x = 275;
                      C.y = 205;
                      C.buttonMode = true;
                      C.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
                      addChild(C);
                  }
                  public function MouseDown(e:MouseEvent):void {
                      C.addEventListener(Event.ENTER_FRAME, moveMyRreth);
                  }
                  public function moveMyRreth(event:Event):void {
                      var thisRreth:Rreth = (event.target as Rreth);
                      thisRreth.x -=  thisRreth.x - mouseX;
                      thisRreth.y -=  thisRreth.y - mouseY;
                  }
              }
          }

           

          I dont know what c is, if it is in the library look at it see exactly what you named it, AS3 is a case sensative language.

           

          It might be the case that you did not give the object a instance name

           

          If you did not give it a instance name, drag it onto the stage give it a instance then delete it.

          • 2. Re: Object follows mouse after clicked
            he11f1re Community Member

            Rreth is the AS3 linkage for the movieclip on my library.

            c is just a variable that copies the movieclip Rreth and displays it on stage after addChild() method.

            It gives the same error even after changing c to C.

            • 3. Re: Object follows mouse after clicked
              he11f1re Community Member

              Anyone can help ...?

              • 4. Re: Object follows mouse after clicked
                kennethkawamoto2 Community Member

                Your var c is defined within the function Sample therefore function MouseDown has no idea what c is. Define outside the function:

                ...
                private var c:Rreth;
                
                public class Sample extends MovieClip {
                        public function Sample():void {
                            c = new Rreth();
                ...
                
                • 5. Re: Object follows mouse after clicked
                  he11f1re Community Member

                  That's absolutely correct ..thanks.

                  I think you made a typo ...the private var must be made inside class

                  • 6. Re: Object follows mouse after clicked
                    kennethkawamoto2 Community Member

                    Oops you're right - silly mistake