[心得] 一些常用的AS3代码
- //确保外部接口可用
- 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) {
- }
- -------------------------------------------------------
- //loader时安全沙箱,先加载策略文件
- var _loader:Loader = new Loader();
- _loader.load(new URLRequest(""),new LoaderContext(true));
- -------------------------------------------------------
- //通过注册外部容器,用事件流的方式获取内部DisplayObject的引用(这样就不用注册N个Listener)
- 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);
- }
复制代码