Monthly Archives: November 2008

Toggle .visible

Actionscript:
  1. // toggle a DisplayObject's visible property
  2. var shape = new Shape();
  3.  
  4. shape.visible = !shape.visible;
  5. trace(shape.visible); // outputs false
  6.  
  7. shape.visible = !shape.visible;
  8. trace(shape.visible); // outputs true
  9.  
  10. shape.visible = !shape.visible;
  11. trace(shape.visible); // outputs false
  12.  
  13. shape.visible = !shape.visible;
  14. trace(shape.visible);  // outputs true

This is pretty obvious to anyone with a full understanding of the ! operator. It's useful for things
like checkboxes and other types of toggle buttons. The first time I ever encountered this technique
was in processing source... in one of the demos on toxi.co.uk

Here's another quick example you can run in your timeline:

Actionscript:
  1. var circle:Shape =  new Shape();
  2. circle.graphics.beginFill(0xFF0000);
  3. circle.graphics.drawCircle(0, 0,10);
  4. circle.x = 80;
  5. circle.y = 105;
  6. addChild(circle)
  7.  
  8. var btn:TextField = new TextField();
  9. btn.text = "click this text to toggle red circle's visibility";
  10. btn.x = btn.y = 100;
  11. btn.selectable = false;
  12. btn.border = true;
  13. btn.autoSize = TextFieldAutoSize.LEFT;
  14. addChild(btn);
  15.  
  16. btn.addEventListener(MouseEvent.CLICK, onClick);
  17. function onClick(evt:MouseEvent):void{
  18.     circle.visible = !circle.visible;
  19. }

Because "not false" is true.

Posted in Operators | Leave a comment

Multiple Graphics Class Calls

Actionscript:
  1. // draw red circle
  2. with (graphics) beginFill(0xFF0000), drawCircle(200,100,30);
  3.  
  4. // draw 100 gray circles
  5. with (graphics) for (var i:int = 0; i<100; i++) beginFill(0x666666), drawCircle(Math.random()*200, Math.random()*200, Math.random()*10), endFill();
  6.  
  7. // draw a few lines
  8. with (graphics) lineStyle(0, 0x000000), moveTo(10,210), lineTo(20,300), lineTo(30,210), lineTo(40,300), lineTo(50,210), lineTo(60,300);

Sometimes it's tedious to write lines and lines of Graphics class method calls. Using a with statement you can reduce these calls into one long (not very readable) line of code.

More readable example:

Actionscript:
  1. with(graphics) {
  2.     beginFill(0xFF0000);
  3.     drawRect(0,0,100,100);
  4.     endFill();
  5.     beginFill(0xFFFF00);
  6.     drawRect(10,10,80,80);
  7.     endFill();
  8.     beginFill(0x0000FF);
  9.     drawCircle(50,50,40);
  10. }

Posted in Graphics | 2 Comments

Click Listener

Actionscript:
  1. var e:String = "addEventListener";
  2.  
  3. stage[e]("click", function():void{
  4.     trace("clicked", arguments[0]);
  5. });

Strange code to do a click listener. arguments[0] is the event object. (Check out the "warning" page for more info about this).

Actionscript:
  1. stage.addEventListener(MouseEvent.CLICK, onStageClick);
  2. function onStageClick(evt:MouseEvent):void {
  3.     trace("clicked", evt);
  4. }

Normal way.

Posted in Events | 2 Comments

2D Array Map

Actionscript:
  1. var col:Array = [0x000000, 0xCCCCCC, 0xFF0000, 0xCCCC00, 0x000055, 0x00CCCC];
  2.  
  3. var map:Array = new Array();
  4. map[0] = [0,0,0,0,0,0,0,0,0,0];
  5. map[1] = [0,0,0,0,0,0,0,0,0,0];
  6. map[2] = [0,5,4,5,4,5,4,5,4,0];
  7. map[3] = [0,4,4,4,4,4,4,4,4,0];
  8. map[4] = [0,4,0,0,0,0,0,0,4,0];
  9. map[5] = [0,3,2,3,2,3,2,3,2,0];
  10. map[6] = [1,1,1,1,1,1,1,1,1,1];
  11. map[7] = [0,2,2,2,2,2,2,2,2,0];
  12. map[8] = [1,1,1,1,1,1,1,1,1,1];
  13.  
  14. for (var i:int = 0; i<map.length; i++){
  15.     for (var j:int = 0; j<map[i].length; j++){
  16.         graphics.beginFill(col[map[i][j]]);
  17.         graphics.drawRect(j * 10, i * 10, 10, 10);
  18.     }
  19. }

Use a 2D array to draw a map of colored rectangles. Lots of stuff you can do with this - like adding game tiles instead of drawing colored rects:

Actionscript:
  1. // add clip with multiple frames - each containing a tile graphic
  2. var tile:MovieClip = TileClip();
  3. tile.gotoAndStop(map[i][j]+1);
  4. addChild(tile);

Posted in Graphics, arrays | Leave a comment