Topic: Loading image sequentially in chumby

Hi all, i left aside music streaming problem and try to load some of my image into my chumby sequentially (just like photoframe), but i got some problem, here is my example code (just example, in my real application i will use a loop to create loading function and a loop to automatically call setInterval function )

var loader:MovieClipLoader = new MovieClipLoader();


this.createEmptyMovieClip("myImageContainer",1);
myImageContainer._xscale = 40;
myImageContainer._yscale = 40;

function loading1(){
    loader.loadClip("/path/img1",myImageContainer);
    clearInterval(myinterval1);
}

function loading2(){
    loader.loadClip("/path/img2",myImageContainer);
    clearInterval(myinterval2);
}

loader.loadClip("/path/img0",myImageContainer);
myinterval1 = setInterval(loading1,10000);
myinterval2 = setInterval(loading2,15000);

The problem is, everytime i re-load the loader instance, the image did not change to the next one smoothly, the reason is that when loadClip method is called, it immediately remove the old one, but internet delay cause that the next one cound not be loaded right time. I need some advice to solve this problem, thank so much.

Re: Loading image sequentially in chumby

Yes this is correct - the first image is unloaded when the load in initiated, and the new image shows up when the new image is received.  That means that the image is not there in between.

The typical approach to this is to keep two (or more) image clips and ping-pong between them - show one while the other is downloading.

For an example of this, take a look at the Sample Webcam Widget source code.

Re: Loading image sequentially in chumby

This is really quick and dirty code, but should do the trick if placed on the main timeline.  Be sure to replace the base url and image array.

stop();
//setup image array
burl = "http://www.your-hosted-domain.com/"; //base url
imgs = new Array("img1.jpg", "img2.jpg", "img3.jpg");
//setup some vars
current_img = 0;
last_depth = 0;
//setup some functions
function startShow (slide_delay:Number):Void {
    //attach a image holder mc
    var imc:MovieClip = _root.createEmptyMovieClip("images", 10);
    //show timer
    var t:Number = setInterval(updateShow, slide_delay);
    //init first img
    updateShow();
}

function updateShow ():Void {
    //build path
    var image_path:String = burl + imgs[current_img];
    //get a depth
    last_depth = _root.images.getNextHighestDepth(); //depth
    //load it
    var image_holder:MovieClip = _root.images.createEmptyMovieClip("holder"+last_depth, last_depth);
    loadImage(image_path, image_holder);
    //update counter
    current_img++;
    //update counter loop
    if (current_img == imgs.length) {
        current_img = 0; //reset for loop
    }
}

function unloadLastImage () {
    var imc:MovieClip = _root.images;
    var last_image:MovieClip = imc["holder"+(last_depth-1)];
    
    if (last_image != undefined) {
        last_image.removeMovieClip();
    }
}

function loadImage (image_path, load_clip) {
    var ls:Object = new Object();
    ls.onLoadInit = function(target_mc:MovieClip) {
        _root.unloadLastImage();
    };
    
    var mcl:MovieClipLoader = new MovieClipLoader();
    mcl.addListener(ls);
    mcl.loadClip(image_path, load_clip);
}

//start show with 5 second delay between images
startShow(5000);

Hope that helps.

Cheers.

Re: Loading image sequentially in chumby

Thanks cbreeze, very useful code, i've started to learn flash for 2 weeks and your code really helped me.
@Duane: Thanks you, i've seen your sample source code, but there's one thing i confused is that why using setInterval function cause memory leak in chumby (this problem always occured when i streamed mp3 file in the chumby )

Re: Loading image sequentially in chumby

If you use setInterval() without corresponding clearInterval()s, then you'll get a memory leak.

interval objects are unusual in that they're not automatically garbage collected when references are lost since the only reference you get to the interval is an integer.

6 (edited by cbreeze 2009-11-04 10:33:10)

Re: Loading image sequentially in chumby

Granted, I would much rather wrap all this up in classes to manage the interval better (going for simplicity here tongue).  In this case though, best practices aside, because of the the fact that the interval is called only once, and runs indefinitely, is a memory leak still a concern?

@ritsumeikan - If you have any other questions feel free to ask.  Looking forward to your upcoming widgets. big_smile

Cheers.

Re: Loading image sequentially in chumby

No, it's not really an issue for widgets running on the chumby device, however, it can be a problem for virtual chumby, since an interval would be created each time the widget is launched.