├── .gitignore ├── README.textile └── src └── net └── richardlord ├── collisions └── HitTest.as ├── fsm ├── State.as └── StateMachine.as ├── input └── KeyPoll.as └── utils ├── FunctionUtils.as ├── MathUtils.as ├── ObjectPool.as ├── WeakRef.as ├── construct.as └── singleton.as /.gitignore: -------------------------------------------------------------------------------- 1 | # General 2 | bin/* 3 | docs/* 4 | swc/* 5 | 6 | # Eclipse 7 | .settings/* 8 | .project 9 | 10 | # Flash Builder 11 | html-template/* 12 | bin-debug/* 13 | bin-release/* 14 | .actionScriptProperties 15 | .flexProperties 16 | .flexLibProperties 17 | 18 | # FDT 19 | .as3_classpath 20 | 21 | # Source Mate 22 | .sourceMate 23 | .sourceMateMetadataSettings 24 | 25 | # IntelliJ 26 | .idea 27 | *.iml 28 | *.ipr 29 | *.iws 30 | 31 | # OS X 32 | Icon 33 | Thumbs.db 34 | .DS_Store -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h1. A random collection of useful code 2 | 3 | This project contains various useful Actionscript code from "my blog":http://www.richardlord.net/blog 4 | 5 | h2. Included 6 | 7 | * "Key Poll":https://github.com/richardlord/Actionscript-Toolkit/blob/master/src/net/richardlord/input/KeyPoll.as - for checking if any key is down or up at a specific time. 8 | * "Finite State Machine":https://github.com/richardlord/Actionscript-Toolkit/tree/master/src/net/richardlord/fsm - A simple finite state machine for Actionscript 3 games. 9 | * "Weak Ref":https://github.com/richardlord/Actionscript-Toolkit/blob/master/src/net/richardlord/utils/WeakRef.as - creates a weak reference to an object. 10 | * "Object Pool":https://github.com/richardlord/Actionscript-Toolkit/blob/master/src/net/richardlord/utils/ObjectPool.as - a simple object pool for any and all object types. 11 | * "Singleton":https://github.com/richardlord/Actionscript-Toolkit/blob/master/src/net/richardlord/utils/singleton.as - a simple factory to create and maintain a single instance of any class or classes. 12 | * "Function Utils":https://github.com/richardlord/Actionscript-Toolkit/blob/master/src/net/richardlord/utils/FunctionUtils.as - utilities to modify a function signature. 13 | * "Math Utils":https://github.com/richardlord/Actionscript-Toolkit/blob/master/src/net/richardlord/utils/MathUtils.as - utilities for changing between degrees and radians and for getting random numbers. 14 | * "Construct":https://github.com/richardlord/Actionscript-Toolkit/blob/master/src/net/richardlord/utils/construct.as - a function to call the constructor of a class (function.apply can't be used to call the constructor). 15 | * "Hit Test":https://github.com/richardlord/Actionscript-Toolkit/blob/master/src/net/richardlord/collisions/HitTest.as - performs collision checking between two display objects by drawing them into a bitmap and checking for an overlap. 16 | 17 | h2. License 18 | 19 | All code is covered by the MIT open-source license. The license text is included at the top of each source code file. -------------------------------------------------------------------------------- /src/net/richardlord/collisions/HitTest.as: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Richard Lord 3 | * Copyright (c) Richard Lord 2007 4 | * http://www.richardlord.net/ 5 | * 6 | * Licence Agreement (The MIT License) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | package net.richardlord.collisions 28 | { 29 | import flash.display.BitmapData; 30 | import flash.display.BlendMode; 31 | import flash.display.DisplayObject; 32 | import flash.geom.ColorTransform; 33 | import flash.geom.Matrix; 34 | import flash.geom.Rectangle; 35 | 36 | public class HitTest 37 | { 38 | public static function hitTestRect( r1:Rectangle, r2:Rectangle ):Boolean 39 | { 40 | return r1.top < r2.bottom && r1.bottom > r2.top 41 | && r1.left < r2.right && r1.right > r2.left; 42 | } 43 | 44 | public static function hitTestObject( d1:DisplayObject, d2:DisplayObject ):Boolean 45 | { 46 | if( !d1.hitTestObject( d2 ) ) 47 | { 48 | return false; 49 | } 50 | var bounds:Rectangle = d1.getRect( d1 ); 51 | var bmd:BitmapData = new BitmapData( bounds.width, bounds.height, false, 0 ); 52 | 53 | var matrix:Matrix = d1.transform.matrix.clone(); 54 | matrix.translate( -bounds.left, -bounds.top ); 55 | bmd.draw( d1, matrix, new ColorTransform( 0, 0, 0, 0, 255, 0, 0, 255 ) ); 56 | 57 | matrix = d2.transform.matrix.clone(); 58 | matrix.translate( -bounds.left, -bounds.top ); 59 | bmd.draw( d2, matrix, new ColorTransform( 0, 0, 0, 0, 0, 0, 255, 255 ), BlendMode.ADD ); 60 | 61 | var rect:Rectangle = bmd.getColorBoundsRect( 0xFFFFFF, 0xFF00FF ); 62 | return rect.width > 0; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/net/richardlord/fsm/State.as: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Richard Lord 3 | * Copyright (c) Richard Lord 2007 4 | * http://www.richardlord.net/blog/finite-state-machines-for-ai-in-actionscript 5 | * 6 | * Licence Agreement (The MIT License) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | package net.richardlord.fsm 28 | { 29 | /** 30 | * The interface for any state used by the state machine 31 | */ 32 | public interface State 33 | { 34 | function enter():void; // called on entering the state 35 | function exit():void; // called on leaving the state 36 | function update( time:Number ):void; // called every frame while in the state 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/net/richardlord/fsm/StateMachine.as: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Richard Lord 3 | * Copyright (c) Richard Lord 2007 4 | * http://www.richardlord.net/blog/finite-state-machines-for-ai-in-actionscript 5 | * 6 | * Licence Agreement (The MIT License) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | package net.richardlord.fsm 28 | { 29 | import flash.utils.Dictionary; 30 | 31 | public class StateMachine 32 | { 33 | private var states : Dictionary; 34 | 35 | private var _currentState : State; 36 | private var _previousState : State; 37 | private var _nextState : State; 38 | 39 | public function StateMachine() 40 | { 41 | states = new Dictionary(); 42 | _currentState = null; 43 | _previousState = null; 44 | _nextState = null; 45 | } 46 | 47 | public function primeStates( ...instances ) : void 48 | { 49 | for each( var state : State in instances ) 50 | { 51 | var type : Class = Object( state ).constructor as Class; 52 | if( type ) 53 | { 54 | states[type] = state; 55 | } 56 | } 57 | } 58 | 59 | public function removeStates( ...instances ) : void 60 | { 61 | for each( var state : State in instances ) 62 | { 63 | var type : Class = Object( state ).constructor as Class; 64 | if( type && states[type] == state ) 65 | { 66 | delete states[type]; 67 | } 68 | } 69 | } 70 | 71 | private function getStateForClass( type : Class ) : State 72 | { 73 | return type in states ? states[type] : states[type] = new type(); 74 | } 75 | 76 | public function get previousState() : State 77 | { 78 | return _previousState; 79 | } 80 | public function get currentState() : State 81 | { 82 | return _currentState; 83 | } 84 | public function get nextState() : State 85 | { 86 | return _nextState; 87 | } 88 | 89 | public function setNextState( type : Class ) : void 90 | { 91 | _nextState = getStateForClass( type ); 92 | } 93 | 94 | public function update( time : Number ) : void 95 | { 96 | if ( _currentState ) 97 | { 98 | _currentState.update( time ); 99 | } 100 | } 101 | 102 | public function changeState( type : Class ) : void 103 | { 104 | _changeState( getStateForClass( type ) ); 105 | } 106 | 107 | public function changeStateInstance( state : State ) : void 108 | { 109 | primeStates( state ); 110 | _changeState( state ); 111 | } 112 | 113 | private function _changeState( state : State ) : void 114 | { 115 | if( _currentState ) 116 | { 117 | _currentState.exit(); 118 | } 119 | _previousState = _currentState; 120 | _currentState = state; 121 | if( _currentState ) 122 | { 123 | _currentState.enter(); 124 | } 125 | } 126 | 127 | public function goToPreviousState() : void 128 | { 129 | if( _previousState ) 130 | { 131 | _changeState( _previousState ); 132 | } 133 | } 134 | 135 | public function goToNextState() : void 136 | { 137 | if( _nextState ) 138 | { 139 | _changeState( _nextState ); 140 | } 141 | } 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/net/richardlord/input/KeyPoll.as: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Richard Lord 3 | * Copyright (c) Richard Lord 2007 4 | * http://www.richardlord.net/blog/polling-the-keyboard-in-actionscript-3 5 | * 6 | * Licence Agreement (The MIT License) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | package net.richardlord.input 28 | { 29 | import flash.events.KeyboardEvent; 30 | import flash.events.Event; 31 | import flash.display.DisplayObject; 32 | import flash.utils.ByteArray; 33 | 34 | /** 35 | *

Games often need to get the current state of various keys in order to respond to user input. 36 | * This is not the same as responding to key down and key up events, but is rather a case of discovering 37 | * if a particular key is currently pressed.

38 | * 39 | *

In Actionscript 2 this was a simple matter of calling Key.isDown() with the appropriate key code. 40 | * But in Actionscript 3 Key.isDown no longer exists and the only intrinsic way to react to the keyboard 41 | * is via the keyUp and keyDown events.

42 | * 43 | *

The KeyPoll class rectifies this. It has isDown and isUp methods, each taking a key code as a 44 | * parameter and returning a Boolean.

45 | */ 46 | public class KeyPoll 47 | { 48 | private var states:ByteArray; 49 | private var dispObj:DisplayObject; 50 | 51 | /** 52 | * Constructor 53 | * 54 | * @param displayObj a display object on which to test listen for keyboard events. To catch all key events use the stage. 55 | */ 56 | public function KeyPoll( displayObj:DisplayObject ) 57 | { 58 | states = new ByteArray(); 59 | states.writeUnsignedInt( 0 ); 60 | states.writeUnsignedInt( 0 ); 61 | states.writeUnsignedInt( 0 ); 62 | states.writeUnsignedInt( 0 ); 63 | states.writeUnsignedInt( 0 ); 64 | states.writeUnsignedInt( 0 ); 65 | states.writeUnsignedInt( 0 ); 66 | states.writeUnsignedInt( 0 ); 67 | dispObj = displayObj; 68 | dispObj.addEventListener( KeyboardEvent.KEY_DOWN, keyDownListener, false, 0, true ); 69 | dispObj.addEventListener( KeyboardEvent.KEY_UP, keyUpListener, false, 0, true ); 70 | dispObj.addEventListener( Event.ACTIVATE, activateListener, false, 0, true ); 71 | dispObj.addEventListener( Event.DEACTIVATE, deactivateListener, false, 0, true ); 72 | } 73 | 74 | private function keyDownListener( ev:KeyboardEvent ):void 75 | { 76 | states[ ev.keyCode >>> 3 ] |= 1 << (ev.keyCode & 7); 77 | } 78 | 79 | private function keyUpListener( ev:KeyboardEvent ):void 80 | { 81 | states[ ev.keyCode >>> 3 ] &= ~(1 << (ev.keyCode & 7)); 82 | } 83 | 84 | private function activateListener( ev:Event ):void 85 | { 86 | for( var i:int = 0; i < 8; ++i ) 87 | { 88 | states[ i ] = 0; 89 | } 90 | } 91 | 92 | private function deactivateListener( ev:Event ):void 93 | { 94 | for( var i:int = 0; i < 8; ++i ) 95 | { 96 | states[ i ] = 0; 97 | } 98 | } 99 | 100 | /** 101 | * To test whether a key is down. 102 | * 103 | * @param keyCode code for the key to test. 104 | * 105 | * @return true if the key is down, false otherwise. 106 | * 107 | * @see isUp 108 | */ 109 | public function isDown( keyCode:uint ):Boolean 110 | { 111 | return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) != 0; 112 | } 113 | 114 | /** 115 | * To test whether a key is up. 116 | * 117 | * @param keyCode code for the key to test. 118 | * 119 | * @return true if the key is up, false otherwise. 120 | * 121 | * @see isDown 122 | */ 123 | public function isUp( keyCode:uint ):Boolean 124 | { 125 | return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) == 0; 126 | } 127 | } 128 | } -------------------------------------------------------------------------------- /src/net/richardlord/utils/FunctionUtils.as: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Richard Lord 3 | * Copyright (c) Richard Lord 2007 4 | * http://www.richardlord.net/ 5 | * 6 | * Licence Agreement (The MIT License) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | package net.richardlord.utils 28 | { 29 | /** 30 | * Methods to addapt function objects to a different parameter set 31 | */ 32 | public class FunctionUtils 33 | { 34 | public static const PARAM_1:ParamObject = new ParamObject( 0 ); 35 | public static const PARAM_2:ParamObject = new ParamObject( 1 ); 36 | public static const PARAM_3:ParamObject = new ParamObject( 2 ); 37 | public static const PARAM_4:ParamObject = new ParamObject( 3 ); 38 | public static const PARAM_5:ParamObject = new ParamObject( 4 ); 39 | public static const PARAM_6:ParamObject = new ParamObject( 5 ); 40 | public static const PARAM_7:ParamObject = new ParamObject( 6 ); 41 | public static const PARAM_8:ParamObject = new ParamObject( 7 ); 42 | public static const PARAM_9:ParamObject = new ParamObject( 8 ); 43 | public static const PARAM_10:ParamObject = new ParamObject( 9 ); 44 | 45 | /** 46 | * Adapt a function. The function and parameters are wrapped as a new function, which 47 | * is returned. Parameters passed to the new function are used to replace the constants 48 | * PARAM_1 ... PARAM_10 if they are in parameters list passed to this method. 49 | * 50 | * @param method The function or method closure to wrap. 51 | * @param parameters The parameters to pass to the wrapped function when the returned 52 | * function is called. If any of the constants PARAM_! to PARAM_10 are in this list, 53 | * then those will be replaced by the corresponding argument when the returned function 54 | * is called. 55 | * @return A function that wraps the function passed in. When calling it, pass it the 56 | * values to replace PARAM_1, PARAM_2 etc if they are used in the parameters list. 57 | */ 58 | public static function adaptFunction( method:Function, ...parameters ):Function 59 | { 60 | return function( ...params ):* 61 | { 62 | var p:Array = parameters.slice( ); 63 | for( var i:int = 0; i < p.length ; ++i ) 64 | { 65 | if( p[i] is ParamObject ) 66 | { 67 | p[i] = params[ p[i].pos ]; 68 | } 69 | } 70 | return method.apply( NaN, p ); 71 | }; 72 | } 73 | 74 | /** 75 | * Adapt a member function. The method and parameters are wrapped as a new function, which 76 | * is returned. Parameters passed to the new function are used to specify the object whose 77 | * method is to be called abd to replace the constants PARAM_1 ... PARAM_10 if they are 78 | * in parameters list passed to this method. 79 | * 80 | * @param methodName The name of the method to wrap. 81 | * @param parameters The parameters to pass to the wrapped function when the returned 82 | * function is called. If any of the constants PARAM_! to PARAM_10 are in this list, 83 | * then those will be replaced by the corresponding argument when the returned function 84 | * is called. 85 | * @return A function that wraps the method passed in. When calling it, pass it the object 86 | * and the values to replace PARAM_1, PARAM_2 etc if they are used in the parameters list. 87 | */ 88 | public static function memberFunction( methodName:String, ...parameters ):Function 89 | { 90 | return function( item:*, ...params ):* 91 | { 92 | var p:Array = parameters.slice( ); 93 | for( var i:int = 0; i < p.length ; ++i ) 94 | { 95 | if( p[i] is ParamObject ) 96 | { 97 | p[i] = params[ p[i].pos - 1 ]; 98 | } 99 | } 100 | return item[ methodName ].apply( item, p ); 101 | }; 102 | } 103 | } 104 | } 105 | 106 | class ParamObject 107 | { 108 | public var pos:int; 109 | 110 | public function ParamObject( p:int ) 111 | { 112 | pos = p; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/net/richardlord/utils/MathUtils.as: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Richard Lord 3 | * Copyright (c) Richard Lord 2007 4 | * http://www.richardlord.net/ 5 | * 6 | * Licence Agreement (The MIT License) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | package net.richardlord.utils 28 | { 29 | 30 | /** 31 | * The Maths class contains a coupleof useful methods for use in maths functions. 32 | */ 33 | public class MathUtils 34 | { 35 | private static const RADTODEG:Number = 180 / Math.PI; 36 | private static const DEGTORAD:Number = Math.PI / 180; 37 | 38 | /** 39 | * Converts an angle from radians to degrees 40 | * 41 | * @param radians The angle in radians 42 | * @return The angle in degrees 43 | */ 44 | public static function asDegrees( radians:Number ):Number 45 | { 46 | return radians * RADTODEG; 47 | } 48 | 49 | /** 50 | * Converts an angle from degrees to radians 51 | * 52 | * @param radians The angle in degrees 53 | * @return The angle in radians 54 | */ 55 | public static function asRadians( degrees:Number ):Number 56 | { 57 | return degrees * DEGTORAD; 58 | } 59 | 60 | /** 61 | * Returns a random integer between two values. 62 | * 63 | * @param min The lowest value to return. 64 | * @param max The highest value to return. 65 | * 66 | * @return A random integer between min and max inclusive. 67 | */ 68 | public static function randomInt( min:int, max:int ):int 69 | { 70 | return min + int( Math.random() * ( max - min + 1 ) ); 71 | } 72 | 73 | /** 74 | * Returns a random number between two values. 75 | * 76 | * @param min The lowest value to return. 77 | * @param max The highest value to return. 78 | * 79 | * @return A random number between min and max inclusive. 80 | */ 81 | public static function randomNumber( min:Number, max:Number ):Number 82 | { 83 | return min + Math.random() * ( max - min ); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/net/richardlord/utils/ObjectPool.as: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Richard Lord 3 | * Copyright (c) Richard Lord 2008 4 | * http://www.richardlord.net/blog/object-pool-class 5 | * 6 | * Licence Agreement (The MIT License) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | package net.richardlord.utils 28 | { 29 | import flash.utils.Dictionary; 30 | import flash.utils.getDefinitionByName; 31 | import flash.utils.getQualifiedClassName; 32 | 33 | /** 34 | * Manages objects by retaining disposed objects and returning them when a new object 35 | * is requested, to avoid unecessary object creation and disposal and so avoid 36 | * unnecessary object creation and garbage collection. 37 | */ 38 | public class ObjectPool 39 | { 40 | private static var pools:Dictionary = new Dictionary( ); 41 | 42 | private static function getPool( type:Class ):Array 43 | { 44 | return type in pools ? pools[type] : pools[type] = new Array( ); 45 | } 46 | 47 | /** 48 | * Get an object of the specified type. If such an object exists in the pool then 49 | * it will be returned. If such an object doesn't exist, a new one will be created. 50 | * 51 | * @param type The type of object required. 52 | * @param parameters If there are no instances of the object in the pool, a new one 53 | * will be created and these parameters will be passed to the object constrictor. 54 | * Because you can't know if a new object will be created, you can't rely on these 55 | * parameters being used. They are here to enable pooling of objects that require 56 | * parameters in their constructor. 57 | */ 58 | public static function getObject( type:Class, ...parameters ):* 59 | { 60 | var pool:Array = getPool( type ); 61 | if( pool.length > 0 ) 62 | { 63 | return pool.pop( ); 64 | } 65 | else 66 | { 67 | return construct( type, parameters ); 68 | } 69 | } 70 | 71 | /** 72 | * Return an object to the pool for retention and later reuse. Note that the object 73 | * still exists, so you need to clean up any event listeners etc. on the object so 74 | * that the events stop occuring. 75 | * 76 | * @param object The object to return to the object pool. 77 | * @param type The type of the object. If you don't indicate the object type then the 78 | * object is inspected to find its type. This is a little slower than specifying the 79 | * type yourself. 80 | */ 81 | public static function disposeObject( object:*, type:Class = null ):void 82 | { 83 | if( !type ) 84 | { 85 | var typeName:String = getQualifiedClassName( object ); 86 | type = getDefinitionByName( typeName ) as Class; 87 | } 88 | var pool:Array = getPool( type ); 89 | pool.push( object ); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/net/richardlord/utils/WeakRef.as: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Richard Lord 3 | * Copyright (c) Richard Lord 2007 4 | * http://www.richardlord.net/blog/create-your-own-weak-references-in-actionscript-3 5 | * 6 | * Licence Agreement (The MIT License) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | package net.richardlord.utils 28 | { 29 | import flash.utils.Dictionary; 30 | 31 | /** 32 | * Creates a wek reference to an object 33 | */ 34 | public class WeakRef 35 | { 36 | private var dic : Dictionary; 37 | 38 | /** 39 | * The constructor - creates a weak reference. 40 | * 41 | * @param obj the object to create a weak reference to 42 | */ 43 | public function WeakRef( obj : * ) 44 | { 45 | dic = new Dictionary( true ); 46 | dic[obj] = 1; 47 | } 48 | 49 | /** 50 | * To get a strong reference to the object. 51 | * 52 | * @return a strong reference to the object or null if the 53 | * object has been garbage collected 54 | */ 55 | public function get() : * 56 | { 57 | for ( var item:* in dic ) 58 | { 59 | return item; 60 | } 61 | return null; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/net/richardlord/utils/construct.as: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Richard Lord 3 | * Copyright (c) Richard Lord 2007 4 | * http://www.richardlord.net/ 5 | * 6 | * Licence Agreement (The MIT License) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | package net.richardlord.utils 28 | { 29 | /** 30 | * This function is used to construct an object from the class and an array of parameters. 31 | * 32 | * @param type The class to construct. 33 | * @param parameters An array of up to ten parameters to pass to the constructor. 34 | */ 35 | public function construct( type:Class, parameters:Array ):* 36 | { 37 | switch( parameters.length ) 38 | { 39 | case 0: 40 | return new type( ); 41 | case 1: 42 | return new type( parameters[0] ); 43 | case 2: 44 | return new type( parameters[0], parameters[1] ); 45 | case 3: 46 | return new type( parameters[0], parameters[1], parameters[2] ); 47 | case 4: 48 | return new type( parameters[0], parameters[1], parameters[2], parameters[3] ); 49 | case 5: 50 | return new type( parameters[0], parameters[1], parameters[2], parameters[3], parameters[4] ); 51 | case 6: 52 | return new type( parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5] ); 53 | case 7: 54 | return new type( parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6] ); 55 | case 8: 56 | return new type( parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7] ); 57 | case 9: 58 | return new type( parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7], parameters[8] ); 59 | case 10: 60 | return new type( parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7], parameters[8], parameters[9] ); 61 | default: 62 | return null; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/net/richardlord/utils/singleton.as: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Richard Lord 3 | * Copyright (c) Richard Lord 2007 4 | * http://www.richardlord.net/blog/singleton-factory 5 | * 6 | * Licence Agreement (The MIT License) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | package net.richardlord.utils 28 | { 29 | /** 30 | * Returns the same instance of a class every time. If necessary, it creates an instance. 31 | * 32 | * Usage - 33 | * var a:SomeClass = singleton( SomeClass ); 34 | */ 35 | public function singleton( type : Class ) : * 36 | { 37 | return type in instances ? instances[type] : instances[type] = new type(); 38 | } 39 | } 40 | 41 | import flash.utils.Dictionary; 42 | var instances : Dictionary = new Dictionary( false ); --------------------------------------------------------------------------------