Author Archives: Zevan

Square Bracket Syntax & Typecasting

CLICK HERE TO COPY
Actionscript:

addChild(new TextField());

getChildAt(0)["text"] = "Hello World";

Notice that square bracket syntax makes it so we don't need to typecast. Take a look at the other way:
CLICK HERE TO COPY
Actionscript:

addChild(new TextField());

TextField(getChildAt(0)).text = "Hello World";

Typecasting is the way to go.... the square bracket technique is just an interesting trick.
I wrote this after somehow stumbling on this [...]

Posted in dynamic, misc | Tagged , | Leave a comment

Snap to Grid

CLICK HERE TO COPY
Actionscript:

[SWF(width = 600, height = 400)]

 

// draw the same grid as yesterday

var tileSize:int = 40;

var cols:int = stage.stageWidth / tileSize;

var rows:int = stage.stageHeight / tileSize;

var grid:Sprite = Sprite(addChild(new Sprite()));

grid.graphics.lineStyle(0,0x000000);

var i:int = 0;

for (i = 1; i<cols; i++){

    var posX:Number = i * tileSize

    grid.graphics.moveTo(posX, 0);

    grid.graphics.lineTo(posX, stage.stageHeight);

}

for (i = [...]

Posted in Graphics, UI, motion | Tagged , | Leave a comment

Stage Grid

CLICK HERE TO COPY
Actionscript:

// try changing the size of  the stage

[SWF(width = 600, height = 400)]

 

// try changing the tile size (make sure swf width and height are evenly divisible by this number)

var tileSize:int = 20;

 

 

var cols:int = stage.stageWidth / tileSize;

var rows:int = stage.stageHeight / tileSize;

 

var grid:Sprite = Sprite(addChild(new Sprite()));

grid.graphics.lineStyle(0,0x000000);

 

var i:int = 0;

 

for (i = [...]

Posted in Graphics | Tagged , | Leave a comment

Tausworthe Random Numbers (seeded)

CLICK HERE TO COPY
Actionscript:

// best thing about this are the random seeds

var s1:Number= 0xFFFFFF;

var s2:Number = 0xCCCCCC;

var s3:Number= 0xFF00FF;

 

// saw this algorithm on this somewhat annoying thread:

// http://www.reddit.com/r/programming/comments/7yjlc/why_you_should_never_use_rand_plus_alternative/

// additional googling brought me to this: http://wakaba.c3.cx/soc/kareha.pl/1100499906/

// and finally to this ( didn't understand most of this one) 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);

  [...]

Posted in Number, misc | Tagged , | 2 Comments