LoaderMax图像调整大小

时间:2011-12-15 16:25:42

标签: actionscript-3

我正在尝试使用LoaderMax加载xml图像并使用LiquidArea调整它们的大小。当我尝试使用下一个或缩略图加载调整大小的其他图像时,我得到一个我无法理解的奇怪错误。我无法得到帮助,所以我来到这里。错误消息和代码可以在下面看到。

RangeError: Error #2006: The supplied index is out of bounds.
    at flash.display::DisplayObjectContainer/addChildAt()
    at com.greensock.layout::AutoFitArea/set preview()
    at com.greensock.layout.core::LiquidData/refreshLevel()
    at com.greensock.layout::LiquidStage/refreshLevels()
    at com.greensock.layout.core::LiquidData$/addCacheData()
    at com.greensock.layout::LiquidArea/pinCorners()
    at com.greensock.layout::LiquidArea/autoPinCorners()
    at com.greensock.layout::LiquidArea()
    at Main/loadImage()
    at Function/<anonymous>()
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at com.greensock.core::TweenCore/complete()
    at com.greensock::TweenMax/complete()
    at com.greensock::TweenMax/renderTime()
    at com.greensock.core::SimpleTimeline/renderTime()
    at com.greensock::TweenLite$/updateAll()

package 
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.DisplayObject;

    import com.greensock.TweenMax;
    import com.greensock.layout.*;
    import com.greensock.events.LoaderEvent;
    import com.greensock.loading.XMLLoader;
    import com.greensock.loading.LoaderMax;
    import com.greensock.loading.ImageLoader;
    import com.greensock.loading.data.ImageLoaderVars;
    import com.greensock.loading.display.ContentDisplay;


    public class Main extends MovieClip
    {
        private static const THUMB_WIDTH:Number = 100;
        private static const THUMB_HEIGHT:Number = 64;
        private static const IMAGE_WIDTH:Number = 550;
        private static const IMAGE_HEIGHT:Number = 355;

        private var ls:LiquidStage;
        private var la:LiquidArea;

        private var xImgList:XMLList;
        public var arrowRight:MovieClip;
        public var arrowLeft:MovieClip;
        private var currentImage:String;
        private var image:ImageLoader;
        private var index:Number = 0;

        public function Main()
        {
            // load in xml
            var xPhotography:XMLLoader = new XMLLoader("assets/data.xml");
            xPhotography.addEventListener( LoaderEvent.COMPLETE, xmlLoaded );
            xPhotography.load();

            arrowRight.alpha = arrowLeft.alpha = 0;
            arrowRight.addEventListener(MouseEvent.ROLL_OVER, rollOverArrowHandler, false, 0, true);
            arrowRight.addEventListener(MouseEvent.ROLL_OUT, rollOutArrowHandler, false, 0, true);
            arrowRight.addEventListener( MouseEvent.CLICK, onArrowClick );
            arrowLeft.addEventListener(MouseEvent.ROLL_OVER, rollOverArrowHandler, false, 0, true);
            arrowLeft.addEventListener(MouseEvent.ROLL_OUT, rollOutArrowHandler, false, 0, true);
            arrowLeft.addEventListener( MouseEvent.CLICK, onArrowClick );
        }

        private function xmlLoaded( e:LoaderEvent ):void
        {
            var xData:XML = e.target.content;// get copy of xml
            xImgList = new XMLList(xData.image);// grabbing xml image nodes

            loadImage( 0 );
        }

        // load in the image
        private function loadImage( index: Number ):void
        {
            ls = new LiquidStage(this.stage,550,419);
            la = new LiquidArea(imgContainer, 0, 0, 550, 419);

            var file:String = xImgList[index]. @ url;

            var image:ImageLoader = new ImageLoader("assets/images/" + file, new ImageLoaderVars()
            .container( imgContainer )
            .x(0)
            .y(0)
            .alpha( 0 )
            .width( IMAGE_WIDTH )
            .height( IMAGE_HEIGHT )
            .onComplete(completeHandler)
            .scaleMode( "proportionalOutside" )
             );

            image.load();
            la.attach(image.content, {scaleMode:ScaleMode.PROPORTIONAL_OUTSIDE, crop:true});
        }

        private function completeHandler(event:LoaderEvent):void
        {
            TweenMax.to(event.target.content, 1.5, {alpha:1});
        }

        private function rollOverArrowHandler(e:MouseEvent):void
        {
            TweenMax.to(e.currentTarget, 0.5, {alpha:1});
        }

        private function rollOutArrowHandler(e:MouseEvent):void
        {
            TweenMax.to(e.currentTarget, 0.5, {alpha:0});
        }

        private function onArrowClick( e:MouseEvent ):void
        {
            switch (e.target)
            {
                case arrowRight :
                    index++;
                    break;
                case arrowLeft :
                    index--;
                    break;
            }

            if (index == xImgList.length())
            {
                index = 0;
            } else if (index < 0)
            {
                index = xImgList.length() - 1;
            }

            checkOldImage( index );// needed to help loading times
        }

        private function checkOldImage( index:Number ):void
        {

            var oldClip:DisplayObject = imgContainer.getChildAt( 0 );
            var nextIndex = index;
            TweenMax.to( oldClip, .5, { autoAlpha: 0, onComplete: completeFadeHandler, onCompleteParams:[oldClip] } );

            function completeFadeHandler(child:DisplayObject):void {
                if (child.parent) {
                    child.parent.removeChild(child);
                }
                loadImage(nextIndex);
            }
        }
    }

}

0 个答案:

没有答案