Results 1 to 5 of 5

Thread: Single Execution setInterval

  1. #1
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Single Execution setInterval

    Hi,

    Is there anyway in Flash to run a command only once after a certain amount of time... precisely the way we use setTimeout in JavaScript?

    Currently I have declared a variable and done a clearInterval the first time when the function is fired. But my problem is that there are almost 7-8 such timeouts that I have to do in regular intervals.

    So... i wanted to know if there is an efficient method to handle this in ActionScript...

    Regards,
    Venkat

  2. #2
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    There are several ways to do so, you mention after a certain amount of time, though.. are you using a timer of any sort, or just going by Frame rate?
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  3. #3
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default setInterval

    I want to use a timer. Nothing frame-based. Something like a function should be called after 500 milliseconds and it should be executed only once.

  4. #4
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Something like this should be tweakable for you then. I haven't tested it's functionality, but it looks correct to me

    I do have Flash open if you need help, but I am building Tilesets at the moment.. bitmap data can be so boring....

    Code:
    function getRand(maxNum, minNum) {
    	return Math.floor(Math.random() * (maxNum - minNum)) + minNum;
    }
    
    var startTime = getTimer();
    var limitTime = getRand(3, 1) * 1000;
    var runTime = true;
    _root.onEnterFrame = function() {
    	if (runTime) {
    		if (getTimer() - startTime > limitTime) {
    			//do action
    			trace("time's up!");
    			runTime = false;
    		}
    	}
    };
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  5. #5
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Alternatley some more options from Morgan:

    Yep, this can be done in scripting, Zoranvedek is on the right lines.

    There are two easy ways I can think of. To delay by a number of frames:

    When you want the delay to start, set a variable to 0. Have a script running once every frame, to increment the variable by 1. Have another part of the script running so that when the variable exceeds 20, to then go on and do something else.
    If you're using 20 frames per second, this would pause the movie for one second.

    ie.
    In the starting script:
    status="BeforeWait"

    In a script running once per frame in the movie:
    if (status=="BeforeWait")
    {
    // All the other stuff you want to do before the wait
    }

    if (status=="StartWait")
    {
    counter=0
    status="NowWaiting"
    }

    if (status=="NowWaiting")
    {
    counter+=1
    if (counter>20)
    {
    status="FinishedWaiting"
    }
    }

    if (status=="FinishedWaiting")
    {
    // All the stuff you want to do after the wait is completed
    }

    On a button when you want the delay to start, add an 'On Button Up' script of:
    status="StartWait"

    For longer delays, change the line : if (counter>20) to be a bigger number. If your movie doesn't run at 20 frames per second, change this accordingly.


    If you need it to be a strict time delay, then the same principle can be used with the time variables, or probably the better one would be the timer() function, which just returns a number rather than having to mess about with hours, minutes, seconds, days, etc.

    ie.
    In the starting script:
    status="BeforeWait"

    In a script running once per frame in the movie:
    if (status=="BeforeWait")
    {
    // All the other stuff you want to do before the wait
    }

    if (status=="StartWait")
    {
    counter=getTimer()
    status="NowWaiting"
    }

    if (status=="NowWaiting")
    {
    if (getTimer()>(counter+1000))
    {
    status="FinishedWaiting"
    }
    }

    if (status=="FinishedWaiting")
    {
    // All the stuff you want to do after the wait is completed
    }

    On a button when you want the delay to start, add an 'On Button Up' script of:
    status="StartWait"

    This would wait 1000 milliseconds. The method you use probably depends on if you want to wait for a number of frames, or for a specific amount of time.

    Morgan.
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

Bookmarks

Posting Permissions

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