我正在创建一个使用平铺和xml文件的游戏。我试图从外部服务器检索这些文件。当我在本地计算机上以调试模式运行程序时,它运行良好。但是当我上传到服务器时。我收到此错误
SecurityError: Error #2122: Security sandbox violation: LoaderInfo.content:
A policy file is required, but the checkPolicyFile flag was not set when this media was loaded.
以下是我的代码
private function init():void
{
game = new EngineApi();
gameObject.eApi = game;
gameObject.EG = this;
game.Tiling(true,800,640,80,80);
game.tileEg.offsY = 7000;
game.tileEg.ScrollLoop(true,0,1,true);//0,1
context.checkPolicyFile = true;
flash.system.Security.loadPolicyFile("http://mysite.com/images/tileSheet.png");
flash.system.Security.loadPolicyFile("http://mysite.com/images/world.xml");//map 0
flash.system.Security.loadPolicyFile("http://mysite.com/images/world1.xml");//map 1
flash.system.Security.loadPolicyFile("http://mysite.com/images/world7.xml");//world 7 and 8 are train track maps. map 2
flash.system.Security.loadPolicyFile("http://mysite.com/images/world8.xml");//map 3
flash.system.Security.loadPolicyFile("http://mysite.com/images/world9.xml");//Ending of 8 //map 4
flash.system.Security.loadPolicyFile("http://mysite.com/images/world10spaceInvader.xml");//Beginning of tracks being centered //map 5
flash.system.Security.loadPolicyFile("http://mysite.com/images/world11LoopBoss.xml");//Tracks loop (you fight boss) //map 6
flash.system.Security.loadPolicyFile("http://mysite.com/images/world12toDesert.xml");//map 7
flash.system.Security.loadPolicyFile("http://mysite.com/images/world2.xml");//map 8
flash.system.Security.loadPolicyFile("http://mysite.com/images/world3.xml");//map 9
flash.system.Security.loadPolicyFile("http://mysite.com/images/world13DesertDoubleTrain.xml");//map 10
flash.system.Security.loadPolicyFile("http://mysite.com/images/world14DoubleTrain.xml");//map 11
game.AddTileSheet("http://mysite.com/images/tileSheet.png");
game.tw = 80;
game.th = 80;
game.LoadTileSheets();
game.addEventListener("tileLoadComplete", prePareTiles);
//anything labeled with map (i.e. map 1) is the order of how the game see's it
game.addXml("http://mysite.com/images/world.xml");//map 0
game.addXml("http://mysite.com/images/world1.xml");//map 1
game.addXml("http://mysite.com/images/world7.xml");//world 7 and 8 are train track maps. map 2
game.addXml("http://mysite.com/images/world8.xml");//map 3
game.addXml("http://mysite.com/images/world9.xml");//Ending of 8 //map 4
game.addXml("http://mysite.com/images/world10spaceInvader.xml");//Beginning of tracks being centered //map 5
game.addXml("http://mysite.com/images/world11LoopBoss.xml");//Tracks loop (you fight boss) //map 6
game.addXml("http://mysite.com/images/world12toDesert.xml");//map 7
game.addXml("http://mysite.com/images/world2.xml");//map 8
game.addXml("http://mysite.com/images/world3.xml");//map 9
game.addXml("http://mysite.com/images/world13DesertDoubleTrain.xml");//map 10
game.addXml("http://mysite.com/images/world14DoubleTrain.xml");//map 11
game.loadXmlMaps();
clouds = new CloudManager(5);
clouds.minSpeed = 25;
clouds.maxSpeed = 35;
game.addGameChild(clouds);
}
以下是我的EngineApi()类
public function addXml(path:String):void
{
xmlDatas.push(path);
}
public function loadXmlMaps():void
{
var gLoader = new gameLoader();
gLoader.xmlToArray(xmlDatas.shift(),10,100);
gLoader.addEventListener("xmlComplete", FinishXmlMaps);
}
public function FinishXmlMaps(e:Event)
{
if(useTiles)
{
tileEg.mapHolder.push(gameLoader(e.currentTarget).tileArray);
}
if(xmlDatas.length > 0)
{
loadXmlMaps();
}else
{
dispatchEvent(new Event("XMLSCOMPLETE"));
}
}
这是我的gameLoader类中的代码
public function xmlToArray(s:String, cols:Number, rows:Number):void
{
this.cols = cols;
this.rows = rows;
//load in tile sheet image
var xmlLoader;
xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlLoadComplete);
xmlLoader.load(new URLRequest(s));
}
private function xmlLoadComplete(e:Event):void
{
var mazeData = new XML(e.currentTarget.data) as XML;
//loop through xml and add rows and columns to aTileMap array
for (var rowCtr=0;rowCtr<rows;rowCtr++) {
var tempArray:Array=new Array();
for (var colCtr=0;colCtr<cols;colCtr++) {
tempArray.push(mazeData.tilerow[rowCtr].tilecol[colCtr]);
}
tileArray.push(tempArray);
}
trace("xml done");
dispatchEvent(new Event("xmlComplete"));
}//END XMLLOAD
下面的代码几乎是数据检索和转换xml到数组的过程。一切正常。我只是得到一个错误,因为我猜它是一个跨域问题。我的问题是如何解决它。
答案 0 :(得分:3)
在我看来,你正在以错误的方式使用Security.loadPolicyFile函数。它用于读取特殊的XML文件,用于控制服务器如何允许Flash Player从中获取数据,而不是用于加载图像或其他文件,例如在loadPolicyFile(“http://mysite.com/images/tileSheet.png”)调用中。
以下是Security.loadPolicyFile的文档:
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/system/Security.html#loadPolicyFile()
以下是有关政策文件的信息:
关于如何解决跨域问题,最简单的方法是将crossdomain.xml文件放在服务器的根目录下。在其中,您可以指定允许从服务器获取数据的域。您可以使用*来允许任何域名。以下是允许任何域的crossdomain.xml的示例:
http://api.flickr.com/crossdomain.xml
Flash Player会在必要时自动查找该文件,并且您的ActionScript代码不需要自行采取任何进一步操作。
使用Security.loadPolicyFile()主要用于需要更多受控和/或细粒度跨域策略的情况。