In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

flash on 2009-2-5

A canvas with boxes on a grid that turn on and off
Get Adobe Flash player
by orazal 05 Feb 2009
package
{
  
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.text.TextFieldAutoSize;
  import flash.display.SimpleButton;
  import flash.events.MouseEvent;
  
  /**
   * A canvas with boxes on a grid that turn on and off
   */
  public class GridCanvas extends Sprite
  {
  
    
    // Click on get results, copy the array on the box as saved string to display it on canvas at startup
    private var saved:String = "1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,0,0,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1";
    
    // Configure box and canvas size
    private static var BOX_SIZE:Number = 21;
    private static var ROWS:Number = 15;
    private static var COLUMNS:Number = 20;

    
    private var button:TextField;
    private var output:TextField;
    private var grid:Sprite;
    private var boxList:Array /* of Box */;
    private var rows:Number;
    private var columns:Number;
    private var valuesList:Array /* of Boolean */;
    
    /**
     * Constructor
     */
    public function GridCanvas()
    {
      this.x = this.y = 20;
      
      // Output
      output = new TextField();
      output.x = 70;
      output.width = 300;
      output.height = 20;
      output.border = true;
    
      output.text = "GridSketch";
      addChild(output);
      
      // Button
      button = new TextField();
      button.width = 65;
      button.height = 20;
      button.background = true;
      button.backgroundColor = 0x000000;
      button.textColor = 0xFFFFFF;
      button.selectable = false;
      button.text = "Get Results";
      addChild(button);
      button.addEventListener(MouseEvent.CLICK, retrieveValues);
      
      // Grid container
      grid = new Sprite();
      grid.y = 40;
      addChild(grid);
      
      buildCanvas(saved);
    
    }
    
    /**
     * Builds canvas 
     */
    private function buildCanvas(values:String):void
    {
        if(values)
        {
            valuesList = values.split(",");
        }
      output.text = valuesList.toString();
      // Grid properties
      boxList = [];
      
      // Boxes in grid
      var col:Number;
      var row:Number;
      var limit:Number = COLUMNS;
      for (var i:Number = 0; i < ROWS*COLUMNS; i++)
      {
        var b:Box = new Box(BOX_SIZE);

        // Draw saved array
        if(valuesList)
        {
            
            if (valuesList[i] == "1")
            {
              b.checked = true;
            }
        }
        
        col = i%limit;
        row = Math.floor(i/limit);
        b.x = col * BOX_SIZE;
        b.y = row * BOX_SIZE;
        grid.addChild(b);
        boxList.push(b);
      }
      
    }
    

    /**
     * Retrieves screen values and stores it in array type string
     * that is displayed in output text
     */
    private function retrieveValues(e:MouseEvent):void
    {
      valuesList = [];
      for (var p:String in boxList)
      {
        valuesList.push(Number(Box(boxList[p]).checked));
      }
      output.text = valuesList.toString();
    }
    

  };

}

    import flash.display.Sprite;
    import flash.display.Graphics;
    import flash.events.MouseEvent;

  class Box extends Sprite{
    private var _checked:Boolean;
    private var size:Number;
    
    /**
     * Constructor
     * @param size The size of the bo in pixels
     */
    public function Box(size:Number) {
      this.size = size;
      uncheck();
      this.mouseEnabled = true;
      this.addEventListener(MouseEvent.CLICK, clickHandler);
    }
    /**
     * Handles click events
     * @param event
     */
    private function clickHandler(event:MouseEvent):void
    {
      if (_checked)
      {
        uncheck();
      }
      else
      {
        check();
      }
    }
    /**
     * Changes the graphic state of the box instance.
     */
    private function check():void
    {
      _checked = true;
      this.graphics.clear();
      this.graphics.lineStyle(1, 0x000000, 1);
      this.graphics.beginFill(0x000000, 1);
      this.graphics.drawRect(0, 0, size, size);
      this.graphics.endFill();
    }
    private function uncheck():void
    {
      _checked = false;
      this.graphics.clear();
      this.graphics.clear();
      this.graphics.lineStyle(1, 0x000000, 1);
      this.graphics.beginFill(0xFFFFFF, 1);
      this.graphics.drawRect(0, 0, size, size);
      this.graphics.endFill();
    }
    
    /**
     * Accessor & Mutator functions
     */
    public function get checked():Boolean
    {
      return _checked;
    }
    
    public function set checked(value:Boolean):void
    {
      if (value)
      {
        check();
      }
      else
      {
        uncheck();
      }
    }
    
  }