首页 > 解决方案 > 加载的 SWF 不会在空中执行其功能

问题描述

回来寻求帮助!所以我正在制作一个 AIR 应用程序,它将 SWF 加载到一个容器中以供用户查看。但是,当我将文件加载到它们的容器中时,加载的 SWF 无法执行它们自己的代码。IE 在加载的 SWF 上按下一个不可见的按钮,它会改变颜色。自从 Security.allowDomain("*"); 在闪存中引发此错误。然而,根据我的阅读,AIR 不允许加载的 swfs 出于某种安全原因执行代码,但我也不能 100% 确定这一点。

SecurityError: Error #3207: Application-sandbox content cannot access this feature.
at flash.system::Security$/allowDomain()
at BugFree()[D:\Desktop\BugFree\BugFree.as:72]

如果没有允许域,它会在尝试单击不可见按钮时引发此安全错误。

*** Security Sandbox Violation ***
SecurityDomain 'file:///D:/Desktop/Rewritten Tester/TechDemoSwordNew.swf' 
tried to access incompatible context 'app:/BugFree.swf'
*** Security Sandbox Violation ***
SecurityDomain 'file:///D:/Desktop/Rewritten Tester/TechDemoSwordNew.swf' 
tried to access incompatible context 'app:/BugFree.swf'
SecurityError: Error #2047: Security sandbox violation: parent: 
file:///D:/Desktop/Rewritten Tester/TechDemoSwordNew.swf cannot access 
app:/BugFree.swf.
at flash.display::DisplayObject/get parent()
at TechDemoSwordNew_fla::Button_Play_21/onButtonPress()

这仅显示在 Animate 输出栏中。当我发布它时,使用嵌入了运行时的应用程序并打开 exe,它不会引发任何错误,但隐形按钮仍然不起作用。

这是正在加载的 swf 的代码。

btnButton.addEventListener(MouseEvent.CLICK, onButtonPress, false, 0, true);
function onButtonPress(event:MouseEvent):void
{
MovieClip(parent).play();
}
stop();

这是按钮内的时间线代码,因为将我的项目放入游戏的游戏公司就是这样做的。我最初提交它时都是在课堂上完成的,但这不是重点。当按下按钮时,加载的 SWF 应该播放然后停止。但是我得到了上面提到的沙盒违规。

用于加载 SWF 的代码如下

public function WeaponLoad()
    {
        if(FileMenu.WeaponFileTxt.text != "")
        {
            LoadWeapon(FileMenu.WeaponFile.nativePath);
        }
        else if(FileMenu.WeaponFileTxt.text == "")
        {
            Character.mcChar.weapon.removeChildAt(0);
            Character.mcChar.weaponOff.removeChildAt(0);
        }
    }

public function LoadWeapon(strFilePath: String)
    {
        WeaponLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, CompleteWeaponLoad);
        WeaponLoader.load(new URLRequest(strFilePath), new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain)));
    }

public function CompleteWeaponLoad(e: Event)
    {
        var WeaponClass: Class;
        if (MiscMenu.WeaponSelect.MainClick.currentFrame != 3)
        {
            try
            {
                trace("WeaponOff");
                WeaponClass = WeaponLoader.contentLoaderInfo.applicationDomain.getDefinition(FileMenu.WeaponLinkTxt.text) as Class;
                this.Character.mcChar.weapon.removeChildAt(0);
                this.Character.mcChar.weaponOff.removeChildAt(0);
                this.Character.mcChar.weapon.addChild(new(WeaponClass)());
            }
            catch (err: Error)
            {
                trace("Either the weapon class doesnt exist or it is wrong");
                this.Character.mcChar.weapon.removeChildAt(0);
                this.Character.mcChar.weaponOff.removeChildAt(0);
            }
        }
        else if (MiscMenu.WeaponSelect.MainClick.currentFrame == 3)
        {
            try
            {
                WeaponClass = WeaponLoader.contentLoaderInfo.applicationDomain.getDefinition(FileMenu.WeaponLinkTxt.text) as Class;
                this.Character.mcChar.weapon.removeChildAt(0);
                this.Character.mcChar.weaponOff.removeChildAt(0);
                this.Character.mcChar.weapon.addChild(new(WeaponClass)());
                this.Character.mcChar.weaponOff.addChild(new(WeaponClass)());
            }
            catch (err: Error)
            {
                trace("Either the weapon class doesnt exist or it is wrong");
                this.Character.mcChar.weapon.removeChildAt(0);
                this.Character.mcChar.weaponOff.removeChildAt(0);
            }
        }
    }

任何帮助都会受到赞赏,因为我不知道如何更改发布设置中的任何安全沙箱设置,因为它对我来说是灰色的。就像我说的那样,我尝试用谷歌搜索它,但我似乎无法找到任何答案。另外值得注意的是,我是一个自学成才的新手,我对 AS3 的很多事情一无所知。我知道我的代码可能会更干净,并且我计划在启动并运行基本程序后对其进行清理并适当减少内存消耗。感谢您的帮助!

标签: securityactionscript-3air

解决方案


您似乎没有正确设置应用程序域。这是 as3 文档中包含的代码:

 var loader:Loader = new Loader();
 var url:URLRequest = new URLRequest("[file path].swf");
 var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);
 loader.load(url, loaderContext);

在您的LoadWeapon函数中使用它。

同时,尽量不要使用大写字母作为变量和方法名的开头。在 ActionScript 中,以大写字母开头的名称代表类名称。它将广泛提高代码的可读性。


推荐阅读