By Zevan | February 26, 2009
Actionscript:
-
var words:String = "one two three four five";
-
-
trace("two: ", words.indexOf("two"));
-
-
var letters:String = "abcd";
-
-
trace("d: ",letters.indexOf("d"));
-
-
trace("z: ",letters.indexOf("z"));
-
-
/*
-
outputs:
-
two: 4
-
d: 3
-
z: -1
-
*/
indexOf() searches a string for another string and returns an index... in line 3 above, I search the words string for the smaller string "two" and indexOf() gives me the index of the letter "t". If indexOf() doesn't find anything it will return -1 (as in the case of line 9).
I seem to recall using this in some unexpected places. I'll see if I can dig up an example over the next few days.
By Zevan | February 25, 2009
Actionscript:
-
var txt:TextField = TextField(addChild(new TextField()));
-
txt.text = "";
-
txt.width = 190;
-
txt.height = 400;
-
txt.multiline = true;
-
-
var count:int = 1;
-
function render():void{
-
var line = int(count).toString(2);
-
while(line.length <31){
-
line = "0" + line;
-
}
-
txt.appendText(line + "\n");
-
txt.scrollV= txt.maxScrollV;
-
}
-
-
addEventListener(Event.ENTER_FRAME, onCountUp);
-
function onCountUp(evt:Event):void {
-
count *= 2;
-
render();
-
if (count ==0x40000000){
-
removeEventListener(Event.ENTER_FRAME, onCountUp);
-
addEventListener(Event.ENTER_FRAME, onCountDown);
-
}
-
}
-
function onCountDown(evt:Event):void {
-
count /= 2;
-
render();
-
if (count ==1){
-
addEventListener(Event.ENTER_FRAME, onCountUp);
-
removeEventListener(Event.ENTER_FRAME, onCountDown);
-
}
-
}
The above animates a zig zag pattern in a TextField.
By Zevan | February 24, 2009
Actionscript:
-
var input:String = "a, b, c";
-
-
var words:Array = input.split(", ");
-
var max:String = "";
-
var maxD:String = (words.length - 1).toString();
-
for (var i:int = 0; i<words.length; i++){
-
max += maxD;
-
}
-
var maxInt:int = parseInt(max,words.length);
-
-
for(i = 0; i<=maxInt; i++){
-
var indices:String = i.toString(words.length);
-
var r:String = "";
-
var k:int=0;
-
for (var j:int = 0; j<indices.length; j++){
-
r += words[parseInt(indices.charAt(j))] +" ";
-
k++;
-
}
-
while(k <words.length) {
-
r = words[0] +" "+ r;
-
k++;
-
}
-
trace(r);
-
}
-
trace(i, " variations");
Like many things on this site, I coded this rather quickly and it can probably be cleaned up...
Setting the input variable of the above snippet to "a, b" will output:
a a
a b
b a
b b
4 variations
Setting the input to "a, b, c" will output:
a a a
a a b
a a c
a b a
a b b
a b c
a c a
a c b
a c c
b a a
b a b
b a c
b b a
b b b
b b c
b c a
b c b
b c c
c a a
c a b
c a c
c b a
c b b
c b c
c c a
c c b
c c c
27 variations
I created this to work with words... inputs like "bread, breath, blobs, backwards". Be careful because you can quickly get millions of outputs:
1 to the power of 1 is 1
2 to the power of 2 is 4
3 to the power of 3 is 27
4 to the power of 4 is 256
5 to the power of 5 is 3125
6 to the power of 6 is 46,656
7 to the power of 7 is 823,543
8 to the power of 8 is 16,777,216
etc...
By Zevan | February 23, 2009
Actionscript:
-
g=graphics;
-
mt=g.moveTo;
-
lt=g.lineTo;
-
ls=g.lineStyle;
-
m=Math;
-
r=m.random;
-
s=m.sin;
-
i=0;
-
o={};
-
function f(e){
-
s=150,x=y=s,z=-s,c=(!i)?addChild(new Bitmap(new BitmapData(s,s))).bitmapData:c;while(i<22500)i++,c.setPixel(i%s,i/s,(i%s|i/s)*mouseX);i=1;
-
}
-
addEventListener("enterFrame",f);
Have to link to this very fun new contest:
http://tweetcoding.machine501.com/
http://gskinner.com/playpen/tweetcoding.html
I may just have to get a twitter account...
Posted in misc | Tagged actionscript, flash |