Monthly Archives: November 2008

Isometric Box

Actionscript:
  1. stage.frameRate = 30;
  2.  
  3. for (var i:int = 0; i<100; i++){
  4.     makeBoxSegment(200, 200 - i, i * 2);
  5. }
  6.  
  7. function makeBoxSegment(xp:Number, yp:Number, col:uint):Sprite {
  8.     var isoBox:Sprite = Sprite(addChild(new Sprite()));
  9.     with (isoBox) scaleY = .5, y = yp, x = xp;
  10.     var box:Shape = Shape(isoBox.addChild(new Shape()));
  11.     box.rotation = 45;
  12.     with (box.graphics) beginFill(col), drawRect(-50,-50,100,100);
  13.     isoBox.addEventListener(Event.ENTER_FRAME, onRotate);
  14.     return isoBox;
  15. }
  16.  
  17. function onRotate(evt:Event):void {
  18.     evt.currentTarget.getChildAt(0).rotation = mouseX;
  19. }

An isometric box that rotates with the mouseX.

Posted in DisplayObject, Graphics | Tagged , , , | 2 Comments

Multiple Keys

Actionscript:
  1. var key:Object = new Object();
  2. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  3. stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
  4. function onKeyPressed(evt:KeyboardEvent):void {
  5.     key[evt.keyCode] = true;
  6.     key.keyCode = evt.keyCode;
  7. }
  8. function onKeyReleased(evt:KeyboardEvent):void {
  9.     key[evt.keyCode] = false
  10. }
  11.  
  12. // example
  13.  
  14. addEventListener(Event.ENTER_FRAME, onLoop);
  15. function onLoop(evt:Event):void {
  16.    
  17.     //trace(key.keyCode);
  18.      
  19.     if (key[Keyboard.LEFT]){
  20.         trace("left");
  21.     }
  22.    
  23.     if (key[Keyboard.RIGHT]){
  24.         trace("right");
  25.     }
  26.    
  27.     // keys #1, #2 and #3 are down
  28.     if (key[49] && key[50] && key[51]){
  29.         trace("one two thee");
  30.     }
  31.    
  32.     // keys #6, #7, #8 and #9 keys are down
  33.     if (key[54] && key[55] && key[56] && key[57]){
  34.         trace("six seven eight nine");
  35.     }
  36. }

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.

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

String.replace()

Actionscript:
  1. var words:String = "ActionSnippet.com is a website. ActionSnippet.com is a blog.";
  2.  
  3. // outputs: "ActionSnippet.com is a website. ActionSnippet.com is a blog.";
  4. trace(words);
  5.  
  6. // the "/g" tells it to replace all instances of ActionSnippet.com
  7. words = words.replace(/ActionSnippet.com/g, "SomeWebsite.com");
  8.  
  9. // outputs: SomeWebsite.com is a website. SomeWebsite.com is a blog.
  10. trace(words);

Just a quick response to a student question.

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

Recursive Tree

Actionscript:
  1. var branches:int = 0;
  2. var maxBranches:int = 400;
  3.  
  4. graphics.lineStyle(0,0x000000);
  5.  
  6. makeBranch(300,350,100,-45,45);
  7.      
  8. function makeBranch(xp:Number, yp:Number, leng:Number, min:Number, max:Number):void {
  9.  
  10.     var endX:Number, endY:Number;
  11.     var theta:Number = (min + Math.random()*(max-min) - 90) * Math.PI / 180;
  12.      
  13.     endX = xp + leng * Math.cos(theta);
  14.     endY = yp + leng * Math.sin(theta);
  15.  
  16.     graphics.moveTo(xp, yp);
  17.     graphics.lineTo(endX, endY);
  18.    
  19.     if (branches <maxBranches) {
  20.         var newLength:Number = leng*.7;
  21.         setTimeout(makeBranch, 0, endX, endY, newLength, -90, 0);
  22.         setTimeout(makeBranch, 0, endX, endY, newLength, 0, 90);
  23.     }
  24.     branches+=2;
  25. }

Draws a tree using recursion.

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