Ultrashock Forums > Flash > ActionScript
ActionScript Tips Thread

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Closed thread | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes
<|1|2| Page 2 of 2
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2006-12-13 #41 Old  
038 - Math and Bits
Last edited by Nutrox : 2006-12-24 at 05:08.
&nbsp;
If you have been paying attention to this thread then you should know that you can quickly floor a number with bit-shifting ( 12.34 >> 0 = 12 ). You can also get the same performance increase if you use bit-shifting for multiplication and&nbsp;division.

This will not work for all values though, you can only multiply and divide values by 2, 4, 8, 16, 32, and so on. Also, the result will be rounded down (floored). However, if you are working with grids or tile based games then this can come in very&nbsp;handy.

To multiply a value you left-shift, and to divide a value you right-shift.

First of all, take a look at the following trace values:

ActionScript Code:
  1. trace( 1 << 1 ); // 2
  2. trace( 1 << 2 ); // 4
  3. trace( 1 << 3 ); // 8
  4. trace( 1 << 4 ); // 16
  5. trace( 1 << 5 ); // 32
  6. trace( 1 << 6 ); // 64
  7. trace( 1 << 7 ); // 128
  8. trace( 1 << 8 ); // 256
  9. // and so on...
  10.  
The trace results are the values that you can multiply and divide a value by. For example, if you wanted to multiply 100 by 32 you would do&nbsp;this:

ActionScript Code:
  1. var value:Number = 100;
  2. trace( value << 5 ); // 3200 ( or 100 * 32 )
  3.  
To divide 128 by 8 you would do this:

ActionScript Code:
  1. var value:Number = 128;
  2. trace( value >> 3 ); // 16 ( or 128 / 8 )
  3.  
To demonstrate the rounding down that occurs here is the same thing but this time 128.99 is&nbsp;used:

ActionScript Code:
  1. var value:Number = 128.99;
  2. trace( value >> 3 ); // 16 ( or Math.floor(128.99 / 8) )
  3.  
As you can see the result is still 16 instead of 16.12375 which you would get with the rounding&nbsp;down.

Here are a few more examples:

ActionScript Code:
  1. trace( 10 << 1 ); // 20  ( or 10 * 2  )
  2. trace( 10 >> 1 ); // 5   ( or 10 / 2  )
  3. trace( 10 << 4 ); // 160 ( or 10 * 16 )
  4. trace( 64 >> 2 ); // 16  ( or 64 / 4  )
  5. trace( 64 << 2 ); // 256 ( or 64 * 4  )
  6.  
 
Codemonkey's Avatar Codemonkey Codemonkey is offline Super Moderator Codemonkey lives in Netherlands 2006-12-24 #42 Old  
039 - Having private constructors
Last edited by Codemonkey : 2006-12-27 at 06:54.
&nbsp;
Sometimes you would want to make a class constructor private when:
  • You have a utility class, like Math and it makes no sense to be able to instantiate an object of this; it's globally accessible anyway and it has no 'state' that could change; each instantiated utility object would be exactly the same and only waste resources.
    Also, a private constructor is a hint to the users about the purpose of the utility class.
  • You want to simulate an abstract class. Abstract classes can't be instantiated directly, because it is not meant for that purpose; its purpose is to provide a certain interface with some common/default implementation of its methods.
    To avoid having people instantiate them anyway, make the constructor private. In AS2, a non-abstract class that extends this simulated abstract class can still call the private constructor.
  • You want to restrict public access to a class, for example to allow a class to manage its own instances/instantiation. The stereotype example of this would be a 'singleton class'. This is a class that lets you call a static method on a class which in return gives you an object of that class, without instantiating a new one (except for the first call).
    Based on the earlier points in this post you could argue that a utility class should be a singleton, because there's only one object needed. But due to the nature of a utility class - being stateless and all - it's not applicable since you can make all the utility methods static, which is easier in use as well in this particular case.
 
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2007-02-26 #43 Old  
040 - [AS3] Reversing the Display List
&nbsp;
Reversing the display order of objects is extremely easy in AS3.

The function:

ActionScript Code:
  1. function reverseChildren(target: DisplayObjectContainer):void
  2. {
  3.     var i:int = target.numChildren >> 1;
  4.     while (i--)
  5.     {
  6.         swapChildrenAt(i, target.numChildren - (i + 1));
  7.     }
  8. }
The example:

ActionScript Code:
  1. // "container" is a Sprite full of children
  2. reverseChildren(container);
 
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2007-02-27 #44 Old  
041 - Odd or Even
Last edited by Nutrox : 2007-02-27 at 21:53.
&nbsp;
A quick 'n' simple way to find out if a number is odd or even is to use the modulo operator ( % ).

ActionScript Code:
  1. var na:Number = 1.0;
  2. var nb:Number = 2.0;
  3. var nc:Number = 3.0;
  4. var nd:Number = 4.0;
  5.  
  6. // Check if the numbers are even
  7.  
  8. trace( !(na % 2) ); // false
  9. trace( !(nb % 2) ); // true
  10. trace( !(nc % 2) ); // false
  11. trace( !(nd % 2) ); // true
  12.  
 
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2007-09-14 #45 Old  
042 - Radian/Degrees Optimisation
&nbsp;
In tip 035-Trigonometry I showed how to convert radians to degrees and vise-versa. I needed to get those conversions happening a lot quicker so I tried a few things out and ended up with the following:


Degrees to Radians (original)
radians = ( degrees / 180 ) * Math.PI

Degrees to Radians (new)
radians = degrees * 0.01745329251994

The (new) version is about 12 times faster than the (original) version.


Radians to Degrees (original)
degrees = ( radians / Math.PI ) * 180

Radians to Degrees (new)
degrees = radians * 57.29577951308232

Again, the (new) version is about 12 times faster than the (original) version.


I don't think it is possible to get the conversions happening any quicker than that, even using lookup tables would be slower, but if anyone does know of a quicker way to do the conversions please let us know.

 
Codemonkey's Avatar Codemonkey Codemonkey is offline Super Moderator Codemonkey lives in Netherlands 2007-09-15 #46 Old  
043 - Radian/Degrees Optimisation followup
&nbsp;
This approach for math operation works well in general and is akin to the lookup table approach, where you avoid unneeded repetition.

For clarity, you may want to define constants for these precalculated values so you and other people remember how you got this 'magic' value.

ActionScript Code:
  1. // change 'var' to 'const' in AS3
  2. public static var PI_DIV_DEGREES = Math.PI / 180;
  3. public static var DEGREES_DIV_PI = 180 / Math.PI;
ActionScript Code:
  1. radians = degrees * PI_DIV_DEGREES;
  2. degrees = radians * DEGREES_DIV_PI;
 
<|1|2| Page 2 of 2
Thread Tools
Display Modes Rate This Thread
Rate This Thread: