A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: random timer

  1. #1
    Member Mofo SwirlyKing's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    476

    random timer

    I feel like this should be really simple, but I cannot figure out how to get a function to run at random intervals in AS3. Would someone mind showing me the most stripped-down version of a random interval timer?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    package
    {
    	import flash.display.Sprite;
    	import flash.events.TimerEvent;
    	import flash.utils.Timer;
    	
    	public class Main extends flash.display.Sprite
    	{
    		private var timer:Timer;
    		private var maxDelay:int = 2000;
    		
    		public function Main():void
    		{
    			timer = new Timer(getRandDelay());
    			timer.addEventListener(TimerEvent.TIMER, doSomething);
    			timer.start();
    		}
    		
    		private function getRandDelay():Number{
    			return Math.random()*maxDelay;
    		}
    		
    		private function doSomething(evt:TimerEvent):void{
    			timer.delay = getRandDelay();
    			trace("bloop!, delay:"+timer.delay);
    		}
    		
    		
    	}
    }

  3. #3
    Member Mofo SwirlyKing's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    476
    thank you, that's exactly what I needed!

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