vini123

博客园 首页 新随笔 联系 订阅 管理

         as3中已经没有了Key.isDown,KeyPoll类模拟实现了此方法,非常实用.

    这个对于键盘控制人物角色八个方向的行走还是比较方便的。

贴出KeyPoll类:

 1 package com{
 2     import flash.events.KeyboardEvent;
 3     import flash.events.Event;
 4     import flash.display.DisplayObject;
 5     import flash.utils.ByteArray;
 6 
 7     public class KeyPoll {
 8         private var states:ByteArray;
 9         private var dispObj:DisplayObject;
10 
11         public function KeyPoll( displayObj:DisplayObject ) {
12             states = new ByteArray();
13             states.writeUnsignedInt( 0 );
14             states.writeUnsignedInt( 0 );
15             states.writeUnsignedInt( 0 );
16             states.writeUnsignedInt( 0 );
17             states.writeUnsignedInt( 0 );
18             states.writeUnsignedInt( 0 );
19             states.writeUnsignedInt( 0 );
20             states.writeUnsignedInt( 0 );
21             dispObj=displayObj;
22             dispObj.addEventListener( KeyboardEvent.KEY_DOWN, keyDownListener, false, 0, true );
23             dispObj.addEventListener( KeyboardEvent.KEY_UP, keyUpListener, false, 0, true );
24             dispObj.addEventListener( Event.ACTIVATE, activateListener, false, 0, true );
25             dispObj.addEventListener( Event.DEACTIVATE, deactivateListener, false, 0, true );
26         }
27 
28         private function keyDownListener( ev:KeyboardEvent ):void {
29             states[ ev.keyCode >>> 3 ] |= 1 << (ev.keyCode & 7);
30         }
31 
32         private function keyUpListener( ev:KeyboardEvent ):void {
33             states[ ev.keyCode >>> 3 ] &= ~(1 << (ev.keyCode & 7));
34         }
35 
36         private function activateListener( ev:Event ):void {
37             for (var i:int = 0; i < 32; ++i) {
38                 states[i]=0;
39             }
40         }
41 
42         private function deactivateListener( ev:Event ):void {
43             for (var i:int = 0; i < 32; ++i) {
44                 states[i]=0;
45             }
46         }
47 
48         public function isDown( keyCode:uint ):Boolean {
49             return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) != 0;
50         }
51         
52         public function isUp( keyCode:uint ):Boolean {
53             return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) == 0;
54         }
55     }
56 }

 

应用:

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

 

 

posted on 2012-05-30 09:44  黑夜丶残枫  阅读(253)  评论(0)    收藏  举报