设置影片剪辑宽度和高度失败

Setting movieClip width and height fails

这里有一个奇怪的问题——希望我犯了一个非常愚蠢的错误。

在 AIR 项目中,我使用 SWFLoader 的实例在自定义 class(MovieClip 的子class)中加载本地 .swf。当 Event.COMPLETE 事件发生时,将调用下面的方法。没什么特别的。

问题是,当我在该方法中设置我的自定义 class 的宽度和高度时,有时 "takes" 有时不会。这是用于动态加载其中一些的控制台输出。我正在使用虚拟值 (100)。加载的 swfs 完全相同,每个都加载成功,但我的自定义 class 的一个实例反映了尺寸集,但另一个没有。

     swf 20 50
     this: instance1120
     width: 100 height: 100
     x: 100 y: 100
     rotation: 0


     swf 20 50
     this: instance1122
     width: 0 height: 250
     x: 100 y: 100
     rotation: 0

protected function btn_completeHandler(event:Event):void
        {
            eventBtn.removeEventListener(Event.COMPLETE, btn_completeHandler);

            if(_source.type == "swf"){
                swf = eventBtn.content as MovieClip;

                trace("swf", swf.width, swf.height);

                this.removeChildren();
                this.addChild(swf);

                swf.x = -swf.width/2;
                swf.y = -swf.height/2;

                this.x = 100;
                this.y = 100;
                this.width = 100;
                this.height = 100;
                this.rotation = 0;

                trace("this:", this.name);
                trace("width:", this.width, "height:", this.height);
                trace("x:", this.x, "y:", this.y);
                trace("rotation:", this.rotation);

                trace("\n");
            }
        }

这可能对您有帮助: Set width/height of a movieclip in AS3

在更改其父项的宽度之前,您应该确保正确添加了 swf 子项。

我会尝试这样的事情:

    protected function btn_completeHandler(event:Event):void
    {
        eventBtn.removeEventListener(Event.COMPLETE, btn_completeHandler);

        if(_source.type == "swf"){
            swf = eventBtn.content as MovieClip;

            trace("swf", swf.width, swf.height);

            this.removeChildren();
            swf.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            this.addChild(swf);

            swf.x = -swf.width/2;
            swf.y = -swf.height/2;
        }
    }

    private function onAddedToStage(e:Event):void
    {
        this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        this.x = 100;
        this.y = 100;
        this.width = 100;
        this.height = 100;
        this.rotation = 0;

        trace("this:", this.name);
        trace("width:", this.width, "height:", this.height);
        trace("x:", this.x, "y:", this.y);
        trace("rotation:", this.rotation);

        trace("\n");
    }