flash on 2017-1-30

by mutantleg
♥0 | Line 87 | Modified 2017-01-30 08:13:55 | MIT License
play

ActionScript3 source code

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

package {
    import flash.geom.Rectangle;
    import flash.geom.Matrix;
    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            
            img = new BitmapData(230,230,false,0);
            graphics.clear();
            graphics.beginBitmapFill(img,new Matrix(2,0,0,2),false,false);
            graphics.drawRect(0,0,460,460);
            graphics.endFill();
            
            var r:Rectangle;
            var i:int; 
            r=new Rectangle();
            for (i=0;i<512;i+=1)
            {
              r.x=Math.random()*230;
              r.y=Math.random()*230;
              r.width=1+16*Math.random();
              r.height=1+16*Math.random();
              img.fillRect(r, Math.random()<0.5?0:0xFF);
            }//nexti
            
            r.x=0;r.y=0;r.width=230;r.height=2;
            img.fillRect(r,0);
            r.y=230-1;
            img.fillRect(r,0);
            r.x=0;r.y=0;r.width=2;r.height=230;
            img.fillRect(r,0);
            r.x=230-1;
            img.fillRect(r,0);
            
            
            stage.addEventListener(Event.ENTER_FRAME,onEnter);
        }//ctor
        
        public var img:BitmapData;
        
        public var rx:int = 0;
        public var ry:int = 0;
        public var mode:int = 0;

        public var cx:int = 0;
        public var cy:int = 0;
        public var wx:int = 1;
        public var wy:int = 0;
        public var px:int=0;
        public var py:int=0;
 
        
        public function isWall(ax:int, ay:int):Boolean
        { if (ax<0||ax>=230||ay<0||ay>=230){return false;} return img.getPixel(ax,ay)==0xFF; }
        
        public function onEnter(e:Event):void
        {
           if(ry>=231){return;}   
            
          img.lock();
            
          var i:int; var t:int;
          for(i=0;i<128;i+=1)
          { 
            //img.setPixel(rx,ry,isWall(rx,ry)?0xFF0000:0x808080); 
            
            if (mode==2)
            {
              if (isWall(rx,ry))
              { rx+=1; if (rx>=230){ry+=1;rx=0; mode=0; }}
              else { mode =0; }   
            }
            
            if (mode ==0)
            {
                if (isWall(rx,ry))
                { cx=rx-1;cy=ry; wx=0;wy=1; mode=1;
                  px=cx;py=cy;
                }
                else
                { 
                if (img.getPixel(rx,ry)!=0xFF0000)
                {img.setPixel(rx,ry,0x808080); }
                 rx+=1;   if (rx>=230){ry+=1;rx=0;} }
            }//endif
            
            if (mode==1)
            {
                img.setPixel(cx,cy, 0xff0000);
              if (isWall(cx+wx,cy+wy)) { t=wx;wx=-wy;wy=t; }
              if (isWall(cx+wx,cy+wy)) { t=wx;wx=-wy;wy=t; }
              if (isWall(cx+wx,cy+wy)) { t=wx;wx=-wy;wy=t; }
              if (isWall(cx+wx,cy+wy)) { t=wx;wx=-wy;wy=t; }
              cx+=wx; cy+=wy; 
              if (isWall(cx+wy, cy-wx)==false)  { t=wx;wx=wy;wy=-t; }  
              //if (img.getPixel(cx,cy)==0xff0000){mode=2;}       
              if (cx==px&&cy==py){mode=2;}
            }//endif
            
            
          }//nexti  
         img.unlock();
        }//onenter
        
    }//classend
}