Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Flow control....issue

    edit::I guess I tried everything else before thinking of a toggle variable....seems to be working now

    I have an if clause that triggers died();

    died() {
    monster.y=-100; //move element off canvas
    setTimeout("respawn()", 2500);
    }

    respawn() {
    //move monster to random location inside canvas
    monster.x = 32 + (Math.random() * (canvas.width - 64));
    monster.y = 32 + (Math.random() * (canvas.height - 64));
    }

    if(collision condition true) {
    died();
    }
    This works but it sends setTimeout("respawn()",2500); about a dozen times, so 2.5 seconds later when it respawns it runs a dozen times and the monster flickers around the canvas before finding it's final destination.

    I've tried both a WHILE loop which WORKED initially but I couldn't get it to reset properly, it worked the first time it ran and that was it....

    I tried an if() with a global variable called count so it would only be called if count was under 2 but it didn't work right....

    I tried a for loop as well. But my main() function is running every 10ms, and it just triggers things so fast that it triggers things multiple times when I only want it to fire once....

    pseudo of what I'm trying to do.

    If(collision) {died();}
    died(){ move char off viewable screen, delay 2.5 seconds before moving back onto screen at random location}

    Seems so simple......but when there's a collision, it sends died() every pixel as the 2 objects move across each other.....where I need it to only trigger died after the first collision is detected and stop checking for 2.5 seconds or the duration of the wait before showing back up on the screen..

    so maybe I need a new function to call died which can only be called once....
    Last edited by nuBee; 12-12-2011 at 03:08 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Ready to kick yourself?
    Code:
    var waiting = null;
    function died() {
        if ( waiting == null )
        {
            waiting = setTimeout(respawn, 2500); // no quotes around respawn and no parens
            monster.y=-100; //move element off canvas
        }
    }
    
    function respawn() {
        waiting = null; // allow the die to set the timeout again
    
        //move monster to random location inside canvas
        monster.x = 32 + (Math.random() * (canvas.width - 64));
        monster.y = 32 + (Math.random() * (canvas.height - 64));
    }
    
    if(collision condition true) {
        died();
    }
    Just don't allow a setTimeout if one is already running!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •