4
public class MyClass extends MovieClip {
            public function MyClass():void {
                my_mc.addEventListener(MouseEvent.CLICK, action);
            }
            private function action(e:MouseEvent):void {
                trace("cliked");
            }
        }

Timeline code

 var myClass:MyClass = new MyClass();
    addChild(myClass);

I can't able to access the my_mc(placed in FLA) movieclip. How do I access?

Benny
  • 2,250
  • 4
  • 26
  • 39
  • Did you add the `my_mc` object manually, and if so does it have an instance name of "my_mc"? or did you add the `my_mc` display object programmatically? If so was it on the timeline with the code `stage.addChild(my_mc);` or `addChild(my_mc);`? or is in simply in the library and exported for actionscript on the first frame? – Taurayi Sep 02 '11 at 09:24
  • I have drawn a `MC` on the stage and I gave a instance name `my_mc`. – Benny Sep 02 '11 at 09:36

2 Answers2

6

Try this:

public class MyClass extends MovieClip
{
    public function MyClass()
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);

    }// end function

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);

        var myMc:MovieClip = stage.getChildByName("my_mc") as MovieClip;
        // var myMc:MovieClip = parent.getChildByName("my_mc") as MovieClip;

        myMc.addEventListener(MouseEvent.CLICK, onMyMcClick)

    }// end function

    private function onMyMcClick(e:MouseEvent)
    {
        trace("clicked");

    }// end function

}// end class

If this doesn't work(which I don't think it will), its because your my_mc display object isn't a child of the stage, but the child of an instance of MainTimeline. If so, then simply comment out the following statement in the above code:

var myMc:MovieClip = stage.getChildByName("my_mc") as MovieClip;

and uncomment the following statement in the above code:

// var myMc:MovieClip = parent.getChildByName("my_mc") as MovieClip;

If my assumption is correct, the my_mc and myClass display objects share the same parent.

Taurayi
  • 3,209
  • 2
  • 17
  • 15
  • `MovieClip(parent).my_mc.addEventListener(MouseEvent.CLICK, onMyMcClick);` working... thanks. – Benny Sep 02 '11 at 11:00
  • **display objects share the same parent.** i didn't get this. Could you explain please? – Benny Sep 02 '11 at 11:34
  • 1
    "**The `my_mc` and `myClass` display objects** share the same parent", basically they're both children of the same display object container which is the `MainTimeline` display object container. – Taurayi Sep 02 '11 at 11:47
  • Is `MainTimeline` and `stage` both are different? – Benny Sep 02 '11 at 11:51
  • 1
    Take a look at the "**How stage, root, and MainTimeline Fit Together**" post at http://www.kirupa.com/forum/showthread.php?223798-ActionScript-3-Tip-of-the-Day/page26, this is where I learnt about `MainTimeline`. – Taurayi Sep 02 '11 at 11:56
0

If my_mc is a MovieClip on the stage of MyClass, you may be trying to access it too early. Constructor code generally executes before the first frame is drawn, so you need to wait for that drawing to take place by listening for Event.ADDED_TO_STAGE:

public class MyClass extends MovieClip {
    public function MyClass():void {
        if(stage) {
            init();
        } else {
            addEventListener(Event.ADDED_TO_STAGE,init);
        }
    }

    private function init(e:Event = null):void {
        if(e) removeEventListener(Event.ADDED_TO_STAGE,init);
        stage.my_mc.addEventListener(MouseEvent.CLICK, action);
    }

    private function action(e:MouseEvent):void {
        trace("cliked");
    }
}
shanethehat
  • 15,460
  • 11
  • 57
  • 87
  • nope... not working. If I use `MyClass` as a document class then its working. – Benny Sep 02 '11 at 08:51
  • ok, so what is `my_mc`? Is it an object on the main timeline or an object inside of MyClass? What error do you get? – shanethehat Sep 02 '11 at 08:52
  • It is an object on the stage. `1120: Access of undefined property my_mc.` – Benny Sep 02 '11 at 08:54
  • ah, ok, see my edit. You just need to look for `stage.my_mc`, and so you will still need the code I've provided to ensure the stage object is available. – shanethehat Sep 02 '11 at 08:54