• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Object follows mouse after clicked

Explorer ,
Aug 23, 2011 Aug 23, 2011

Copy link to clipboard

Copied

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 ?

TOPICS
ActionScript

Views

575

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 24, 2011 Aug 24, 2011

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

...

Votes

Translate

Translate
Participant ,
Aug 23, 2011 Aug 23, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 23, 2011 Aug 23, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 23, 2011 Aug 23, 2011

Copy link to clipboard

Copied

Anyone can help ...?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2011 Aug 24, 2011

Copy link to clipboard

Copied

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

...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 24, 2011 Aug 24, 2011

Copy link to clipboard

Copied

That's absolutely correct ..thanks.

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2011 Aug 24, 2011

Copy link to clipboard

Copied

LATEST

Oops you're right - silly mistake

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines