如何使用 as3 类访问舞台上的电影剪辑? [英] How do I access a movieClip on the stage using as3 class?
本文介绍了如何使用 as3 类访问舞台上的电影剪辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class MyClass extends MovieClip {
public function MyClass():void {
my_mc.addEventListener(MouseEvent.CLICK, action);
}
private function action(e:MouseEvent):void {
trace("cliked");
}
}
时间线代码
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
显示对象不是舞台的子级,而是 my_mc
显示对象的子级代码>主时间线代码>.如果是这样,那么只需注释掉上面代码中的以下语句:
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;
如果我的假设是正确的,my_mc
和 myClass
显示对象共享同一个父对象.
If my assumption is correct, the my_mc
and myClass
display objects share the same parent.
这篇关于如何使用 as3 类访问舞台上的电影剪辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文