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 | November 7, 2008
Actionscript:
-
var yellow:uint = 0xFFFF00;
-
-
// draws yellow circle
-
with(graphics) beginFill(yellow), drawCircle(100,100,50);
-
-
// invert the color using XOR assignment
-
// yellow becomes 0x0000FF
-
yellow ^= 0xFFFFFF;
-
-
// draws blue circle
-
with(graphics) beginFill(yellow), drawCircle(200,100,50);
Just a fun use for XOR. You could also do it without XOR assignment:
Actionscript:
-
with(graphics) beginFill(yellow ^ 0xFFFFFF), drawCircle(200,100,50);
Playing a little with bitwise operators is a good way to make sure you understand them. Try tweaking this trace statement:
Actionscript:
-
trace((0x543210 ^ 0xFFFFFF).toString(16));
If you'd like to read about bitwise operators, I recommend wikipedia's article.
Also posted in one-liners | Tagged binary, bitwise, color |
By Zevan | November 6, 2008
Actionscript:
-
// toggle a DisplayObject's visible property
-
var shape = new Shape();
-
-
shape.visible = !shape.visible;
-
trace(shape.visible); // outputs false
-
-
shape.visible = !shape.visible;
-
trace(shape.visible); // outputs true
-
-
shape.visible = !shape.visible;
-
trace(shape.visible); // outputs false
-
-
shape.visible = !shape.visible;
-
trace(shape.visible); // outputs true
This is pretty obvious to anyone with a full understanding of the ! operator. It's useful for things
like checkboxes and other types of toggle buttons. The first time I ever encountered this technique
was in processing source... in one of the demos on toxi.co.uk
Here's another quick example you can run in your timeline:
Actionscript:
-
var circle:Shape = new Shape();
-
circle.graphics.beginFill(0xFF0000);
-
circle.graphics.drawCircle(0, 0,10);
-
circle.x = 80;
-
circle.y = 105;
-
addChild(circle)
-
-
var btn:TextField = new TextField();
-
btn.text = "click this text to toggle red circle's visibility";
-
btn.x = btn.y = 100;
-
btn.selectable = false;
-
btn.border = true;
-
btn.autoSize = TextFieldAutoSize.LEFT;
-
addChild(btn);
-
-
btn.addEventListener(MouseEvent.CLICK, onClick);
-
function onClick(evt:MouseEvent):void{
-
circle.visible = !circle.visible;
-
}
Because "not false" is true.