2013-05-31 2 views
0

플랫폼 게임을 만들려고하고 있으며 실행, 점프 및 이중 점프 기능이 있습니다. 이제는 내 문제는 플레이어가 점프 버튼을 누르고 영웅이 한 번만 점프하는 대신 계속 점프하는 것입니다. 내가 위로 버튼을 누르고 있으면 나는 그것을 원하지 않는 점프를 계속할 것이다. 나는 단지 키가 여전히 눌러져 있어도 한번 점프를 수행하기를 원한다. 나는 이중 점프에 대해 같은 것을 원한다. 어떻게 할 수 있을까?AS3 : 단일 키만 누르기

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.Event; 
    import flash.events.KeyboardEvent; 
    import flash.ui.Keyboard; 

    public class Player extends MovieClip 
    { 
     //Player run speed setting 
     var RunSpeed:Number = 8; 
     //Player key presses 
     var RightKeyPress:Boolean = false; 
     var LeftKeyPress:Boolean = false; 
     var UpKeyPress:Boolean = false; 
     //Jump variables 
     var Gravity:Number = 1.5; 
     var JumpPower:Number = 0; 
     var CanJump:Boolean = false; 
     var DoubleJump:Boolean = false; 

     public function Player() 
     { 
      // constructor code 
      stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed); 
      addEventListener(Event.ENTER_FRAME,Update); 
      stage.addEventListener(KeyboardEvent.KEY_UP,KeyReleased); 
     } 

     function KeyPressed(event:KeyboardEvent) 
     { 
      //When Key is Down 
      if (event.keyCode == 39) 
      { 
       RightKeyPress = true; 
      } 

      if (event.keyCode == 37) 
      { 
       LeftKeyPress = true; 
      } 

      if (event.keyCode == 38) 
      { 
       UpKeyPress = true 

      } 
     } 

     function Update(event:Event) 
     { 
      //Adding gravity to the game world 
      JumpPower += Gravity; 
      //if player is more than 300 on the y-axis 
      if (this.y > 300) 
      { 
       //Player stays on the ground and can jump 
       JumpPower = 0; 
       CanJump = true; 
       DoubleJump = false; 
      } 

      //If player is on floor and right key is pressed then run right 
      if ((RightKeyPress && CanJump)) 
      { 
       x += RunSpeed; 
       gotoAndStop('Run'); 
       scaleX = 1; 
      } 
      else if ((LeftKeyPress && CanJump)) 
      { 
       //Otherwise if on floor and left key is pressed run left 
       x -= RunSpeed; 
       gotoAndStop('Run'); 
       scaleX = -1; 
      } 
      else if ((UpKeyPress && CanJump)) 
      { 
       //Otherwise if on floor and up key is pressed then jump 
       JumpPower = -15; 
       CanJump = false; 
       gotoAndStop('Jump'); 
       DoubleJump = true; 
      } 

      //If on floor and right and up key are pressed then jump 
      if ((RightKeyPress && UpKeyPress && CanJump)) 
      { 
       JumpPower = -15; 
       CanJump = false; 
       gotoAndStop('Jump'); 
       DoubleJump = true; 
      } 

      //If on floor and left and up key are pressed then jump 
      if ((LeftKeyPress && UpKeyPress && CanJump)) 
      { 
       JumpPower = -15; 
       CanJump = false; 
       gotoAndStop('Jump'); 
       DoubleJump = true; 
      } 

      //If jumped and right key is pressed then move right 
      if ((RightKeyPress && CanJump == false)) 
      { 
       x += RunSpeed; 
       scaleX = 1; 
      } 
      else if ((LeftKeyPress && CanJump == false)) 
      { 
       //Otherwise if jumped and left key is pressed then move left 
       x -= RunSpeed; 
       scaleX = -1; 
      } 

      //If in air and able to doublejump and pressed up key, then double jump 
      if (UpKeyPress && DoubleJump && JumpPower > -2) 
      { 
       JumpPower = -13; 
       DoubleJump = false; 
       gotoAndStop('DoubleJump'); 
      } 

      //If on floor and no key is presses stay idle 
      if ((!RightKeyPress && !LeftKeyPress && CanJump)) 
      { 
       gotoAndStop('Idle'); 
      } 

      this.y += JumpPower; 
     } 

     function KeyReleased(event:KeyboardEvent) 
     { 
      if (event.keyCode == 39) 
      { 
       event.keyCode = 0; 
       RightKeyPress = false; 
      } 

      if (event.keyCode == 37) 
      { 
       event.keyCode = 0; 
       LeftKeyPress = false; 
      } 

      if (event.keyCode == 38) 
      { 
       event.keyCode = 0; 
       UpKeyPress = false; 
      } 
     } 
    } 
} 
+0

이중 점프가 발생하는 곳에 중단 점을 넣으려고 했습니까? 부울을 가진 논리가 올바르게 작동해야하는 것처럼 나에게 보인다. 그러나 당신은 어떤 이유로 그 블록에서 반복적으로 끝나야 만 하는가? 업데이트 기능의 주요 지점에서 부울을 추적 해보십시오. – shaunhusain

+0

@shaunhusain : 중단 점? 나는 AS3에 익숙하지 않기 때문에 아직 많이 알지 못하지만 빨리 배우고 있습니다. – user2350724

+0

문제가되지 않습니다.이 링크를 확인하십시오. http://www.adobe.com/devnet/flash/articles/flash_as3_debugging.html 또는 조금만 Google을 사용하면이 기사의 후반부가 도움이 될 것 같지만 길기는합니다. – shaunhusain

답변

2

나는 어떤 일이 일어나고 것은 의심 그 즉시 플레이어의 y 값이 그렇게 오랫동안 키를 누르고 UpKeyPress && CanJump 모두에 해당하는 때문에 그가 점프로 다시 이동 그를 가능하게하는 if (this.y > 300) 조건을 충족하는 .

내 생각 엔 다음과 같은 일을하는 것은 요 답 ... 당신의 업데이트 기능에서

가까이 당신을 얻을 수 있다는 것입니다 : 당신의 및 KeyPressed 기능에 다음

function Update(event:Event) 
{ 
    //Adding gravity to the game world 
    JumpPower += Gravity; 
    //if player is more than 300 on the y-axis 
    if (this.y > 300) 
    { 
     //Player stays on the ground and can jump 
     JumpPower = 0; 
     // Do not allow another jump until the UpKey is pressed 
     //CanJump = true; 
     //DoubleJump = false; 
    } 
    ... 

:

function KeyPressed(event:KeyboardEvent) 
{ 
    //When Key is Down 
    if (event.keyCode == 39) 
    { 
     RightKeyPress = true; 
    } 

    if (event.keyCode == 37) 
    { 
     LeftKeyPress = true; 
    } 

    if (event.keyCode == 38) 
    { 
     UpKeyPress = true 
     // Do not allow another jump until the UpKey is pressed 
     if (this.y > 300) 
     { 
      CanJump = true; 
      DoubleJump = false; 
     } 
    } 
} 

또한 제 2의 shaunhusain이 중단 점을 설정하고 디버깅 코드에 익숙해 지도록 권장합니다.

+3

나중에 참조 할 수 있도록 일부 키를 열거하는 Keyboard 클래스가 있으므로 38을 Keyboard.UP로 바꿀 수 있습니다. – Rich

관련 문제