By Zevan | November 30, 2008
Actionscript:
-
stage.frameRate = 30;
-
-
for (var i:int = 0; i<100; i++){
-
makeBoxSegment(200, 200 - i, i * 2);
-
}
-
-
function makeBoxSegment(xp:Number, yp:Number, col:uint):Sprite {
-
var isoBox:Sprite = Sprite(addChild(new Sprite()));
-
with (isoBox) scaleY = .5, y = yp, x = xp;
-
var box:Shape = Shape(isoBox.addChild(new Shape()));
-
box.rotation = 45;
-
with (box.graphics) beginFill(col), drawRect(-50,-50,100,100);
-
isoBox.addEventListener(Event.ENTER_FRAME, onRotate);
-
return isoBox;
-
}
-
-
function onRotate(evt:Event):void {
-
evt.currentTarget.getChildAt(0).rotation = mouseX;
-
}
An isometric box that rotates with the mouseX.
By Zevan | November 29, 2008
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");
-
}
-
-
// keys #1, #2 and #3 are down
-
if (key[49] && key[50] && key[51]){
-
trace("one two thee");
-
}
-
-
// keys #6, #7, #8 and #9 keys are down
-
if (key[54] && key[55] && key[56] && key[57]){
-
trace("six seven eight nine");
-
}
-
}
The first 10 lines of code make up this snippet. This is an easy way to keep track of multiple key presses. For games, this is the only key technique I use ... wrapped up in a Singleton.
By Zevan | November 28, 2008
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.
By Zevan | November 27, 2008
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, yp);
-
graphics.lineTo(endX, endY);
-
-
if (branches <maxBranches) {
-
var newLength:Number = leng*.7;
-
setTimeout(makeBranch, 0, endX, endY, newLength, -90, 0);
-
setTimeout(makeBranch, 0, endX, endY, newLength, 0, 90);
-
}
-
branches+=2;
-
}
Draws a tree using recursion.