flash on 2016-5-29

by mutantleg
♥0 | Line 49 | Modified 2016-05-29 22:28:10 | 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/4kYS
 */

package {
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKdown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKup);
            stage.addEventListener(Event.ENTER_FRAME, onEnter);
        }//ctor
        
        public var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,false);
        public function onKdown(e:KeyboardEvent):void { vecKey[e.keyCode] = true; }
        public function onKup(e:KeyboardEvent):void { vecKey[e.keyCode] = false; }
        public function isKey(k:int):Boolean { return vecKey[k]; }
        
        public var cx:Number = 235;
        public var cy:Number = 235;
        
        public var overId:int = 0;
        
        public function isOver(ax:Number,ay:Number, rx:Number,ry:Number,rw:Number,rh:Number):Boolean
        { graphics.drawRect(rx,ry, rw,rh); 
          return (ax<rx||ax>rx+rw||ay<ry||ay>ry+rh) == false;   }
      
       public function checkRect(id:int, rx:Number,ry:Number,rw:Number,rh:Number, spec:int):void
       {
           if (isOver(cx,cy,rx,ry,rw,rh)==false) { return; }
            if (spec==1){  if (cx<rx+rw*0.5) { cx=rx+rw*0.5; } }
            if (spec==2){  if (cx>rx+rw*0.5) { cx=rx+rw*0.5; } }
            if (spec==3){  if (cy<ry+rh*0.5) { cy=ry+rh*0.5; } }
            if (spec==4){  if (cy>ry+rh*0.5) { cy=ry+rh*0.5; } }
           overId = id;   
           graphics.drawRect(rx+1,ry+1,rw-2,rh-2);
       }//checkrect
      
      
       public function onEnter(e:Event):void
       {
           graphics.clear();
           graphics.lineStyle(2, 0);
           
           var ms:Number; ms = 8;
           if (isKey(Keyboard.UP)) { cy-=ms;}
           if (isKey(Keyboard.DOWN)) { cy+=ms;}
           if (isKey(Keyboard.LEFT)) { cx-=ms;}
           if (isKey(Keyboard.RIGHT)) { cx+=ms;}
           
           checkRect(1, 132,132,64,64, 0);
           checkRect(2, 132+80,132,64,64, 1);
           checkRect(3, 132+80+80,132,64,64, 2);
           checkRect(4, 132,132+80,64,64, 3);
           checkRect(5, 132,132+80+80,64,64, 4);
           
           /*
           if (isOver(cx,cy, 132,132,64,64))
           {
               graphics.drawCircle(cx,cy,8);
               
              // if (cx<132+32){cx=132+32;}
              if (cy<132+32){cy=132+32;}
               
           }
           */
           
           graphics.drawCircle(cx,cy,16);
           
       }//onenter  
        
    }//classend
}