By Zevan | February 1, 2009
Actionscript:
-
package {
-
import flash.display.*;
-
-
public class Main extends MovieClip{
-
-
private static var _display:MovieClip;
-
private static var _stage:Stage;
-
-
// use getters instead of public static vars so they
-
// cannot be reset outside of Main
-
public static function get display():MovieClip{
-
return _display;
-
}
-
-
public static function get stage():Stage{
-
return _stage;
-
}
-
-
public function Main(){
-
Main._display = this;
-
Main._stage = stage;
-
-
// test out the static references
-
var t:Test = new Test();
-
}
-
}
-
}
-
-
class Test{
-
public function Test(){
-
// test out the static references
-
with(Main.display.graphics){
-
lineStyle(1, 0xFF0000);
-
for (var i:int = 0; i<100; i++){
-
lineTo(Math.random() * Main.stage.stageWidth,
-
Math.random() * Main.stage.stageHeight);
-
}
-
}
-
}
-
}
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:
-
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....
By Zevan | January 28, 2009
Actionscript:
-
//
-
// swap some variables
-
// all techniques except the first are from http://cpptruths.blogspot.com/2006/04/swapping-two-integers-in-one-liner.html
-
//
-
var a:Number = 1.1;
-
var b:Number= 2.2;
-
-
trace(a, b);
-
-
// best, fastest, easiest to read way
-
var t:Number= a;
-
a = b;
-
b = t;
-
-
trace(a, b);
-
-
// not recommended slower ways:
-
-
b=a+b-(a=b);
-
-
trace(a, b);
-
-
// xor versions will only work with ints and uints
-
trace("\nxor kills decimals:");
-
-
// easy to understand xor version
-
a^=b;
-
b^=a;
-
a^=b;
-
-
trace(a, b);
-
-
// one line xor version
-
-
a=(b=(a=b^a)^b)^a;
-
-
trace(a, b);
-
-
/* outputs:
-
1.1 2.2
-
2.2 1.1
-
1.1 2.2
-
-
xor kills decimals:
-
2 1
-
1 2
-
*/
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.
By Zevan | December 30, 2008
Actionscript:
-
var backgroundColor:uint = 0xEFEFEF;
-
var borderColor:uint = 0xFF0000;
-
var buttonOverColor:uint = 0x0000FF;
-
var buttonOutColor:uint = 0x00CCCC;
-
-
var uiColors:XML = <ui>
-
<default color="0xCCCCCC" />
-
<background color = { backgroundColor } />
-
-
<!-- note hexidecimal formatting code -->
-
<border color={ ("0x" + borderColor.toString(16)) } />
-
-
<button overColor={ buttonOverColor} outColor={ buttonOutColor } />
-
</ui>
-
-
trace(uiColors.toXMLString());
-
-
/*outputs:
-
<ui>
-
<default color="0xCCCCCC"/>
-
<background color="15724527"/>
-
<border color="0xff0000"/>
-
<button overColor="255" outColor="52428"/>
-
</ui>
-
*/
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:
-
<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 actionscript, E4X, flash, XML |