Actionscript:
-
// NOTE: this code won't do anything in you're timline....
-
-
// do you do this?
-
-
if (ball.x <0 || ball.y <0 || ball.x> stage.stageWidth || ball.y> stage.stageHeight){
-
// cause ball to explode
-
}
-
-
// or this?
-
-
if (isAtStageEdge(ball)){
-
// cause ball to explode
-
}
-
-
function isAtStageEdge(mc:MovieClip):Boolean{
-
return (mc.x <0 || mc.y <0 || mc.x> stage.stageWidth || mc.y> stage.stageHeight);
-
}
This is pretty standard stuff, but it can increase code readability especially for complex conditionals.
I also do this if I find myself repeating an a semi-complex if statement in two or more places.
If I'm just brainstorming I probably won't bother, but on real projects it helps keep code readable.
Actionscript:
-
[SWF(height = 950, width=1000)]
-
var txt:TextField;
-
var num:int = 0;
-
for (var i:int = 0; i<761; i++){
-
if (i % 76 == 0){
-
makeTxt(num);
-
num++;
-
}
-
if (i <0x21){
-
txt.appendText("-\n");
-
}else{
-
txt.appendText(String.fromCharCode(i) + " = " + i + "\n");
-
}
-
}
-
function makeTxt(num:Number):void{
-
txt= TextField(addChild(new TextField()));
-
txt.x = 10 + num * 100;
-
txt.y = 10;
-
txt.height = 930;
-
txt.text = "";
-
}
This will draw a few columns of char codes.
Posted in strings | Tagged actionscript, flash |
By Zevan | February 28, 2009
Actionscript:
-
var letterA:String = "a";
-
var letterZ:String = "z";
-
-
trace(letterA.localeCompare(letterZ));
-
trace(letterZ.localeCompare(letterA));
-
/*
-
outputs
-
-25
-
25
-
*/
String.localeCompare() calculates the sorting order between two Strings....
Posted in strings | Tagged actionscript, flash |
By Zevan | February 27, 2009
Actionscript:
-
function randomize(a:*, b:*):int{
-
return Math.round(Math.random()*8) - 4;
-
}
-
-
var i:int;
-
var fruits:Array;
-
-
trace("Math.random()");
-
for (i = 0; i<4; i++){
-
// reset fruits array:
-
fruits = ["apple", "grape","pear","cherry"];
-
fruits.sort(randomize);
-
trace(fruits);
-
}
-
-
-
// seeds
-
var s1:Number= 0xFF00FF;
-
var s2:Number = 0xCCCCCC;
-
var s3:Number= 0xFF00F0;
-
-
function tRandomize(a:*, b:*):int{
-
return Math.round(rand()*8) - 4;
-
}
-
-
trace("\nTausworthe rand()");
-
for (i= 0; i<4; i++){
-
fruits = ["apple", "grape","pear","cherry"];
-
fruits.sort(tRandomize);
-
trace(fruits);
-
}
-
// from www.ams.org/mcom/1996-65-213/S0025-5718-96-00696-5/S0025-5718-96-00696-5.pdf
-
function rand():Number {
-
s1=((s1&4294967294)<<12)^(((s1<<13)^s1)>>19);
-
s2=((s2&4294967288)<<4)^(((s2<<2)^s2)>>25);
-
s3=((s3&4294967280)<<17)^(((s3<<3)^s3)>>11);
-
var r:Number = (s1^s2^s3) * 2.3283064365e-10;
-
r = (r<0) ? r+=1 : r;
-
return r;
-
}
-
-
/*
-
outputs:
-
Math.random()
-
grape,apple,pear,cherry
-
pear,cherry,apple,grape
-
grape,apple,pear,cherry
-
grape,apple,cherry,pear
-
-
Tausworthe rand()
-
apple,grape,pear,cherry
-
cherry,grape,pear,apple
-
grape,apple,cherry,pear
-
grape,pear,apple,cherry
-
*/
The above shows how to randomly sort or shuffle an array. This is useful in games. To achieve this I made use of the compareFunction argument of Array.sort(). Most sorting algorithms go through the array and compare values until the desired sort order is achieved. The compareFunction argument is a function that takes two values a and b and returns an integer that is negative positive or zero... see this info from the docs:
* A negative return value specifies that A appears before B in the sorted sequence.
* A return value of 0 specifies that A and B have the same sort order.
* A positive return value specifies that A appears after B in the sorted sequence.
So in the case of a randomizing an array you simply need to return a random int -1, 0 or 1. This is what I've done in the past (Math.round()*2) -1) ... but when I was writing this snippet it seemed like 0 caused less variation in the output of the array so I made the range from -4 to 4 instead. This could have just been my imagination, but it seems like having less chance of a zero caused the arrays to be a bit more shuffled.
The reason I also included a version that uses Tausworthe is because of the easy seeding. In some cases you may want to use seeded randomness to sort an array.
UPDATE:
Was digging around about this and found a much faster method for randomizing arrays... not a big deal if you have small arrays, but if you need to randomize 1000's of values this method is much faster than using Array.sort()