s:分组并加载图像

时间:2012-03-28 10:20:01

标签: actionscript-3 flash flex actionscript

看看下面的课程

public class CanvasHolder extends SpriteVisualElement {
    ....

    public function draw(filename: String):void {
        var loader:Loader = new Loader();
        loader.addEventListener(Event.COMPLETE, completeHandler);
        var request:URLRequest = new URLRequest(.....);
        loader.load(request);
        addChild(loader);
    }
}

当它在mxml中使用时如下

<ctw:CanvasHolder id="holder"/>

已加载图片。但是当我尝试将其插入滚动条

<s:Group id="canvasGroup" width="400" height="100%" clipAndEnableScrolling="true">
    <ctw:CanvasHolder id="holder"/>
</s:Group>

图像未显示。有人能告诉我问题在哪里吗?

1 个答案:

答案 0 :(得分:1)

    package
{
    import flash.display.Bitmap;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.net.URLRequest;

    import mx.binding.utils.BindingUtils;

    import spark.core.SpriteVisualElement;

    public class CanvasLoader extends SpriteVisualElement
    {
        public function CanvasLoader()
        {
            super();
        }


        public function draw(url: String):void {
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
            var request:URLRequest = new URLRequest(url);
            loader.load(request);
        }


        private function completeHandler(event:Event):void
        {
            var bitmap:Bitmap = event.target.content as Bitmap;
            addChild(bitmap);
        }

        private function onError(event:IOErrorEvent):void 
        {
            trace(event.text);
        }

    }
}

并在主mxml中添加此

holder.draw("http://yourdomain.com/image.jpg");