·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3.0处理按键事件,多个按键按下时的响应事件,按键盘的事件

时间:2013-10-30 17:02cuplayer.com
[AS3]as3.0处理按键事件,多个按键按下时的响应事件,按键盘的事件写法 package { importcom.KeyPoll; importflash.display.Sprite; importflash.events.Event; importflash.ui.Keyboard; importflash.text.TextField; importflash.text.TextFormat; [SWF( wid

[AS3]as3.0处理按键事件,多个按键按下时的响应事件,按键盘的事件写法

  1. package  
  2.     import com.KeyPoll; 
  3.     import flash.display.Sprite; 
  4.     import flash.events.Event; 
  5.     import flash.ui.Keyboard; 
  6.     import flash.text.TextField; 
  7.     import flash.text.TextFormat; 
  8.  
  9.     [SWF(width = "720",height = "450",frameRate = "12",backgroundColor = "0x3366ee")] 
  10.      
  11.     /** 
  12.     *@ web: http://blog.vini123.com www.cuplayer.com 
  13.     *@ explanation 文章来自网上,整理vini。 这个对于键盘控制人物角色八个方向的行走还是比较方便的。 
  14.     */ 
  15.     public class Main extends Sprite 
  16.     { 
  17.         private var key:KeyPoll; 
  18.         private var curPosition:int; 
  19.         private var txt:TextField; 
  20.  
  21.         public function Main():void 
  22.         { 
  23.             addEventListener(Event.ADDED_TO_STAGE,addtoStageHandler); 
  24.         } 
  25.         private function addtoStageHandler(e:Event) 
  26.         { 
  27.             removeEventListener(Event.ADDED_TO_STAGE,addtoStageHandler); 
  28.  
  29.             var txtFormat:TextFormat=new TextFormat(); 
  30.             txtFormat.size = 14
  31.             txt=new TextField(); 
  32.             txt.width=stage.stageWidth-20; 
  33.             txt.multiline=true
  34.             txt.setTextFormat(txtFormat); 
  35.             txt.textColor=0xff0000
  36.             txt.selectable = false
  37.             txt.mouseEnabled = false
  38.             txt.x = 10
  39.             txt.y = 20
  40.             addChild(txt); 
  41.             key = new KeyPoll(stage); 
  42.             addEventListener(Event.ENTER_FRAME,enterFrameHandler); 
  43.         } 
  44.         private function enterFrameHandler(e:Event) 
  45.         { 
  46.             controlKey(); 
  47.         } 
  48.  
  49.         private function controlKey():void 
  50.         { 
  51.             if (key.isDown(Keyboard.UP)) 
  52.             { 
  53.  
  54.                 if (key.isDown(Keyboard.LEFT)) 
  55.                 { 
  56.                     curPosition = 7
  57.                 } 
  58.                 else if (key.isDown(Keyboard.RIGHT)) 
  59.                 { 
  60.                     curPosition = 8
  61.                 } 
  62.                 else 
  63.                 { 
  64.                     curPosition = 3
  65.                 } 
  66.             } 
  67.             else if (key.isDown(Keyboard.DOWN)) 
  68.             { 
  69.                 if (key.isDown(Keyboard.LEFT)) 
  70.                 { 
  71.                     curPosition = 5
  72.  
  73.                 } 
  74.                 else if (key.isDown(Keyboard.RIGHT)) 
  75.                 { 
  76.                     curPosition = 6
  77.                 } 
  78.                 else 
  79.                 { 
  80.                     curPosition = 1
  81.                 } 
  82.             } 
  83.             else if (key.isDown(Keyboard.LEFT)) 
  84.             { 
  85.                 curPosition = 2
  86.  
  87.             } 
  88.             else if (key.isDown(Keyboard.RIGHT)) 
  89.             { 
  90.                 curPosition = 4
  91.             } 
  92.             txt.text = "cuplayer.com提示请按小键盘的上下左右或两个同时一起按,注意看着:" + String(curPosition); 
  93.         } 
  94.     } 

热门文章推荐

请稍候...

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

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