0

我有一个 Scaleform 电影,我想将它用作游戏用户界面的容器。我希望它能够加载和卸载将用作不同 HUD 和菜单的其他 swf 文件。当我加载一个 swf 时,我需要 Actionscript 来注册 DisplayObject 的名称,这样游戏就会知道刚刚加载了哪个“视图”(即 HUD、暂停菜单、商店菜单等)。

我可以使用 Loader.load() 加载其他 swf,但由于某种原因,我无法更改它们的名称。我不断收到错误 1074。

[编辑:添加有关错误的更多信息。“错误 #1074:非法写入只读属性。” 显然我正在尝试写入只读属性。那么如何使该属性不是只读的呢? name在我正在加载的任何其他 UIComponents 中不是只读的。]

    public function loadView(viewName:String, movieFileName:String):void
    {
        var loader:Loader = new Loader();
        var url:URLRequest = new URLRequest(movieFileName);
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
        loader.name = viewName;
        loader.load(url);
    }
    
    private function loaderComplete(e:Event):void
    {
        var loader:Loader = e.currentTarget.loader as Loader;
        var content:DisplayObject = LoaderInfo(e.target).content; // This returns the content I'm looking for, but I always get error 1074 if I try changing its name
        // var content:DisplayObject = loader.getChildAt(0); // This also returns the content I'm looking for, but it also gives me error 1074 if I try changing its name
        
        // content.name = loader.name; // This line always gives me error 1074
        
        // var newView:View = View(content); // Even if I try casting the content as a custom .as class...
        // newView.setName(loader.name); // public function setName(newName:String):void { this.name = newName; } // ...I still get error 1074
        
        addChild(content);
    }

我只是不允许更改返回的 swf 电影的名称属性吗?我可以在 swf 的文档类中设置名称吗?我也尝试过,但是无论我在文档类中的何处更改名称(他们的类扩展了 scaleform.clik.core.UIComponent,我尝试在构造函数和 configUI 中设置名称),它似乎总是被覆盖我添加孩子()。

[还有另一个编辑。显然对“名称”属性有些混淆。这是它的工作原理...]

我从这段代码开始。我只是把它放在我电影的第一帧。

import TestUIComponent;
var testUIComponent:TestUIComponent = new TestUIComponent();
testUIComponent.name = "Something something";
trace("This is the testUIComponent's name: " + testUIComponent.name);
addChild(testUIComponent);

这是 TestUIComponent 类:

package  {
    import scaleform.clik.core.UIComponent;
        
    public class TestUIComponent extends UIComponent {
        
        public function TestUIComponent() {
        }

        override protected function configUI():void {   
            super.configUI();
            enableInitCallback = true;
        }
    }
}

那里没什么好看的。它只是一个 Actionscript 3 scaleform.clik.core.UIComponent (需要指定,因为我认为不同的包中至少有 3 个不同的 UIComponents)。enableInitCallback 是以前在 Flash 的属性面板中可见的属性,但现在在 AS 3 中,您似乎只能在代码中更改它。

所以我运行该代码,这就是我所看到的:

This is the testUIComponent's name: Something something
CLIK Load: root.Something something

如果我注释掉该行

// testUIComponent.name = "Something something";

然后运行代码,这就是我看到的:

This is the testUIComponent's name: instance1
CLIK Load: root.instance1

回到我最初的问题,“CLIK Load:”之后的文本是从 UI 发送到游戏的名称。我需要这个名字有意义,这样游戏才能知道刚刚加载了什么。我尝试加载的 swf 文件具有作为 scaleform.clik.core.UIComponent 子级的文档类,因此我认为它们的名称属性的工作方式与上面的 TestUIComponent 相同。显然它没有。正如您在顶部看到的那样,我什至将 loader.content 转换为 View(它是 UIComponent 的子项),但我仍然无法更改名称。

4

1 回答 1

0

This is what I meant in the comments. Try something like this:

//... where you declare your variables, make them public to use in other functions...
public var myString = "";


//... later where you declare functions...
public function loadView(viewName:String, movieFileName:String):void
{
    var loader:Loader = new Loader();
    var url:URLRequest = new URLRequest(movieFileName);
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
    //loader.name = viewName;
    myString = viewName; //# 1) update String here to re-access in next function...
    loader.load(url);
}

private function loaderComplete(e:Event):void
{
    var content:DisplayObject = LoaderInfo(e.target).content; // This returns the content I'm looking for, but I always get error 1074 if I try changing its name
    
    // content.name = loader.name; // This line always gives me error 1074
    content.name = myString; //# 2) set content name to whatever myString holds (ie the viewName)...
    
    addChild(content);
}
于 2021-05-17T15:06:40.103 回答