Category Archives: OOP

Snippet Template (imports)

Actionscript:
  1. package {
  2.     import adobe.utils.*;
  3.     import flash.accessibility.*;
  4.     import flash.display.*;
  5.     import flash.errors.*;
  6.     import flash.events.*;
  7.     import flash.external.*;
  8.     import flash.filters.*;
  9.     import flash.geom.*;
  10.     import flash.media.*;
  11.     import flash.net.*;
  12.     import flash.printing.*;
  13.     import flash.profiler.*;
  14.     import flash.sampler.*;
  15.     import flash.system.*;
  16.     import flash.text.*;
  17.     import flash.ui.*;
  18.     import flash.utils.*;
  19.     import flash.xml.*;
  20.    
  21.     dynamic public class Snippet extends MovieClip {
  22.         public function Snippet() {
  23.              // paste your snippet here (functions and all)
  24.         }
  25.     }
  26. }

This snippet imports all flash packages and is dynamic... you can copy actionsnippet code into the constructor of this file if you use Flex, FlashDevelop, TextMate etc... I tested it with a bunch of snippets and it seems to work nicely.

When I first teach classes in AS3 this is the template I use:

Actionscript:
  1. package{
  2.     import flash.display.*;
  3.     import flash.events.*;
  4.      
  5.     public class Main extends Sprite{
  6.         // etc...
  7.     }
  8. }

Display and events cover a lot of ground..... next one I find myself adding is flash.geom, followed by flash.net... I'd say those are my top 4 most frequently used packages.... I do lots of text layout in the Flash IDE, otherwise flash.text would be in there....

Also posted in dynamic, misc, timeline | Tagged , | 4 Comments

Static Stage and More…

Actionscript:
  1. package {
  2.     import flash.display.*;
  3.  
  4.     public class Main extends MovieClip{
  5.    
  6.         private static var _display:MovieClip;
  7.         private static var _stage:Stage;
  8.        
  9.         // use getters instead of public static vars so they
  10.         // cannot be reset outside of Main
  11.         public static function get display():MovieClip{
  12.             return _display;
  13.         }
  14.        
  15.         public static function get stage():Stage{
  16.             return _stage;
  17.         }
  18.        
  19.         public function Main(){
  20.            Main._display = this;
  21.            Main._stage = stage;
  22.            
  23.            // test out the static references
  24.            var t:Test = new Test();
  25.         }
  26.     }
  27. }
  28.  
  29. class Test{
  30.     public function Test(){
  31.         // test out the static references
  32.         with(Main.display.graphics){
  33.             lineStyle(1, 0xFF0000);
  34.             for (var i:int = 0; i<100; i++){
  35.                 lineTo(Math.random() * Main.stage.stageWidth,
  36.                        Math.random() * Main.stage.stageHeight);
  37.             }
  38.         }
  39.     }
  40. }

This snippet creates two private static variables that reference the stage and the main timeline/document class. It then uses getters to regulate the use of these static vars so that they cannot be reset from outside the Main class.

Sometimes the amount of extra coding you need to do to maintain a valid stage reference can be cumbersome... similarly, passing references of the document class all around your app can be annoying. If you don't mind using two global vars in your app... this trick can come in handy.

What's nice about using getters here is that if someone tries to do this:

Actionscript:
  1. Main.display = new MovieClip();

they'll get an error... in flex, you even see that little red ex pop up next to this line of code if you write it :) ... that wouldn't happen with a public static var....

Also posted in DisplayObject, variables | Tagged , , | Leave a comment

Not a Snippet (wandering robots)

Actionscript:
  1. package {
  2.     import flash.display.Sprite;
  3.    
  4.     [SWF(width=500, height=500, backgroundColor=0xEFEFEF, frameRate=30)]
  5.     public class Main extends Sprite{
  6.         private var _robotManager:RobotManager;
  7.         public function Main(){
  8.           _robotManager = new RobotManager(this, 12);
  9.         }
  10.     }
  11. }
  12.  
  13. class RobotManager{
  14.    
  15.     private var _robots:Array;
  16.     private var _top:Sprite;
  17.     private var _robotNum:int;
  18.    
  19.     public function RobotManager(top:Sprite, robotNum:int){
  20.         _top = top;
  21.         _robotNum = robotNum;
  22.         _robots = new Array();
  23.         setupRobots();
  24.         top.addEventListener(Event.ENTER_FRAME, onRun);
  25.     }
  26.    
  27.     private function setupRobots():void{
  28.         for (var i:int = 0; i<_robotNum; i++){
  29.             _robots[i] = new Robot();
  30.             _robots[i].x = 100+(i % 3) * 150;
  31.             _robots[i].y = 100+int(i / 3) * 100;
  32.             _robots[i].addEventListener("death", onRobotDie);
  33.             _top.addChild(_robots[i]);
  34.         }
  35.     }
  36.     private function onRobotDie(evt:Event):void{
  37.         for (var i:int = 0; i<_robotNum; i++){
  38.             if (_robots[i] == evt.currentTarget){
  39.                 _robots.splice(i, 1);
  40.             }
  41.         }
  42.         _robotNum--;
  43.     }
  44.    
  45.     private function onRun(evt:Event):void{
  46.         collisions();
  47.     }
  48.    
  49.     private function collisions():void{
  50.         var currBot:Robot;
  51.         for (var i:int = 0; i<_robotNum; i++){
  52.             currBot = _robots[i];
  53.             for (var j:int = 0; j<_robotNum; j++){
  54.                 if (currBot != _robots[j]){
  55.                     var dx:Number = currBot.x - _robots[j].x;
  56.                     var dy:Number = currBot.y - _robots[j].y;
  57.                     if (Math.sqrt((dx * dx) + (dy * dy)) <40){
  58.                         currBot.die();
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66. import flash.display.*
  67. import flash.events.*;
  68.  
  69. class Robot extends Sprite{
  70.     private var _inc:Number
  71.     private var _td:Number;
  72.     private var _theta:Number;
  73.     private var _rad:Number;
  74.    
  75.     public function Robot(){
  76.         _theta = Math.random() * (Math.PI * 2);
  77.         _td = _theta;
  78.         _inc = Math.random() * .2 + .05;
  79.         _rad = _inc * 10;
  80.         with(graphics){
  81.             beginFill(0x666666);
  82.             drawRect(-10,-10,20,20);
  83.             endFill();
  84.             beginFill(0x333333);
  85.             drawRect(-5, -5, 20, 10);
  86.             lineStyle(0,0xAAAAAA);
  87.             endFill();
  88.             drawCircle(0,0,20);
  89.         }
  90.         addEventListener(Event.ENTER_FRAME, onRun);
  91.     }
  92.    
  93.     private function onRun(evt:Event):void{
  94.         _td  += _inc;
  95.         _theta += _inc * Math.cos(_td);
  96.         if (x <0 || y <0 || x> 500 || y> 500){
  97.             _theta += Math.PI;
  98.         }
  99.         x += _rad * Math.cos(_theta);
  100.         y += _rad * Math.sin(_theta);
  101.         rotation = _theta / Math.PI * 180;
  102.     }
  103.    
  104.     public function die():void{
  105.         removeEventListener(Event.ENTER_FRAME, onRun);
  106.         addEventListener(Event.ENTER_FRAME, onDie);
  107.     }
  108.    
  109.     private function onDie(evt:Event):void{
  110.         scaleX = scaleY += .1;
  111.         alpha -= .1;
  112.         if (scaleX> 2){
  113.             removeEventListener(Event.ENTER_FRAME, onDie);
  114.             if (parent){
  115.                 parent.removeChild(this);
  116.                 dispatchEvent(new Event("death"));
  117.             }
  118.         }
  119.     }
  120. }

This code should be placed in a document class... not the timeline (hopefully you already know that)

The above creates 12 wandering robots that die when they collide with one another.

This is not a snippet really... but it's only slightly over 100 lines and it contains a few tricks I find myself using from time to time.

You can view the swf here.

Also posted in Events, arrays, motion | Tagged , , | 3 Comments