Topic: Timed loop

Hello,
I was hoping someone might help me out. I know little to nothing about flash programming, but I thought I’d try my hand at making a web cam local widget.
I can display the web cam, and I can set the profile to reload it, but what I’d really like to do is to have the widget be persistent and refresh the image itself.
Can somebody please explain to me how to do a timed loop in a widget...say reload a url every 4 seconds and in the profile make it persistent (forever)?
I haven’t had any luck…

Thank you

Re: Timed loop

This is done using "intervals", which are repeating timers.  You set up an interval to call some function periodically.

There are two relevant functions - setInterval() and clearInterval() that are used to create and destroy them.

Re: Timed loop

Hello Duane,
Thanks for the help. I got the setInterval(), clearInterval to work…but it ended up giving me fits.
For some reason, my app would start out reloading fine at every interval and then it would become erratic and then hang. I put a trace on it and found that with each interval, the trace returned value was doubling and then doubling again and so forth exponentially and then crash.
I played around with this for a while, but finally gave up after discovering that setTimeout seemed to work as well but without this issue.
My first version appended the time onto the URL to avoid caching, but it seems that it takes longer to execute then just appending a random number.

I do have a couple of questions though…
First, running my widget locally, what it the profile setting to have it run “forever” ?

And second…do you know of any way to avoid the “dead screen” in between interval refreshes?
I’m not sure how to hold the previous image until the new one is loaded.

I doubt if this is the best flash script…but it’s my first attempt.

Thanks for your help



class CamView.Main {
  private var Clip:MovieClip;
  function Main(mc:MovieClip) {
    setTimeout(main, 4000);
    clearTimeout()
        var RandomNum = Math.random()*1000;
        var RoundNoCache = Math.round(RandomNum);
        var url = "http://user:password@url:port/cgi-bin/video2.jpg?size=2&quality=3" + " " + RoundNoCache
        var ThisLoader:MovieClipLoader = new MovieClipLoader();
    Clip = mc.createEmptyMovieClip("Image", mc.getNextHighestDepth());
    ThisLoader.loadClip( url, "Image");
  }
static function main(mc:MovieClip) {
      var app = new Main(mc);
}
}

Re: Timed loop

It sounds like you had an interval leak - setInterval creates a new repeating timer every time it's called, and returns an integer that must be handed off to clearInterval when you want it to go away.

The way many of our webcams work is that they use *two* MovieClips - you load into one while displaying the other and ping-pong between them.

5 (edited by gmcbay 2010-11-30 15:54:14)

Re: Timed loop

Here's a complete double-buffered webcam in haxe:

package ;

import flash.Lib;
import flash.MovieClip;
import haxe.Timer;

class WebCam extends flash.MovieClipLoader
{
    private static var LOAD_DELAY:Int = 4000;    
    private static var webCam:WebCam;
    private var imageClips:Array<MovieClip>;
    private var currentIndex:Int;
    
    // ------------------------------------------------------------------------
    static function main() 
    {
        webCam = new WebCam();
    }

    // ------------------------------------------------------------------------
    function new():Void 
    {
        super();
        
        currentIndex = 0;

        imageClips = [Lib._root.createEmptyMovieClip("clip_0", 0), 
                      Lib._root.createEmptyMovieClip("clip_1", 1)];

        onLoadNext();
    }
    
    // ------------------------------------------------------------------------
    private function onLoadNext():Void 
    {
        imageClips[currentIndex]._visible = false;
        
        loadClip("http://os4.prod.camzone.com/still?cam=0&frmcnt=0&" + 
                      Math.floor(Math.random() * 65536), imageClips[currentIndex]);
        
        currentIndex++;
        
        if (currentIndex >= imageClips.length)
        {
            currentIndex = 0;
        }
    }
    
    // ------------------------------------------------------------------------
    public override function onLoadInit(target:MovieClip):Void 
    {
        target.swapDepths(target._parent.getNextHighestDepth());
        target._visible = true;        
        Timer.delay(onLoadNext, LOAD_DELAY);
    }
}

You could, of course, do the same thing in ActionScript with slightly different code.



Another option for timing is to increment a frame number variable in an onEnterFrame handler and then perform an action when the frame number reaches a certain treshold, eg, in AS, inside a class which extends MovieClip:


var frames:Number = 0;

function onEnterFrame():Void 
{
    frames++;

    // If movie is 12 fps, 48 frames is roughly 4 seconds
    if(frames == 48)
    {
        frames = 0; // reset if you want a timed interval
        doSomething();
    }
}

This form of timing is less precise than using the ActionScript timers, but for non-animation tasks it is generally good enough and it saves you from the perils of fully managing your own interval handles.

Re: Timed loop

Thanks Duane...I guess I wasn't handing of the integer.
I'm more used to C and visual basic...haven't figured out how to hold my head to make sense out of this flash.

Thanks as well gmcbay; I'll see what I can do with your examples.

Re: Timed loop

Thanks gmcbay...I built your code with haxe and it works great.
I'm working on writing my own in ActionScript now.

Thanks again