ActionScript 多图加载 按图顺序索引

Tag:ActionScript 加载图片 位置 顺序 多张图

1,写一个自己的EVENT类:DataEvent

 

package util
{
    import flash.events.Event;

    public class DataEvent extends Event
    {
        public var data:String;
        public var content:Object;
        public static const COMPLETE:String="ObjectLoadOver";
        public function DataEvent(type:String=DataEvent.COMPLETE, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            super(type, bubbles, cancelable);
        }
    }
}

2,写自己的一个Loader类:ObjectLoader

package util{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    public class ObjectLoader extends Sprite {
        private var data:String;
        private var ld:Loader;
        public var loaded:Function;
        public function ObjectLoader(url:String,addedData:String,loadExec:Function) {
            ld=new Loader;
            ld.contentLoaderInfo.addEventListener(Event.COMPLETE,onObjectLoaded);
            ld.load(new URLRequest(url));
            loaded=loadExec;
            data=addedData;
            addEventListener(DataEvent.COMPLETE,loaded);
        }
        private function onObjectLoaded(e:Event):void {
            var event:DataEvent=new DataEvent();
            event.data=data;
            event.content=e.target.content;
            trace(e.target.content);
            //event.target.content=e.target.content;
            if (loaded!=null) {
                dispatchEvent(event);
            } else {
                trace("请给ObjectLoader的loaded方法赋值");
            }
        }

    }
}

3,调用自己的Loader

public function Abcde()
        {
            cards=new Array(13);
            for(var i:uint=0;i<13;i++)
            {
                var url:String="images/poke/C/"+(i+3)+".jpg";
                var ol:ObjectLoader=new ObjectLoader(url,(i+3).toString(),LoadDone);
            }
        }
        public function LoadDone(e:DataEvent):void
        {
            trace(e.data);
            var bitmap:Bitmap=e.content as Bitmap;
            bitmap.y=top;
            bitmap.x=parseInt(e.data)*20+left;
            cards[parseInt(e.data)-3]=bitmap;
            total++;
            if(total==13)
            {
                for(var i:uint=0;i<13;i++)
                    addChild(cards[i]);
                }
                   }

实现了异步加载图片,加载完成后获取图片指针

posted on 2009-09-26 00:49  yangyh  阅读(1036)  评论(2编辑  收藏  举报