简体   繁体   English

Actionscript 3:如何更改 Loader.load() 返回的内容的名称属性? 错误 1074

[英]Actionscript 3: How can I change the name property of content returned by Loader.load()? Error 1074

I have a Scaleform movie that I want to serve as the container for my game's user interface.我有一个 Scaleform 电影,我想将它用作游戏用户界面的容器。 I want it to be able to load and unload other swf files that will serve as different HUDs and menus.我希望它能够加载和卸载将用作不同 HUD 和菜单的其他 swf 文件。 And when I load a swf, I need Actionscript to register the name of the DisplayObject, so the game will know which "view" (ie, HUD, pause menu, shop menu, etc.) just loaded.而当我加载一个 swf 时,我需要 Actionscript 来注册 DisplayObject 的名称,这样游戏就会知道刚刚加载了哪个“视图”(即 HUD、暂停菜单、商店菜单等)。

I am able to load other swfs using Loader.load(), but for some reason I can't change their names.我可以使用 Loader.load() 加载其他 swf,但由于某种原因,我无法更改它们的名称。 I keep getting error 1074.我不断收到错误 1074。

[Edit: Adding more info on the error. [编辑:添加有关错误的更多信息。 "Error #1074: Illegal write to read-only property." “错误 #1074:非法写入只读属性。” Apparently I'm trying to write to a read-only property.显然我正在尝试写入只读属性。 So how do I make that property not-read-only?那么如何使该属性不是只读的呢? name isn't read-only in any other UIComponents I'm loading.] 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);
    }

Am I just not allowed to change the name property of swf movies that get returned?我只是不允许更改返回的 swf 电影的名称属性吗? Can I set the name in the document class of the swf?我可以在 swf 的文件 class 中设置名称吗? I tried that too, but no matter where I change the name inside the document class (their class extends scaleform.clik.core.UIComponent, and I try setting the name in the constructor and in configUI), it always seems to get overwritten when I addChild().我也尝试过,但是无论我在文档 class 中的哪个位置更改名称(他们的 class 扩展了 scaleform.clik.core.UIComponent,我尝试在构造函数和 configUI 中设置名称),它似乎总是被覆盖我添加孩子()。

[And another edit. [还有另一个编辑。 Apparently there is some confusion over the "name" property.显然对“名称”属性有些混淆。 Here's how it works...]这是它的工作原理...]

I start off with this code.我从这段代码开始。 I just put it in frame 1 of my movie.我只是把它放在我电影的第一帧。

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

This is the class TestUIComponent:这是 class TestUIComponent:

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

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

Nothing fancy there.那里没什么好看的。 It's just an Actionscript 3 scaleform.clik.core.UIComponent (need to specify that because I think there are at least 3 different UIComponents in different packages).它只是一个 Actionscript 3 scaleform.clik.core.UIComponent (需要指定,因为我认为不同的包中至少有 3 个不同的 UIComponents)。 enableInitCallback is a property that used to be visible in Flash's properties panel, but now in AS 3, it seems you can only change it in code. enableInitCallback 是以前在 Flash 的属性面板中可见的属性,但现在在 AS 3 中,您似乎只能在代码中更改它。

So I run that code, and this is what I see:所以我运行该代码,这就是我所看到的:

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

If I comment out the line如果我注释掉该行

// testUIComponent.name = "Something something";

and then run the code, this is what I see:然后运行代码,这就是我看到的:

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

Going back to my original problem, the text that comes after "CLIK Load:" is the name that is getting sent from the UI to the game.回到我最初的问题,“CLIK Load:”之后的文本是从 UI 发送到游戏的名称。 I need that name to be something meaningful so the game knows what just got loaded.我需要这个名字有意义,这样游戏才能知道刚刚加载了什么。 The swf files I am trying to load have Document Classes that are children of scaleform.clik.core.UIComponent, so I thought their name properties would work the same way as the TestUIComponent above.我尝试加载的 swf 文件具有作为 scaleform.clik.core.UIComponent 子级的文档类,因此我认为它们的名称属性的工作方式与上面的 TestUIComponent 相同。 Apparently it doesn't.显然它没有。 And as you can see all the way back up at the top, I even cast the loader.content as a View (which is a child of UIComponent), and I still can't change the name.正如您在顶部看到的那样,我什至将 loader.content 转换为 View(它是 UIComponent 的子项),但我仍然无法更改名称。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理Loader.load()中的SecurityError#2070和#2000 - How to handle the SecurityError #2070 and #2000 in Loader.load() Loader.load和Loader.loadBytes的区别 - Loader.load and Loader.loadBytes differences 如何禁止用户通过AS3 / Flash Loader.load()加载swf文件,而仅允许加载图像? - can I prohibit users from loading swf files by AS3 / Flash Loader.load() and only allow loading images? 在 Flash/AS3 中使用 Loader.load() function 时是否存在挂起或延迟? - Is there a hang or lag when using the Loader.load() function in Flash/AS3? 如何仅从Flex 3中的Actionscript使用SWFLoader并动态加载内容? - How can I use SWFLoader purely from Actionscript in Flex 3 and dynamically load content? Actionscript 加载程序错误 #1009 - Actionscript Loader Error #1009 如何判断Actionscript对象是否具有某个动态属性? - How can I tell if an Actionscript object has a certain dynamic property on it? loader.load((newSource is URLRequest)?newSource:new URLRequest(newSource))到底是什么? 做? - What the heck does loader.load((newSource is URLRequest)? newSource:new URLRequest(newSource)); do? 我如何从数组中获取类名 - actionscript how can I get class name from array Actionscript 3:如何获取符号名称,而不是实例名称 - Actionscript 3: How can I get the symbol name, and not the instance name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM