bitmap based

by YoupSolo forked from flash on 2012-11-14 (diff: 68)
♥2 | Line 54 | Modified 2012-11-15 08:47:26 | MIT License
play

ActionScript3 source code

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

// forked from hucota7's flash on 2012-11-14
// waoooo ^^ be carefull with your displaylist
// it's really processor intensive.
// and myBitmapBata.getPixel(x,y) is the fastest getter you can have in Flash :)

package 
{

    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.ui.Mouse;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;

    public class FlashTest extends Sprite 
    {
     
      private var _maze_bmp:Bitmap;
      private var maze:Sprite; // bitmap are not interactive

        public function FlashTest() 
        {

            //EDITABLE VARS
            var mazeSize:int = 69; // Keep odd
            var mazeScale:int = 6;

            //////////////////////////////////
            //VARIABLES
            //var :Array = new Array();
            //////////////////////////////////
            //SETUP           

           var _maze_data:BitmapData = new BitmapData( mazeSize, mazeSize, false, 0xFFFFFF );
           _maze_bmp = new Bitmap( _maze_data);
           _maze_bmp.scaleX = _maze_bmp.scaleY = mazeScale;             

            _maze_data.lock(); // not really usefull here but it's a good practice
            for (var i:int = 0; i < mazeSize; i++)
            {
                for (var j:int = 0; j < mazeSize; j++)
                {

                    //wallArray.push([]);
                    if ( (i == 0 || j == 0 || i == mazeSize-1 || j == mazeSize-1) ||
                    ( i % 2 == 0 && j % 2 == 0 ) ||
                    ( int( Math.random()*2 ) == 0 && ( i % 2 == 0 || j % 2 == 0 ) ) )
                    {
                        _maze_data.setPixel(j,i,0x0);
                        /*
                        var wall:Sprite = new Sprite();
                        wall.graphics.beginFill(0x000000);
                        wall.graphics.drawRect(0,0,mazeScale,mazeScale);
                        wall.graphics.endFill();
                        wall.x = i*mazeScale;
                        wall.y = j*mazeScale;
                        addChild(wall);
                        wallArray[i][j] = 1;
                        */
                    }else{
                        //wallArray[i][j] = 0;
                    }
                }
            }
            _maze_data.unlock();
            

            maze = new Sprite;
            maze.addChild( _maze_bmp );
            maze.x = (stage.stageWidth - maze.width) >> 1;
            maze.y = (stage.stageHeight - maze.height) >> 1;
            maze.rotationY = 20; // the inner coords system is still usable
            addChild( maze );

            

            // and you can access to data really fast using getPixel || getPixel32
            maze.addEventListener(MouseEvent.CLICK, _onClickMe );
        }

        

        private function _onClickMe(e:MouseEvent):void
        {

            var p:Point = new Point( int(maze.mouseX / _maze_bmp.scaleX) , int( (maze.mouseY / _maze_bmp.scaleY) ) );
            if ( _maze_bmp.bitmapData.getPixel( p.x, p.y ) == 0x0 )  
            {
                _maze_bmp.bitmapData.setPixel(p.x, p.y, 0xFFFFFF);                   
            }else{
                _maze_bmp.bitmapData.setPixel(p.x, p.y, 0x0);                   
            }

        }

    }

}