2012-10-05 2 views
0

키가 다운 상태와 업 상태 사이에서 전환 될 때이를 감지하려고합니다. UP 상태로 되돌리기 전에 키가 한 프레임 동안 해제되었음을 표시해야합니다.키 릴리즈 프레임 감지

필요한 경우 정보 나 코드를 추가 할 수 있습니다. 미리 감사드립니다.

package engine 
{ 
import flash.display.Stage; 
import flash.events.KeyboardEvent; 


/** 
* Input Manager 
*/ 
public class Input 
{ 
    static private const UP   : uint = 0; 
    static private const PRESS  : uint = 1; 
    static private const HELD  : uint = 2; 
    static private const END_PRESS : uint = 3; 

    static private const START_PRESS:uint = 9999; 


    static private var keys  : Vector.<uint>; 
    static private var active : Vector.<KeyState>; 



    static public function init(stage:Stage):void 
    { 
     stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); 
     stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp); 

     keys = new Vector.<uint>(255); // state of all keys 
     active = new Vector.<KeyState>();  // only keys in a state other than up 
     //time = new Vector.<Time>(); 

     stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); 
     stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); 
    } 


    /// Flash Event: A key was just pressed 
    static public function onKeyDown(e:KeyboardEvent):void 
    { 
     // If the system is sending another key down event, but the key is marked 
     // as being in some other state than down; ignore it. 
     if (keys[ e.keyCode ] != UP) 
      return; 

     keys[ e.keyCode ] = START_PRESS; 

     var keyState:KeyState = new KeyState(e.keyCode, Time.frameCount); 
     active.push(keyState); 
    } 

    /// Flash Event: A key was raised 
    static public function onKeyUp(e:KeyboardEvent):void 
    { 

     keys[ e.keyCode ] = UP 

     // Loop through all active keys; there is a slight chance that 
     // more than one entry for a key being "down" snuck in due to 
     // how Flash/OS handles input. 
     var keyState:KeyState; 
     for (var i:int = active.length - 1; i > -1; i--) 
     { 

      keyState = active[i];    // get next keystate in active list 
      if (keyState.code == e.keyCode) // if the code matches 
       active.splice(i, 1);   // remove 
     } 
    } 

    /// Call this once per frame 
    static public function update():void 
    { 
     var code :uint; 
     var keyState:KeyState; 

     // Go through all the inputs currently mark as being active... 
     for (var i:int = active.length - 1; i > -1; i--) 
     { 
      keyState = active[i]; 
      code = keyState.code; 

      // If a key is pressed and it's the frame after it was pressed, 
      // change the state. 
      if (keys[code] == PRESS && keyState.frame < Time.frameCount) 
      { 
       keys[code] = HELD; 
       continue; 
      } 

      // If a press is just starting, mark it as started and update 
      // the frame for the press to be this frame. 
      if (keys[code] == START_PRESS) 
      { 
       keys[code] = PRESS; 
       keyState.frame = Time.frameCount; 
      } 

     } 

    } 

    /// Has a key just been pressed in this frame? 
    static public function getKeyDown(code:uint):Boolean 
    { 
     return keys[ code ] == PRESS; 
    } 

    /// Is a key in state other than being "up"? 
    static public function getKey(code:uint):Boolean 
    { 
     return keys[ code ] == HELD; 
    } 

    static public function getKeyRelease(code:uint):Boolean 
    { 
     return keys[ code ] == END_PRESS; 
    } 
} 
} 


internal class KeyState 
{ 
public var code :uint; 
public var frame:uint; 

/// CTOR 
function KeyState(code:uint, frame:uint) :void 
{ 
    this.code = code; 
    this.frame = frame; 
} 
} 
+1

나는 정말로 당신이 궁금한 것이 무엇인지 모르겠다. 나는 당신이 성취하고자하는 것을 이해하지만, 당신에게 잘못된 것이 무엇인지를 진술하지 않았다. 디버깅을 해봤 는가? , 오류가 발생합니까? 단지 약간의 코드로 시작해야 할 부분은 확실하지 않습니다. – shaunhusain

+0

현재 프레임 번호를 알고 싶다면'private var current_frame : int = 0;'및 enterFrame 리스너에'current_frame ++;'를 추가하면됩니다. –

답변

0

프레임 내에서 수행 할 작업이 필요하므로 Event.ENTER_FRAME 수신기를 사용해야합니다. 이렇게하면 "방금 출시 된"키를 "위로"상태로 올바르게 재설정 할 수 있습니다. 이 경우 수행

static public function onKeyUp(e:KeyboardEvent):void 
{ 
    keys[ e.keyCode ] = END_PRESS; 
    // I'm not sure why you need other checks in here, add them if you like 
} 

static public function onEnterFrame(e:Event):void 
{ 
    for (var i:int=keys.length-1; i>=0;i--) if (keys[i]==END_PRESS) { 
     keys[i]=UP; 
     // do whatever you need with i'th key, as this will be the "just released" key. 
    } 
} 

업데이트 : 당신은 이미 초보적인 입력 프레임 절차가 있고, 이미 언론에 대한 역학과 열린 상태의 일부를 가지고, 그래서 당신은 분명 그 과정에이주기를 추가 할 수 있습니다 .