By Zevan | December 18, 2008
Actionscript:
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function onLoop(evt:Event):void {
-
var boolA:Boolean = false;
-
var boolB:Boolean = false;
-
-
if (mouseX> 200){
-
boolA = true;
-
}else{
-
boolA = false;
-
}
-
-
if (mouseY <200){
-
boolB = true
-
}else{
-
boolB = false;
-
}
-
-
if (boolA || boolB){
-
trace("regular or");
-
}
-
-
// this is the XOR conditional
-
if (int(boolA) ^ int(boolB)){
-
trace("exclusive or");
-
}
-
-
}
There's an obvious more logical ways to do this. But I thought it was somewhat interesting to see....
So in binary XOR will give us:
0^0 = 0;
0^1 = 1;
1^0 = 1;
1^1 = 0;
What other operator might you use to get the same results?... that's right !=
With regular OR we get:
0|0 = 0;
0|1 = 1;
1|0 = 1;
1|1 = 1;
Posted in misc | Also tagged binary, bitwise, flash, Operators |
By Zevan | December 17, 2008
Actionscript:
-
trace((new Date()).getTime() + Math.random()*0xFFFFFF);
A hacky way to get a unique ID, although this isn't, perfect it works well enough. For a project that you really expect to get huge amounts of traffic... you might consider using php's uniqid() function and passing it into your swf. That said, the odds of two users getting the same two ID values are about as good as the odds of you winning the lottery twice in a row.
Here is a version wrapped in a function, with a prefix argument:
Actionscript:
-
trace(uniqid("z"));
-
-
function uniqid(prefix:String):String{
-
return prefix + (new Date()).getTime() + Math.random()*0xFFFFFF;
-
}
Posted in misc, one-liners, random | Also tagged flash |
By Zevan | December 16, 2008
Actionscript:
-
function numParents(e:XML):int {
-
var num:int = 0;
-
while (e.parent()!= null) {
-
num++;
-
e = e.parent();
-
}
-
return num;
-
}
Sometimes when reading through XML it's helpful to know how many parents a given node has. This function will do that. I recently used this function in an accordion widget.
Posted in XML | Also tagged flash |
By Zevan | December 15, 2008
Actionscript:
-
var cam:Camera = Camera.getCamera();
-
var video:Video = new Video(320, 240);
-
video.attachCamera(cam);
-
addChild(video);
Adds the feed from your webcam to the stage.
Posted in Video | Also tagged flash |