0

嗨这个问题仍然困扰着我。看起来很简单。我在 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");
        }

    }

此代码不起作用。而且我想使用即使电影剪辑不在舞台上(没有实例名称)也可以访问的一个。
希望您能够帮助我。谢谢。

4

1 回答 1

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");
        }

    }
于 2013-08-20T10:08:37.630 回答