如何访问使用AS3类舞台上的影片剪辑?影片剪辑、舞台上

由网友(感情通缉犯)分享简介:public class MyClass extends MovieClip {public function MyClass():void {my_mc.addEventListener(MouseEvent.CLICK, action);}private function action(e:MouseEvent):...
public class MyClass extends MovieClip {
            public function MyClass():void {
                my_mc.addEventListener(MouseEvent.CLICK, action);
            }
            private function action(e:MouseEvent):void {
                trace("cliked");
            }
        }

时间轴code

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

我不能能够访问 my_mc中(放置在FLA)的影片剪辑。如何访问?

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

推荐答案

试试这个:

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

如果这不起作用(我不认为它会),它因为你的 my_mc中显示对象是不是阶段的孩子,但 MainTimeline 实例的孩子。如果是这样,那么只需注释掉下面的语句在上面的code:

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;

和取消注释在上面code以下语句:

and uncomment the following statement in the above code:

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

如果我的假设是正确的, my_mc中 MyClass的显示对象共享相同的父。

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

阅读全文

相关推荐

最新文章