views:

67

answers:

4

I'm having issue while loading thumbnail, I set an interval of time for each thumb to load on stage. Some of them can't load sequential in order, they are missing, are lagging...

Actually, I set a preloader to the file size but since I load dynamically from XML, file size is smaller.

Does anyone know a solution to solve that issue? It's really bug me though...

I load with XML file and don't have any issue with parsing process.

Sometimes all thumbnails can be loaded on Stage, if bandwidth a bit slow it's lagged mean first thumb is loading then it's suddenly skip the second thumbnail, it loads third thumbnail instead and so on...

Sometimes it can load the whole thumbnails... it's weird

Here, I set a timer to load each thumbnail

protected static const INTERVAL_TIME:uint       = 1000;
initLoadTime = new Timer(INTERVAL_TIME);
initLoadTime.addEventListener(TimerEvent.TIMER, handleLoadedEachThumbnail, false, 0, true);
initLoadTime.start();

protected function handleLoadedEachThumbnail(event:TimerEvent):void
{
var container:MovieClip = new MovieClip();

if (currentIndex < tabPhoto.length) {
 thumbnailPositionMC = positionEachThumbnail();
 container.addChild(thumbnailPositionMC);
 addChild(container);


 currentIndex++;
}

if (currentIndex == tabPhoto.length) {
 currentIndex = 0;
 initLoadTime.removeEventListener(TimerEvent.TIMER, handleLoadedEachThumbnail);
 initLoadTime.stop();
}

}

+1  A: 

I think it would be better to create a loop and use the loader's Event.COMPLETE to know when to load the next thumbnail instead of using an interval. That way you will avoid conflicts.

var imageLoader:Loader;
var currentImageLoading:uint;
var thumbsToLoad:Array; //array of strings of paths to the thumbnail

function loadThumbs(thumbs:Array):void
{
    currentImageLoading = 0;
    thumbsToLoad = thumbs;
    loadNextThumb();

function loadNextThumb():void
{
    imageLoader = new Loader();
    var imageUrl:String = thumbsToLoad[currentThumbLoading];
    var imageRequest:URLRequest = new URLRequest( imageUrl );
    imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadThumbComplete);
    imageLoader.load( imageRequest );
}

function loadThumbComplete(e:Event):void
{
    // do stuff with loaded thumb here
    var thumb:Sprite = new Sprite();
    thumb.addChild(imageLoader.content);
    trace(thumb.width);
    currentThumbLoading++;
    if (currentThumbLoading < thumbsToLoad.length) {
        loadNextThumb();
    } else {
        // loading cycle is complete
    }
}

Edited after comments

I fixed the loadThumbComplete to take the event parameter. To get the width of the loaded thumb I prefer to take the content from the imageLoader and put it in a Sprite.

knuckfubuck
haha was writing the same answer :) @Qpixo You could also use the loading classes from greensock.com , it's a bit of an overkill for the problem here but they're packed with advanced loading features
PatrickS
Yes I use URLLoader to load from XML file.// Filename, call it in mainloader = new URLLoader();loader.load(new URLRequest(filename));addFileListener(loader, callBack);
Qpixo
@Qpixo That's not the same as what I put in my answer.
knuckfubuck
@Knuckfubuck: why your function loadThumbComplete() doesn't have parameter (event:Event)? and currentThumbLoading++ is supposed to be in the if (currentThumbLoading < thumbsToLoad.length) ??
Qpixo
@Knuckfubuck: When using your method above:Also I was trying to get the width of each thumb and got an error.imageLoader.content.width => Error #1009: Cannot access a property or method of a null object reference.with imageLoader.width, it gives me 0How can I get each thumbnail width???
Qpixo
@Qpixo see my edited answer above. currentThumbLoading++ is fine where it is. It should increment before the cycle checks if it should continue or not.
knuckfubuck
@Qpixo if my answer helped you solve it, consider accepting it.
knuckfubuck
A: 

Here are my code, I'm not sure where to use Event.Complete through my code though...

protected function handleLoadedPhotos(event:Event):void {
if ((event.target as URLLoader) != null) {
    xmlItems = new XML(event.target.data);

    var childLength:uint = xmlItems.children().length();

    for (var i:uint=0;i < childLength;i++) {
        var tabPhotoInfo:ThumbInfo = new ThumbInfo();

        // Set the ID 
        tabPhotoInfo.id = xmlItems.children()[i].@id;

        // Set url image 
        for (var j:uint=0;j < xmlItems.children()[i].children().length();j++) {
            tabPhotoInfo.url = xmlItems.children()[i].children()[j].toString();
        }

        tabPhoto.push(tabPhotoInfo);
    }               
}

setLoadBitmap();
dispose(); // Remove useless ressources after loaded                    

}

protected function setLoadBitmap():void {
var tabPhotosLength:uint = tabPhoto.length;

for (var index:uint=0;index < tabPhotosLength;index++) {
    // Insert each image name to bitmap to process the resize aspect ratio
    tabThumbnail.push(new Thumbnail(tabPhoto[index].url));
}

// Create next/previous buttons
previousButtonContainer = createPreviousButtons();
nextButtonContainer     = createNextButtons();

// Add events to next and previous buttons
addEventListeners(previousButtonContainer, handleMoveThumbnail, handleMouseOverAndOut);
addEventListeners(nextButtonContainer, handleMoveThumbnail, handleMouseOverAndOut);

// Set time interval to load each thumbnail on stage
initTime(); // <<== call this method which is those codes above

}

protected function dispose():void {
loadXML.loader.removeEventListener(Event.COMPLETE, handleLoadedPhotos);
loadXML.loader.removeEventListener(ProgressEvent.PROGRESS, handleLoadedPhotos);
loadXML.loader.removeEventListener(IOErrorEvent.IO_ERROR, handleLoadedPhotos);
loadXML.loader = null;

}

Qpixo
A: 

I got that annoying error message: RangeError: Error #1125: The index 4 is out of range 4.

In my XML file: it contains 4 image url files

This time all pictures can load on Stage and well positionned like I want but there's an Range error compiling...

Does anyone know How can I solve it?

I have modified my code to:

protected function loadNextThumb():void
    {
        imageLoader = new Loader();
        var imageURL:String = tabPhoto[currentIndex].url;

        imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleCompletedLoadedThumb, false, 0, true);
        imageLoader.load(new URLRequest(imageURL));
    }

protected function handleCompletedLoadedThumb(event:Event):void
    {
        //var thumb:Sprite = new Sprite();
        //thumb.addChild(imageLoader.content);

        //tabThumbWidthIncrement[currentIndex] = imageLoader.x;
        //currentIndex++;

        if (currentIndex <= tabPhoto.length) {
            trace(currentIndex); // this gives 0,1,2,3

            imageLoader.alpha = 0;

            // Display and position each thumb (imageLoader) on Stage
            imageLoader.x = totalWidth;

            if (event.currentTarget.width >= WIDTH_LANDSCAPE) {
                totalWidth += (event.currentTarget.width+SPACE);
                trace('>>> Landscape'+" >>ImageWidth: "+event.currentTarget.width);
            } else {
                totalWidth += (event.currentTarget.width+SPACE);
                trace('*** Portrait'+" >>ImageWidth: "+event.currentTarget.width);
            }

            addChild(imageLoader);
            thumbnailContainer.addChild(imageLoader);
            addChild(thumbnailContainer);

            tabThumbWidthIncrement[currentIndex] = imageLoader.x;
            currentIndex++;

            // Animate fading
            TweenLite.to(imageLoader, TIME, { delay:DELAY, alpha:ALPHA_ON, ease:Linear.easeOut });

            loadNextThumb();
        } else {
            // When completed loaded then... 
            imageLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleCompletedLoadedThumb);
            imageLoader = null;
            currentIndex = 0;
        }
    }
Qpixo
Does anyone know how to fix that error?I have declared tabPhoto as Vectorex: var tabPhoto:Vector.<ThumbInfo>;ThumbInfo is a custom class which contains id, name, url and getters/setters
Qpixo
@Qpixo You're getting that error because you are incrementing inside the if statement where you check if (currentIndex <= tabPhoto.length). You don't want to call loadNextThumb if currentIndex is equal to the array length as that would try to load an item that doesn't exist. Follow my original answer example and keep the incrementing and the currentIndex check in the same places and it'll work.
knuckfubuck
@Knuckfubuck When I put currentIndex++ before if (currentIndex < tabPhoto.length) statement like your, my last picture doesn't display on Stage and I got an error while clicking right button index 4 is out of range 4.
Qpixo
I don't get it.when currentIndex reaching to its max, I put else statement { currentIndex = 0; } already
Qpixo
@Qpixo currentIndex starts at 0. if (currentIndex<tabPhoto.length) should stop on 3 if the length is 4.
knuckfubuck
@Knuckfubuck: I understand if in total: 4 items, index starts from 0 and end to 3. What I don't understand is: the last item doesn't show up on Stage but only 0,1 and 2.
Qpixo
@Knuckfubuck:Ok I found the problem. It's my display on Stage. I have to call both in if statement and else statement. It's weird!!
Qpixo
@Qpixo Cool, glad you got it working.
knuckfubuck
@Knuckfubuck: Thanks a lot for your help and your patience. Sometimes it's just a ridiculos problem which could get a bit frustrated. But once found it, I'm so happy to learn something new everyday... :)
Qpixo
@Qpixo No problem. Quick tip: If you accept the answer by clicking the check mark next to it, your acceptance rate will go up. People will be more likely to answer your questions when that number is higher. Right now it's at 10%.
knuckfubuck
A: 

I have declared tabPhoto and tabThumbWidthIncrement as Vector

var tabPhoto:Vector.<ThumbInfo> = new Vector.<ThumbInfo>;
var tabThumbWidthIncrement:Vector<uint> = new Vector.<uint>;
Qpixo