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 2010-7-27

Originally 
I intend for this to be a Lights Out game
User should be able to pick size
Circles will be randomly placed in vis
Goal is to get rid of lights
Game Mechanice: click on cell
toggles the lighting of all orthogonally adjacent cells
Get Adobe Flash player
by dvjc 28 Jul 2010
    Embed
/**
 * Copyright dvjc ( http://wonderfl.net/user/dvjc )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/1vbs
 */

// Originally forked from wondermame's kick all balls out of blue zone; 青マスから玉を追い出してください

// I intend for this to be a Lights Out game
// User should be able to pick size
// Circles will be randomly placed in vis
// Goal is to get rid of lights
// Game Mechanice: click on cell
// toggles the lighting of all orthogonally adjacent cells

package
{
  import flash.events.Event
  import flash.events.MouseEvent;
  import flash.display.Sprite;
  import flash.display.Graphics;

  public class main extends Sprite
  {
    private var map:Object = new Object();
    private var size:int = 0;
    private var count:int;
    private var prev_scale:Number, current_scale:Number;
    private var balls:Array=[];

    public function main()
    {
      // ensures a square board, placed at top/left corner
      stage.align = "TL";
      stage.scaleMode = "noScale";
      scaleX = stage.stageWidth / 5;
        
      // draw board
      createBoard( 5 );

      addEventListener(Event.ENTER_FRAME, animate);
    }

    private function toggleOrthogonalAdjacents 
    (
        x:int
        , y:int
        , b:Ball
    )
    :void
    {
      // current ball
      var id0:String = "" + x + "," + y;
      // new ball position - to right
      var x1:int = x + 1;
      var id1:String = "" + x1 + "," + y;
      // new ball position - one row down
      var y1:int = y + 1;
      var id2:String = "" + x + "," + y1;
      // if the two new positions are emtpy, continue
      if (map[id1] == null && map[id2] == null)
      {
        // delete the clicked ball
        delete map[id0];
        // turn on the two new positions
        map[id1] = map[id2] = true;
        // extend grid if might go off-grid
        if (x + 2 == size || y + 2 == size) extend();
      }
      else
      {
        // new positions unavailable, flutters the clicked
        b.flutter(map[id1] ? 1 : 0, map[id2] ? 1 : 0);
      }
        
    }

    private function split(x:int, y:int, b:Ball):void
    {
      // current ball
      var id0:String = "" + x + "," + y;
      // new ball position - to right
      var x1:int = x + 1;
      var id1:String = "" + x1 + "," + y;
      // new ball position - one row down
      var y1:int = y + 1;
      var id2:String = "" + x + "," + y1;

      // if the two new positions are emtpy, continue
      if (map[id1] == null && map[id2] == null)
      {
        // delete the clicked ball
        delete map[id0];
        // turn on the two new positions
        map[id1] = map[id2] = true;
        // extend grid if might go off-grid
        if (x + 2 == size || y + 2 == size) extend();
        // removes the clicked ball from vis
        removeChild(b);
        // adds the new balls to the stage
        addChild(new Ball(x1, y, 1, 0, toggleOrthogonalAdjacents));
        addChild(new Ball(x, y1, 0, 1, toggleOrthogonalAdjacents));
      }
      else
      {
        // new positions unavailable, flutters the clicked
        b.flutter(map[id1] ? 1 : 0, map[id2] ? 1 : 0);
      }
    }

    private function createBoard
    (
        sizeOfBoard:int=5
    )
    :void
    {
        // create cells of which the board is comprised
        for( var i:int=0; i<sizeOfBoard; i++ )
        {
            addChild (new Cell(sizeOfBoard, i));
            balls[ sizeOfBoard + ":" + i ] = new Ball(sizeOfBoard, i, 0, 0, toggleOrthogonalAdjacents);
            addChild( balls[ sizeOfBoard + ":" + i ]);

            addChild( new Cell(i, sizeOfBoard));
            balls[ i + ":" + sizeOfBoard ] = new Ball(i, sizeOfBoard, 0, 0, toggleOrthogonalAdjacents);
            addChild( balls[ i + ":" + sizeOfBoard ]);
        }
        addChild( new Cell( sizeOfBoard, sizeOfBoard) );
        balls[ sizeOfBoard + ":" + sizeOfBoard ] = new Ball(sizeOfBoard, sizeOfBoard, 0, 0, toggleOrthogonalAdjacents);
        addChild( balls[ sizeOfBoard + ":" + sizeOfBoard ]);
        
        var sw:int;
        var sh:int;
        var scale:Number;
        
        // scale out stage
        for( var j:int=0; j<sizeOfBoard; j++ )
        {
            sw = stage.stageWidth;
            sh = stage.stageHeight;
            prev_scale = scaleX;
            current_scale = (sw<sh?sw:sh)/(sizeOfBoard + 0.125);
            scale = current_scale*(3-sizeOfBoard-j)/4 + (prev_scale)*(sizeOfBoard-j-1)/4;
            scaleX = scaleY = scale;
        }
    }


    private function extend():void
    {
      // adds a new row and column of cells
      for (var i:int = 0; i < size; i++)
      {
        addChild(new Cell(size, i));
        addChild(new Cell(i, size));
      }
      // adds a new corner cell (bottom right)
      addChild(new Cell(size, size));
      // updates the size of the swf
      size++;
      
      var sw:int = stage.stageWidth;
      var sh:int = stage.stageHeight;
      
      prev_scale = scaleX;
      current_scale = (sw < sh ? sw : sh) / (size - 0.5);
      count = 4;
    }

    private function animate(e:Event):void
    {
      if (count >= 0)
      {
        var scale:Number;
        scale = current_scale + (prev_scale - current_scale) * count / 4;
        scaleX = scaleY = scale;
        count--;
      }
    }
  }
}

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

class Cell extends Sprite {
  public function Cell(_x:int, _y:int) {
    var g:Graphics = this.graphics;
    x = _x;
    y = _y;
    g.beginFill(0xffffff);
    g.drawRect(0, 0, 1, 1);
    g.endFill();
    g.beginFill(_x + _y <= 2 ? 0x3333cc : 0x333333);
    g.drawRect(0.1, 0.1, 0.9, 0.9);
    g.endFill();
  }
}

class Ball extends Sprite
{
  private var lx:int, ly:int, dx:int, dy:int;
  private var split:Function;
  private var count:int;
  private var fluttering:Boolean = false;

  public function Ball(_lx:int, _ly:int, _dx:int, _dy:int, _split:Function)
  {
    lx = _lx; dx = _dx; x = lx - dx;
    ly = _ly; dy = _dy; y = ly - dy;
    split = _split; count = 4;

    var g:Graphics = this.graphics;
    if(true){}
    g.beginFill(0xffff66);
    g.drawCircle(0.55, 0.55, 0.4);
    g.endFill();

    addEventListener(Event.ENTER_FRAME, animate);
    addEventListener(MouseEvent.CLICK, on_click);
    buttonMode = true;
    useHandCursor = true;
  }

  public function flutter(_dx:int, _dy:int):void
  {
    count = 8;
    dx = _dx; dy = _dy;
    fluttering = true;
    addEventListener(Event.ENTER_FRAME, animate);
  }

  private function on_click(e:MouseEvent):void
  {
    split(lx, ly, this);
  }

  private function animate(e:Event):void 
  {
    if (fluttering)
    {
      var s1:Number = (4 - Math.abs(4 - count)) / 64;
      var s2:Number = count * count / 128;
      x = lx + dx * s1 + (Math.random() - 0.5) * s2;
      y = ly + dy * s1 + (Math.random() - 0.5) * s2;
    }
    else
    {
      x = lx - dx * count * count / 16;
      y = ly - dy * count * count / 16;
    }
    if (count-- == 0) 
    {
      removeEventListener(Event.ENTER_FRAME, animate);
    }
  }
}