Category Archives: variables

Static Stage and More…

Actionscript:
  1. package {
  2.     import flash.display.*;
  3.  
  4.     public class Main extends MovieClip{
  5.    
  6.         private static var _display:MovieClip;
  7.         private static var _stage:Stage;
  8.        
  9.         // use getters instead of public static vars so they
  10.         // cannot be reset outside of Main
  11.         public static function get display():MovieClip{
  12.             return _display;
  13.         }
  14.        
  15.         public static function get stage():Stage{
  16.             return _stage;
  17.         }
  18.        
  19.         public function Main(){
  20.            Main._display = this;
  21.            Main._stage = stage;
  22.            
  23.            // test out the static references
  24.            var t:Test = new Test();
  25.         }
  26.     }
  27. }
  28.  
  29. class Test{
  30.     public function Test(){
  31.         // test out the static references
  32.         with(Main.display.graphics){
  33.             lineStyle(1, 0xFF0000);
  34.             for (var i:int = 0; i<100; i++){
  35.                 lineTo(Math.random() * Main.stage.stageWidth,
  36.                        Math.random() * Main.stage.stageHeight);
  37.             }
  38.         }
  39.     }
  40. }

This snippet creates two private static variables that reference the stage and the main timeline/document class. It then uses getters to regulate the use of these static vars so that they cannot be reset from outside the Main class.

Sometimes the amount of extra coding you need to do to maintain a valid stage reference can be cumbersome... similarly, passing references of the document class all around your app can be annoying. If you don't mind using two global vars in your app... this trick can come in handy.

What's nice about using getters here is that if someone tries to do this:

Actionscript:
  1. Main.display = new MovieClip();

they'll get an error... in flex, you even see that little red ex pop up next to this line of code if you write it :) ... that wouldn't happen with a public static var....

Also posted in DisplayObject, OOP | Tagged , , | Leave a comment

Variable Swap

Actionscript:
  1. //
  2. // swap some variables
  3. // all techniques except the first are from http://cpptruths.blogspot.com/2006/04/swapping-two-integers-in-one-liner.html
  4. //
  5. var a:Number = 1.1;
  6. var b:Number= 2.2;
  7.  
  8. trace(a, b);
  9.  
  10. // best, fastest, easiest to read way
  11. var t:Number= a;
  12. a = b;
  13. b = t;
  14.  
  15. trace(a, b);
  16.  
  17. // not recommended slower ways:
  18.  
  19. b=a+b-(a=b);
  20.  
  21. trace(a, b);
  22.  
  23. // xor versions will only work with ints and uints
  24. trace("\nxor kills decimals:");
  25.  
  26. // easy to understand xor version
  27. a^=b;
  28. b^=a;
  29. a^=b;
  30.  
  31. trace(a, b);
  32.  
  33. // one line xor version
  34.  
  35. a=(b=(a=b^a)^b)^a;
  36.  
  37. trace(a, b);
  38.  
  39. /* outputs:
  40. 1.1 2.2
  41. 2.2 1.1
  42. 1.1 2.2
  43.  
  44. xor kills decimals:
  45. 2 1
  46. 1 2
  47. */

The above swaps variables a and b in a few different ways. The first way (using a temp variable) is the best and fastest way... the rest of the ways are just interesting and fun.

I was coding and something reminded me that there are obscure variable swapping techniques out there... so I figured I'd google for a bit.... there are tons of examples of these online - with lots of good explanations.... I got the above from this link.

Also posted in Operators, one-liners | Tagged , | Leave a comment

Constants as Function Argument Defaults

Actionscript:
  1. const RADIUS = 10;
  2. const COLOR = 0xCCCCCC;
  3.  
  4. function createCircle(xp:Number, yp:Number, radius:Number = RADIUS, col:uint = COLOR):void{
  5.     var c:Sprite = new Sprite();
  6.     c.graphics.beginFill(col);
  7.     c.graphics.drawCircle(xp, yp, radius);
  8.     addChild(c);
  9. }
  10.  
  11. // optional last 2 values are populated with const defaults
  12. createCircle(100,100);
  13. // optional color value is populated with default 0xCCCCCC;
  14. createCircle(200,100, 20);
  15. //
  16. createCircle(300,100, 50,0x0000FF);

Also posted in functions | Tagged , | Leave a comment

E4X and ActionScript Variables

Actionscript:
  1. var backgroundColor:uint = 0xEFEFEF;
  2. var borderColor:uint = 0xFF0000;
  3. var buttonOverColor:uint = 0x0000FF;
  4. var buttonOutColor:uint = 0x00CCCC;
  5.  
  6. var uiColors:XML = <ui>
  7.     <default color="0xCCCCCC" />
  8.     <background color = { backgroundColor } />
  9.    
  10.     <!-- note hexidecimal formatting code -->
  11.     <border color={ ("0x" + borderColor.toString(16)) } />
  12.    
  13.     <button overColor={ buttonOverColor} outColor={ buttonOutColor } />
  14. </ui>
  15.  
  16. trace(uiColors.toXMLString());
  17.  
  18. /*outputs:
  19. <ui>
  20.   <default color="0xCCCCCC"/>
  21.   <background color="15724527"/>
  22.   <border color="0xff0000"/>
  23.   <button overColor="255" outColor="52428"/>
  24. </ui>
  25. */

The first time I needed to use an ActionScript variable within inline XML I was stumped. I couldn't figure it out and I wasn't able to easily find it on google. I eventually found it somewhere (don't remember where... possibly hidden away in the docs).

Now a search for "insert actionscript variables into e4x" on google gives plenty of results. But I figure it's worth a post.

I use actionscript to generate XML all the time so this comes in handy. I also store color values in automatically generated XML all the time. If I'm feeling organized I'll use something like what you see on line 10:

Actionscript:
  1. <border color={ ("0x" + borderColor.toString(16)) } />

If you look at the output you'll see this formats the uint so that it's readable as a hex number. By default (as you can see in the output) uints will show up in decimal notation. This really doesn't make any difference if you don't care about XML readability ... or if you just don't care about the readability of the colors stored in your XML.....

Also posted in XML, color | Tagged , , , | Leave a comment