as3尝试从movieclip内部访问主时间轴变量

时间:2016-04-03 07:12:27

标签: actionscript-3 variables movieclip coercion

我刚刚找到了办法,但我仍然遇到错误。

在主时间轴上我有:

var onBeat:Boolean = new Boolean;

在影片剪辑中,我尝试使用:

访问它
MovieClip(root).onBeat = true;

从技术上讲,变量会发生变化。但它抛出了这个导致问题的错误:

  

错误#1034:类型强制失败:无法将flash.display :: Stage @ 7fffaa2c0d1转换为flash.display.MovieClip。

这只是一项不可能完成的任务吗?

1 个答案:

答案 0 :(得分:0)

You got that error because you've added your MovieClip to the stage's display list and not to the main timeline one.

So in your main timeline code (or your document class), you can add your MovieClip using addChild(your_mc_instance) or this.addChild(your_mc_instance).

But you can also get a working code even with your MovieClip instance added to the stage using, for example :

var _root:DisplayObjectContainer = DisplayObjectContainer(root);
MovieClip(_root.getChildAt(0)).onBeat = true;

_root.getChildAt(0) here is returning your main timeline instance as it has also been added to the stage's display list before any other object.

Hope that can help.