A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] action occuring after a set amount of time.

  1. #1
    suvat cobhc's Avatar
    Join Date
    Jan 2004
    Location
    england
    Posts
    156

    [RESOLVED] action occuring after a set amount of time.

    how is this done?
    im assuming "getTimer();" is used and "random();"
    but in what context?
    something like:
    code:

    _root.onEnterFrame = function() {
    timer = getTimer()/1000;
    if ((timer) = .25) {
    do action
    }
    }
    };


    where does the random number code go#

    basically i want the timer to start counting down from a random time of max about 3 seconds until it reaches 0.then an action occurs

    any help is appreciated
    thanks
    cobhc

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518

    Maybe try something like this...

    Code:
    //
    function getRand(maxNum, minNum) {
    	return Math.floor(Math.random() * (maxNum - minNum)) + minNum;
    }
    //
    var startTime = getTimer();
    var limitTime = getRand(3, 1) * 1000;
    _root.onEnterFrame = function() {
    	if (getTimer() - startTime > limitTime) {
    		//do action
    		trace("time's up!");
    		delete this.onEnterFrame;
    	}
    };

  3. #3
    suvat cobhc's Avatar
    Join Date
    Jan 2004
    Location
    england
    Posts
    156
    your a saint that works perfectly!!!
    thanks so much!
    cobhc

  4. #4
    suvat cobhc's Avatar
    Join Date
    Jan 2004
    Location
    england
    Posts
    156
    just encountered a major problem
    as this event occurs after a random amount of time (ive included a demo of my game this time so it can be more specifically related) it stops all the other code within the _root.onEnterFrame instance.
    i think its something to do with the delete this.onEnterFrame function?
    thanks
    rory
    the example

  5. #5
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Yes, if you are using the _root.onEnterFrame for other things, the time out will stop them. The modified code below eliminates this problem.

    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;
    		}
    	}
    };

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center