A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: code4u - DelayedLoop

  1. #1
    Member
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    76

    Post code4u - DelayedLoop

    Hey guys!

    Here's an interesting ActionScript 3 class for experienced programmers. It can delay simple 'for loops'. It's very useful for scripts with many loops that take much processing power (e.g. A* search algorithm). Check it out!

    You can copy the following script or download the attached sample movie.

    Notice that both the code and the sample are released under GNU General Public License.

    -------------
    DelayedLoop
    Version 1.0
    -------------


    Code:
    /*
    	DelayedLoop
    	 Version 1.0
    	 code4u projects
    	
        Copyright (C) 2009 TreeBytes Interactive
    
    	[NATIVE] + [AS3] only
    	
    
        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        any later version.
    
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
    
        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    
    
    
    	-HOW IT WORKS-
    	
    	Just create a new DelayLoop instance.
    	
    	-----
    	
    	new DelayedLoop( func:Function ,
    					 start:Number,
    					 end:Number
    					 [,prio:Number] );
    	
    	func	-	the function that contains the loop script.
    	Function	It has to take 1 parameter so that the handler
    				can pass the current loop position.
    	
    	start	-	the start position of the loop.
    	Number
    	
    	end		-	the end position.
    	Number
    	
    	prio	-	(default = 1) the priority of the loop.
    	Number		Loops with a higher value will be called
    				more often.
    				
    	+-NOTICE!---------------------------------------+
    	| The handler will increase the loop position	|
    	| by 1 on every step.							|
    	+-----------------------------------------------+
    	
    	
    	-EXAMPLE-
    	
    	// A simple 'for loop':
    	// ------------------
    	
    	for (var i:uint=0; i < myArray.length; i++)
    	{
    		myArray[i].foo();
    		myArray[i].x += 3;
    		root.foo += myArray[i].y;
    	}
    	
    
    
    	
    	// Now we "delay" this loop:
    	// -------------------------
    	
    	function myloop(i:uint)
    	{
    		myArray[i].foo();
    		myArray[i].x += 3;
    		root.foo += myArray[i].y;
    	}
    	
    	var dl = new DelayedLoop(myloop,0,myArray.length);
    	
    	// That's all!
    */
    
    #if native || as3
    class DelayedLoop extends EventDispatcher
    {
    	static var list:Array = new Array;
    	static var setup:Boolean = false;
    	static var performanceMaximum:Number;
    	static var performanceRaise = 0.25;
    	
    	var func:Function;
    	var end:Number;
    	var userPriority:Number;
    	var priority:Number;
    	var intensity:Number;
    	var position:Number;
    	
    	function DelayedLoop(func:Function,start,end,prio=1)
    	{
    		this.func = func;
    		this.position = Math.abs(start);
    		this.end = Math.abs(end);
    		this.userPriority = Math.abs(prio);
    		this.priority = Math.abs(prio);
    		this.intensity = -1;
    		
    		if (!DelayedLoop.setup)
    		{
    			root.addEventListener(Event.ENTER_FRAME,DelayedLoop.run);
    			DelayedLoop.setup = true;
    			DelayedLoop.performanceMaximum = (1000 / (root.stage.frameRate+5))
    		}
    		
    		DelayedLoop.list.push(this);
    	}
    	
    	function call()
    	{
    		this.func(this.position);
    		this.position = this.position+1;
    		this.priority = 0;
    	}
    	
    	static function run(e:Event)
    	{
    		var globtimebef = getTimer();
    		var usablePerformance = performanceMaximum;
    
    		list.sortOn("priority",Array.NUMERIC | Array.DESCENDING);
    		
    		var runs = 0;
    		
    		while (usablePerformance > 0 && list.length > 0)
    		{
    			list.sortOn("priority",Array.NUMERIC | Array.DESCENDING);
    
    			var work = DelayedLoop.list[0];
    			
    			if (work.position < work.end)
    			{
    				if (work.intensity < usablePerformance)
    				{
    					var tbef = getTimer();
    					work.call();
    					var time = getTimer()-tbef;
    					work.intensity = time;
    					usablePerformance = usablePerformance-time;
    					runs++;
    				}
    			}
    			else
    			{
    				list.removeValue(work);
    			}
    			usablePerformance--;
    			
    			for (var a=0; a<list.length; a++)
    			{
    				list[a].priority = list[a].priority+list[a].userPriority;
    			}
    		}
    		
    		if (list.length > 0)
    		{
    			if (runs == 0)
    			{
    				work = list[0];
    				work.call();
    			}
    			var globtime = getTimer() - globtimebef;
    			
    			if(globtime > (1000 / (root.stage.frameRate)))
    			{
    				performanceMaximum = performanceMaximum-5;
    				performanceRaise = performanceRaise / 2;
    			}
    			else if (performanceMaximum < (1000 / (root.stage.frameRate-5)))
    			{
    				performanceMaximum = performanceMaximum+performanceRaise;
    				performanceRaise = performanceRaise*1.01;
    			}
    		}
    	}
    }
    #endif

    Have fun!
    Attached Files Attached Files

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