flash on 2015-3-22

by mutantleg
♥0 | Line 81 | Modified 2015-03-22 03:52:38 | 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/aFha
 */

package {
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
           
           mw=16;mh=16; cw=16;ch=16;
           vecGrid = new Vector.<int>(mw*mh, false);           
           vecObj = new Vector.<xObj>(0, false);
           
           var a:xObj;      
            a = new xObj();       a.cx = 8; a.cy=8;        vecObj.push(a); a.id = vecObj.length;
            a = new xObj();       a.cx = 4; a.cy=4;        vecObj.push(a); a.id = vecObj.length;
            a = new xObj();       a.cx = 6; a.cy=1;        vecObj.push(a); a.id = vecObj.length;
            a = new xObj();       a.cx = 9; a.cy=2;        vecObj.push(a); a.id = vecObj.length;
            a = new xObj();       a.cx = 12; a.cy=3;        vecObj.push(a); a.id = vecObj.length;
            
           
           stage.addEventListener(Event.ENTER_FRAME, onEnter); 
        }//ctor
        
        public var vecGrid:Vector.<int>;
        public var mw:int = 16;
        public var mh:int = 16;
        public var cw:Number = 8;
        public var ch:Number = 8;
        
        public var vecObj:Vector.<xObj>;
        
        public var gt:int = 0;
        
        public function onEnter(e:Event):void
        {
            graphics.clear();
            graphics.lineStyle(2, 0);
   
            graphics.drawRect(0,0, mw*cw, mh*ch);
   
            var vec:Vector.<xObj>;
            var i:int; var k:int; var num:int; var a:xObj;
            vec = vecObj;    num = vec.length;
            for (i = 0; i < num; i++)
            { a = vec[i];  
              if (a == null || a.hp < 0) { for (k = i; k < num-1; k++) {  vec[k] = vec[k+1]; }
              num -= 1; i -= 1; vec.pop();  continue; }
              updateObj(a);
            }//nexti
            
            
            gt += 1;
        }//onenter
        
        public function updateObj(a:xObj):void
        {
          graphics.drawRect(a.cx*cw, a.cy*ch, cw, ch); 
          if (a.id > 1)  { graphics.beginFill(0,1); graphics.drawRect(a.cx*cw, a.cy*ch, cw, ch); graphics.endFill();   }  
            
          //note: this trick ( &(mw-1)) only works on certain map size (bitflag voodoo)
      
           if (gt % 16 == 0)          
          if (a.id == 1) 
          {
            a.vx = 0; a.vy = 0;
            if (Math.random() < 0.5) { a.vx = 1; } 
            else { a.vy = 1; } 
          }
 
          if (gt % (a.id==1?4:8) == 0) 
             {       
                a.cx = int(a.cx +a.vx) & (mw-1);  
                a.cy = int(a.cy +a.vy) & (mh-1); 
             }
    
          if (a.id == 1) { return; }        
            
          var w:xObj;
          w = vecObj[0];
          
          var kx:int;          var ky:int;
          var wx:int;          var ax:int;
          
         
          wx = w.cx; ax = a.cx;
          if (ax - wx > (mw/2)) { ax -= mw; }
          if (wx - ax > (mw/2)) { wx -= mw; }
          if (wx < ax) { kx = -1; }   if (wx > ax) { kx = 1;  }
      
          wx = w.cy; ax = a.cy;            
          if (ax - wx > (mh/2)) { ax -= mh; }
          if (wx - ax > (mh/2)) { wx -= mh; }
          if (wx < ax) { ky = -1; }   if (wx > ax) { ky = 1;  }
            
         // graphics.drawRect((a.cx+kx)*cw, a.cy*ch, cw, ch); 
         // graphics.drawRect((a.cx)*cw, (a.cy+ky)*ch, cw, ch); 

          a.vx =kx; a.vy=ky;
            
        }//upobj
        
    
        
    }//classend
}

internal class xObj
{
   public var cx:Number = 0;
   public var cy:Number = 0;
   public var vx:Number = 0;
   public var vy:Number = 0;
 
   public var hp:Number = 100; 
    
   public var id:int = 0; 
    
    
}//xact