这个问题仍困扰着我。看起来很简单。 我在lib和舞台上有动画片段,它有一个链接类“Box.as”,另一个链接到“Circle.as”。 我想从Circle.as访问Box.as的动画片段,反之亦然。
public class Circle extends MovieClip
{
private var _circle:MovieClip;
private var _box:Box;
public function Circle()
{
_circle = new MovieClip();
if (stage) onStage();
else this.addEventListener(Event.ADDED_TO_STAGE,onStage);
}
private function onStage(e:Event = null)
{
_circle = stage.getChildByName("blue_circle") as MovieClip;
this.addEventListener(Event.ENTER_FRAME,hitTarget);
}
private function hitTarget(e:Event):void
{
if (_circle.hitTestObject(_box.mc)) //test if 2 movieclips are colliding
{ // _box.mc is just created the same as _circle
trace("hi");
}
}
这段代码不起作用。而且我想使用一个即使动画片段不在舞台上也可以访问的(没有实例名称) 希望您能够帮助我。感谢。
答案 0 :(得分:0)
看起来你真的很亲密!您只是忘了创建班级Box
的新实例。因此,在public function Circle()
内添加
_box = new Box();
如果有效,请告诉我。如果没有,您的链接可能有问题...
您的整个代码将如下所示
public class Circle extends MovieClip
{
private var _circle:MovieClip;
private var _box:Box;
public function Circle()
{
_box = new Box();
_circle = new MovieClip();
if (stage) onStage();
else this.addEventListener(Event.ADDED_TO_STAGE,onStage);
}
private function onStage(e:Event = null)
{
_circle = stage.getChildByName("blue_circle") as MovieClip;
this.addEventListener(Event.ENTER_FRAME,hitTarget);
}
private function hitTarget(e:Event):void
{
if (_circle.hitTestObject(_box.mc)) //test if 2 movieclips are colliding
{ // _box.mc is just created the same as _circle
trace("hi");
}
}