flash on 2015-1-18

by Cheshir
♥0 | Line 97 | Modified 2015-01-18 03:06:43 | MIT License
play

ActionScript3 source code

/**
 * Copyright Cheshir ( http://wonderfl.net/user/Cheshir )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/uFUE
 */

package {
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.events.MouseEvent;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        private var universe:Bitmap;
        private var data:BitmapData;
        private var firstGene:uint = 0xf000;
        
        private var scale:int = 10;
        public function FlashTest() {
            // write as3 code here..
            data = new BitmapData(stage.stageWidth/scale, stage.stageHeight/scale,false,0);
            universe = new Bitmap(data); //,'auto',true);
            universe.scaleX = universe.scaleY = scale;
            addChild(universe);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, lifeDraw);
            stage.addEventListener(MouseEvent.MOUSE_UP, lifeStDraw);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, addLife);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, startStopLife);
           // stage.frameRate = 1;
            log('game stop press SPACE to start');
        }
        
        private var isDraw:Boolean = false;
        private function lifeDraw(e:MouseEvent):void{ isDraw = true; }
        private function lifeStDraw(e:MouseEvent):void{ isDraw = false; }
        private function addLife(e:MouseEvent):void {
            if(isDraw){
            if(data.getPixel(e.localX/scale, e.localY/scale) > 0){
                data.setPixel(e.localX/scale, e.localY/scale, 0);
            } else 
            data.setPixel(e.localX/scale, e.localY/scale, firstGene);
            } 
        }
        private var neighbors:int = 0;
        private var newGene:uint = 0;
        private var dat:uint = 0;
        private var coeff:Array = [];    // 
        private function changePoint(x:int, y:int):void{
            //data.getPixel(x,y);    // получена клетка
            // 2 правила нужно осмотреть 8 соседних и поссчитать их количество
            // еще хотелось бы рассчитать новый цвет для следующего поколения
            neighbors = 0;            
            newGene = firstGene;
            coeff = [data.getPixel(x-1,y-1), data.getPixel(x,y-1), data.getPixel(x+1,y-1),
                    data.getPixel(x-1,y), data.getPixel(x+1,y),
                    data.getPixel(x-1,y+1), data.getPixel(x,y+1), data.getPixel(x+1,y+1)];
            dat = data.getPixel(x-1,y-1);
            for(var i:int=0; i<coeff.length; i++){
                if(coeff[i] > 0){
                    neighbors++;
                    if(coeff[i]>newGene){
                        newGene = coeff[i];
                    }
                }
            }

            if(neighbors == 3 && data.getPixel(x,y) == 0 ){
                data.setPixel(x,y,newGene+0xff);    // зарождение
            } else if( data.getPixel(x,y) > 0 && ( neighbors < 2 || neighbors > 4 )) {
                data.setPixel(x,y,0);    // смерть
            }

            // если у клетки меньше 2-х живых соседей - она умирает
            // если у клетки больше 3-х живых соседей - она умирает
            // если 3 соседа - клетка жива...
        }
        private var isGame:Boolean = false;
        private function startStopLife(e:KeyboardEvent):void{
            isGame = !isGame;
            if(e.keyCode == Keyboard.SPACE){
                if(isGame){
                    log('game started');
                    stage.addEventListener(Event.ENTER_FRAME, loop);
                } else {
                    log('game stop press SPACE to start');
                    stage.removeEventListener(Event.ENTER_FRAME, loop);
                    total = 0;
                }
            }
        }
        private var total:int = 0;
        private function loop(e:Event):void{
            for(var x:int = 0; x<data.width; x++){
                for(var y:int = 0; y<data.height; y++){
                    changePoint(x,y);
                    total++;
                }
            }
            log(total);
        }

        private var tf:TextField = new TextField();
        private function log(data:Object):void{
            if(!tf.stage){
                tf.textColor = 0xffff00;
                tf.autoSize = 'left';
                tf.x = 30; tf.y = 300;
                stage.addChild(tf);
            }
            tf.text = data.toString();
        }
    }
}