Web Analytics Made Easy -
StatCounter Storing objects for later call - CodingForum

Announcement

Collapse
No announcement yet.

Storing objects for later call

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Storing objects for later call

    Just wandered if anyone has any ideas about storing objects for later calling by the window.setTimeout object. I want some object to be called over and over by the setTimeout function, and then it is paused, where the called object and its associated timer are both stored. When it's unpaused, they are released and called by the window.setTimeout to resume.

    something like:
    var game = false;

    function FirstGuy () {
    if (game) {
    // do something
    window.setTimeout("FirstGuy()",200);
    } else {
    pause.store (FirstGuy,200);
    }
    }

    function Pause () {
    this.calls = new Array ();
    this.timers = new Array ();
    this.num = 0;
    this.store = pauseStore;
    this.release = pauseRelease;
    }

    var pause = new Pause ();

    function pauseStore (called,timer) {
    this.calls[this.num] = called;
    this.timers[this.num] = timer;
    this.num++;
    }

    function pauseRelease () {
    for (var i=0; i<this.num; i++) {
    window.setTimeout(this.calls[i]+"()",this.timers[i]);
    }
    }

    As you can see, pause is a stack which stores the recursive stuff when it is called,and then is supposed to release them to resume recursion when it is deactivated.

    The problem is that I'm not sure how to store and release. I've tried these:
    pause.store (FirstGuy,200);
    pause.store ("FirstGuy",200);

    window.setTimeout(pause.calls[i]+"()",this.timers[i]);
    window.setTimeout("pause.calls[i]()",this.timers[i]);

    So far, no cigar. Any suggestions would be greatly appreciated. Also, if anyone knows how to put a timeout on hold and then resume it, that's good also (still need the pause thing, though).

    Thanks in advance.
    If at first you don't succeed, spend more time online researching javascript!
    Beck

  • #2
    no quite there, though

    Thanks, Dave, but the problem is a little more complicated than that. What you're saying will work, but imagine having well over a dozen timeouts taking place at once (making a video game, basically) and when the player pauses the game, I want to store these timeouts until the game resumes. I never know what bullets are on the page, what enemies are in action, etc, and all of them need to hault when paused and pick up where left off when unpaused.

    Technically I could hardcode all sorts of checks, etc, but then I'd even have to modify like crazy to change even one object. The more generic stack storage idea doesn't require any modifications.

    So if anyone's got anything for me to think about, fire away!

    Thanks
    If at first you don't succeed, spend more time online researching javascript!
    Beck

    Comment

    Working...
    X