一些常用的AS3代码[转]

确保外部js接口可用:

if (ExternalInterface.available) {

    ExternalInterface.xxx

}

---------------------------------------------------------

确保BitmapData初始化成功:

try {

    //init Bitmapdata

} catch (e:ArgumentError) {

}

--------------------------------------------------------

网络加载出错分支处理:

var _urlLoader:URLLoader = new URLLoader();

_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);

try {

    _urlLoader.load(new URLRequest(""));

} catch (e:Error) {

}

-------------------------------------------------------

安全沙箱加载策略文件:

var _loader:Loader = new Loader();

_loader.load(new URLRequest(""),new LoaderContext(true));

-------------------------------------------------------

注册外部容器,通过事件冒泡获得内部引用:

var _container:DisplayObjectContainer;

var obj1:DisplayObject;......and many

addChild(obj1);.....and many

_container.addEventListener(MouseEvent.CLICK,onClickHandler,true);

function onClickHandler(e:MouseEvent):void {

    var obj:DisplayObject = e.target as DisplayObject; //obj...

}

-------------------------------------------------------

事件监听自删除:

obj.addEventListener(MouseEvent.CLICK,onClickHandler);

function onClickHandler(e:MouseEvent):void {

    e.currentTarget.removeEventListener(e.type, arguments.callee);

}

--------------------------------------------------------

Math函数替换:

Math.abs; ---> num > 0 ? num : -num;

Math.floor; ---> int(num) or num>>0;

Math.ceil; ---> int(num) + 1 or num>>0 + 1;

Math.max: ---> num = a > b ? a : b;

Math.min: ---> num = a > b ? b : a;

Math.round: ---> int(num + 0.5) or (num + 0.5)>>0;

 

1,获取flashvars:

var paramObj:Object = LoaderInfo(root.loaderInfo).parameters;
if (paramObj["xxxxx"]) xxxxx= encodeURIComponent(paramObj["xxxxx"]);

2,Security.allowDomain的作用:

允许某域名下js或swf或anything else,访问该swf内部的函数和方法。

3,allowScriptAccess

有always,sameDomain,never三个值~定义了swf是否能够访问页面的javascript代码,决定了ExternalInterface.call之类的方法是否能访问成功。

 

原帖地址:

http://hi.baidu.com/dannyfd/blog/item/fa0c791a0664e84b42a9ad69.html

http://hi.baidu.com/dannyfd/blog/item/065ab98eff303800b31bbad3.html

posted @ 2011-01-17 15:47  北冥  阅读(467)  评论(0编辑  收藏  举报