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

ライフゲーム

Get Adobe Flash player
by resense 19 Mar 2009
    Embed
package {
  
  import flash.display.*;
  import flash.events.*;
  import flash.geom.Rectangle;
  import flash.external.ExternalInterface;

  public class sk003 extends Sprite {
    private var image_:Bitmap;
    private var sw_:uint,sh_:uint;
    private var world_:Array;

    private var bmpdata_:BitmapData;

    public function _trace(str:*):void {
      ExternalInterface.call('console.log',str);
    }
    public function sk003() {
      addEventListener(Event.ADDED_TO_STAGE, added_to_stage);
    }
    public function added_to_stage(evt:Event):void {
      //
      sw_ = stage.stageWidth/5;
      sh_ = stage.stageHeight/5;
      world_ = new Array(sw_);
      for(var x:uint=0;x<sw_;x++) {
        world_[x] = new Array(sh_);
        for(var y:uint=0;y<sh_;y++) 
          world_[x][y] = [0,0];
      }
      // bitmap
      bmpdata_ = new BitmapData(sw_,sh_,false);
      var bmp:Bitmap = new Bitmap(bmpdata_);
      bmp.width = stage.stageWidth;
      bmp.height = stage.stageHeight;
      addChild(bmp);

      // seed
      for(var i:uint = 0; i<sw_*sh_*0.6; i++) {
        var px:uint = Math.floor(sw_*Math.random());
        var py:uint = Math.floor(sh_*Math.random());
        world_[px][py][1] = 1;
        bmpdata_.setPixel(px,py,0xffffff);
      }
      //
      addEventListener(Event.ENTER_FRAME, loop);
    }
    public function loop(evt:Event):void {
      bmpdata_.fillRect(new Rectangle(0,0,sw_,sh_),0x000000);
      for(var x:uint=0;x<sw_;x++) {
        for(var y:uint=0;y<sh_;y++) {
          if((world_[x][y][1] == 1) || (world_[x][y][1] == 0 && world_[x][y][0] == 1)) {
            world_[x][y][0] = 1;
            bmpdata_.setPixel(x,y,0xfffffff);
          }
          if(world_[x][y][1] == -1) {
            world_[x][y][0] = 0;
          }
          world_[x][y][1] = 0;
        }
      }
      //
      for(var x2:uint=0;x2<sw_;x2++) {
        for(var y2:uint=0;y2<sh_;y2++) {
          var count:uint = neighbors(x2,y2);
          if(count == 3 && world_[x2][y2][0] == 0) {
            world_[x2][y2][1] = 1;
          }
          if((count < 2 || count > 3) && world_[x2][y2][0] == 1) {
            world_[x2][y2][1] = -1;
          }
        }
      }
    }
    private function neighbors(x:uint,y:uint):uint {
      return world_[(x + 1) % sw_][y][0] +
        world_[x][(y + 1) % sh_][0] +
        world_[(x + sw_ - 1) % sw_][y][0] +
        world_[x][(y + sh_ - 1) % sh_][0] +
        world_[(x + 1) % sw_][(y + 1) % sh_][0] +
        world_[(x + sw_ - 1) % sw_][(y + 1) % sh_][0] +
        world_[(x + sw_ - 1) % sw_][(y + sh_ - 1) % sh_][0] +
        world_[(x + 1) % sw_][(y + sh_ - 1) % sh_][0];
    }
  }
}