Author Archives: Zevan

Multiple Keys

CLICK HERE TO COPY
Actionscript:

var key:Object = new Object();

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);

stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);

function onKeyPressed(evt:KeyboardEvent):void {

    key[evt.keyCode] = true;

    key.keyCode = evt.keyCode;

}

function onKeyReleased(evt:KeyboardEvent):void {

    key[evt.keyCode] = false

}

 

// example

 

addEventListener(Event.ENTER_FRAME, onLoop);

function onLoop(evt:Event):void {

   

    //trace(key.keyCode);

     

    if (key[Keyboard.LEFT]){

        trace("left");

    }

   

    if (key[Keyboard.RIGHT]){

        trace("right");

    [...]

Posted in associative arrays, keys | Tagged , | 4 Comments

String.replace()

CLICK HERE TO COPY
Actionscript:

var words:String = "ActionSnippet.com is a website. ActionSnippet.com is a blog.";

 

// outputs: "ActionSnippet.com is a website. ActionSnippet.com is a blog.";

trace(words);

 

// the "/g" tells it to replace all instances of ActionSnippet.com

words = words.replace(/ActionSnippet.com/g, "SomeWebsite.com");

 

// outputs: SomeWebsite.com is a website. SomeWebsite.com is a blog.

trace(words);

Just a quick response to a student question.

Posted in string manipulation, strings | Tagged , , | Leave a comment

Recursive Tree

CLICK HERE TO COPY
Actionscript:

var branches:int = 0;

var maxBranches:int = 400;

 

graphics.lineStyle(0,0x000000);

 

makeBranch(300,350,100,-45,45);

     

function makeBranch(xp:Number, yp:Number, leng:Number, min:Number, max:Number):void {

 

    var endX:Number, endY:Number;

    var theta:Number = (min + Math.random()*(max-min) - 90) * Math.PI / 180;

     

    endX = xp + leng * Math.cos(theta);

    endY = yp + leng * Math.sin(theta);

 

    graphics.moveTo(xp, [...]

Posted in Graphics, functions | Tagged , , , | Leave a comment

3D Shape

CLICK HERE TO COPY
Actionscript:

stage.frameRate = 30;

var centerX:Number = 200, centerY:Number = 200, zpos:Number, xpos:Number, ypos:Number, depth:Number;

var rotX:Number = 0, rotY:Number = 0, px:Number, py:Number, pz:Number;

var cosx:Number, cosy:Number, sinx:Number, siny:Number;

 

var canvas:BitmapData = new BitmapData(400,400,true,0xFF000000);

addChild(new Bitmap(canvas));

 

addEventListener(Event.ENTER_FRAME, onLoop);

 

function onLoop(evt:Event):void {

    canvas.fillRect(canvas.rect, 0xFF000000);

     rotX += (mouseX / 50 - rotX)/12;

     rotY += (mouseY / 50 - [...]

Posted in BitmapData, setPixel | Tagged , , | 5 Comments