·您当前的位置:首页 > 技术教程 > JavaScript >

[AS3]as3中键盘按键控制影片拖动源代码实例

时间:2014-04-18 10:02王翔
[AS3]as3中键盘按键控制影片拖动源代码实例

[AS3]as3中键盘按键控制影片拖动源代码实例

  1. package 
  2.     import flash.display.Sprite; 
  3.     import flash.events.Event; 
  4.     import flash.events.KeyboardEvent; 
  5.     import flash.events.TimerEvent; 
  6.     import flash.utils.Timer; 
  7.     import flash.utils.getTimer; 
  8.      
  9.     public class Main extends Sprite 
  10.     {     
  11.         var mascot:Bo = new Bo(); 
  12.         var leftArrow:Boolean = false
  13.         var rightArrow:Boolean = false
  14.         var upArrow:Boolean = false
  15.         var downArrow:Boolean = false
  16.          
  17.         public function Main():void 
  18.         { 
  19.             init(); 
  20.         } 
  21.         private function init():void 
  22.         { 
  23.             this.addChild(mascot); 
  24.             stage.addEventListener(KeyboardEvent.KEY_DOWN,downs); 
  25.             stage.addEventListener(KeyboardEvent.KEY_UP,ups); 
  26.             stage.addEventListener(Event.ENTER_FRAME,moves); 
  27.         } 
  28.          
  29.         public function downs(e:KeyboardEvent):void 
  30.         { 
  31.             if(e.keyCode == 37) 
  32.             { 
  33.                 leftArrow = true
  34.             } 
  35.             else if(e.keyCode == 38) 
  36.             { 
  37.                 upArrow = true
  38.             } 
  39.             else if(e.keyCode == 39) 
  40.             { 
  41.                 rightArrow = true
  42.             } 
  43.             else if(e.keyCode == 40) 
  44.             { 
  45.                 downArrow = true
  46.             } 
  47.         } 
  48.          
  49.         public function ups(e:KeyboardEvent):void 
  50.         { 
  51.             if(e.keyCode == 37) 
  52.             { 
  53.                 leftArrow = false
  54.             } 
  55.             else if(e.keyCode == 38) 
  56.             { 
  57.                 upArrow = false
  58.             } 
  59.             else if(e.keyCode == 39) 
  60.             { 
  61.                 rightArrow = false
  62.             } 
  63.             else if(e.keyCode == 40) 
  64.             { 
  65.                 downArrow = false
  66.             } 
  67.         } 
  68.          
  69.         public function moves(e:Event):void 
  70.         { 
  71.             var speed:Number = 15
  72.              
  73.             if(leftArrow && upArrow) 
  74.             { 
  75.                 mascot.x -speed
  76.                 mascot.y -speed
  77.             } 
  78.              
  79.             if(leftArrow) 
  80.             { 
  81.                 mascot.x -speed
  82.             } 
  83.             else if(upArrow) 
  84.             { 
  85.                 mascot.y -speed
  86.             } 
  87.              
  88.             if(rightArrow && downArrow) 
  89.             { 
  90.                 mascot.x += speed; 
  91.                 mascot.y += speed; 
  92.             } 
  93.              
  94.             if(rightArrow) 
  95.             { 
  96.                 mascot.x += speed; 
  97.             } 
  98.             else if(downArrow) 
  99.             { 
  100.                 mascot.y += speed; 
  101.             } 
  102.         } 
  103.          
  104.     } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器