swf跨域互相加载

 理论知识

 条件:a.swf ,b.swf,A服务器,B服务器,

要求:a.swf放在A服务器,b.swf放在B服务器,a.swf调用b.swf

关键因素: Security.allowDomain()类对象,那么至少在3.0中是这样的,2.0应该前面还有system类,具体可以查看as2.0,及as3.0类帮助文档

  

 code:

a.swf

package customize 
{
import flash.display.*;
import flash.events.Event;
import flash.system.Security; 
import flash.system.System;
import flash.net.*;

/**
* ...
* @author slowsay
*/
public class Main extends Sprite
{

public function Main() 
{
if (stage)init();
else { this.addEventListener(Event.ADDED_TO_STAGE, init) };

}

private function init(e:Event=null):void 
{ 
this.removeEventListener(Event.ADDED_TO_STAGE, init);

//init
Security.allowDomain("*");

var path = "http://www.juzui.com/b.swf";
var swfload:Loader = new Loader;
swfload.load(new URLRequest(path));
addChild(swfload);
this.addEventListener(Event.ENTER_FRAME, update);
} 
private var _box:Sprite = new Sprite();
private function update(e:Event):void 
{

addChild(_box);
for (var i:int = 0; i < 100; i++) 
{
var bitmap:Bitmap = new Bitmap(new BitmapData(2, 2, false, 0xffffff * Math.random()));
var _s:Sprite = new Sprite;
_s.name = "s" + i;
_s.addChild(bitmap);
_box.addChild(_s);
}
}
}

}

 b.swf

 

package customize 
{	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.*;
	import flash.events.Event;
	import flash.system.Security;
	import flash.system.System;
	import flash.net.*;
	
	/**
	 * ...
	 * @author slowsay
	 */
	public class Mainb extends Sprite
	{
		private var _box:Sprite=new Sprite;
		public function Mainb() 
		{
			
			if (stage)init();
			else { this.addEventListener(Event.ADDED_TO_STAGE, init) };
			
		}
		private function init(e:Event=null):void 
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
			//init
			Security.allowDomain("*");
			//addChild
			addChild(_box);
			var bitmap:Bitmap = new Bitmap(new BitmapData(100, 100, false, 0xff0000));
			_box.addChild(bitmap);
			
			bitmap.x = 0-int(bitmap.width / 2);
			bitmap.y = 0-int(bitmap.height / 2);
			_box.x = int(stage.stageWidth / 2);
			_box.y = int(stage.stageHeight / 2);
		}
	}

}

 

posted @ 2012-03-12 11:05  slowsay  阅读(377)  评论(0)    收藏  举报