2014-01-18 122 views
1

在移動控制器中工作,我遇到了一個有趣的問題。當我快速左右按壓然後跳躍時,角色繼續向右或向左走,這讓我覺得在某些情況下,moveLeft或moveRight從不會將對象設置爲false。TouchPhase.Ended未執行

我看了很多關於TouchPhase.Ended的帖子都沒有解僱,你們有沒有經歷過這個?有沒有解決方案?

編輯我的職務,其中包括我的代碼的傢伙:

void Update(){ 
    TouchManager(); 
} 

void FixedUpdate() { 
    Move(); 
} 

void Move() 
{ 
    //True if player hits ground 
    groundHit = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround); 
    //True if player hits top 
    topHit = Physics2D.OverlapCircle (topCheck.position, topRadius, whatIsTop); 
    anim.SetBool ("Ground", groundHit); 
    anim.SetFloat ("vSpeed", this.rigidbody2D.velocity.y); 
    if (Globals.gameStatus == Globals.GameStatus.Dead) { 
     anim.SetFloat ("Speed", 0); 
     return; 
    } 
    float move = 0.0f; 

    if (moveLeft) { 
     move = -1.0f; 
    } 
    if (moveRight) { 
     move = 1.0f; 
    } 

    anim.SetFloat ("Speed", Mathf.Abs (move)); 

    rigidbody2D.velocity = new Vector2 (move * maxSpeed, this.rigidbody2D.velocity.y); 
    if (move > 0 && !facingRight) 
     Flip(); 
    else 
     if (move < 0 && facingRight) 
      Flip(); 
} 

void TouchManager() 
{ 

    if (Input.touchCount > 0) { 
     var touchCount = Input.touchCount; 
     for (var i = 0; i < touchCount; i++) { 
      var touch = Input.GetTouch (i); 
      //Debug.Log("FingerID: " + touch.fingerId.ToString()); 

      if (touch.phase == TouchPhase.Began) { 
       //Debug.Log("Touch Began executed for touchID: " + touch. 
       if (guiLeft.HitTest (touch.position, Camera.main)) 
        moveLeft = true; 
       if (guiRight.HitTest (touch.position, Camera.main)) 
        moveRight = true; 
       if (guiJump.HitTest (touch.position, Camera.main)) 
       { 
        Debug.Log("Jump pressed"); 
        if (groundHit && !stickmanIsDead) { 
         anim.SetBool ("Ground", false); 
         rigidbody2D.AddForce (new Vector2 (0, jumpForce)); 
        } 
        //Record start time only once 
        holdJumpDownStartedAt = Time.time; 
       } 
      } 
      //Fall from Platforms 
      if(touch.phase == TouchPhase.Stationary){ 
       if (guiJump.HitTest (touch.position, Camera.main)){ 
        if(Time.time - holdJumpDownStartedAt > dropPlatformAt){ 
         Debug.Log(string.Format("Holded for {0} second :) ",dropPlatformAt.ToString())); 
         holdJumpDownStartedAt = 0.0f; 

         #region Falling Keystroke 
         var playerFalling = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer ("Floor-Air")); 
         //Fall Key 
         if (playerFalling) 
          StartCoroutine (Fall()); 
         #endregion 
        } 
       } 
      } 
      if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled){ 
       if (guiLeft.HitTest (touch.position, Camera.main)) 
        moveLeft = false; 
       if (guiRight.HitTest (touch.position, Camera.main)) 
        moveRight = false; 
      } 
     } 
    } 
} 

+0

你能告訴我們你的特定代碼?確實有TouchPhase.Ended據報不會觸發的情況(你在什麼平臺上進行btw),但更多的時候是實際的代碼是一個問題。 – Bart

+0

也許TouchPhase.Canceled被觸發而不是Ended?但沒有任何代碼,我們無法真正幫助。是否有左右按鈕?他們是gui按鈕嗎? –

+0

剛剛添加我的代碼,請參閱我的文章。感謝您的回覆如此之快。有三個按鈕,左和右,也跳。看我的代碼。 – Dilmer

回答

0

想通了感謝你們反正這裏是我落得這樣做:

void CharacterTouchManager(){

//Debug.Log("Moving..." + rigidbody2D.velocity.x.ToString()); 
    //Debug.Log("Player is: " + Globals.playIsDoing.ToString()); 

    //Movement 
    if(Input.touchCount > 0){ 
     var touchCount = Input.touchCount; 
     for (var i = 0; i < touchCount; i++) { 
      var touch = Input.GetTouch (i); 
      //if player presses less than 1/5 of the screen, go left. 
      if(touch.position.x < Screen.width/5 && touch.position.y < Screen.height/3){ 
       touchControlSwitchToJumpMode = false; 
       //if(rigidbody2D.velocity.x > -0.1){ 
       // moveLeft = true; 
       //} 
       //else 
       // moveLeft = true; 

       moveLeft = true; 
       moveRight = false; 
      } 
      //if player presses between 1/5 and 2/5 of the screen, go right. 
      if(touch.position.x > Screen.width/5 && touch.position.x < Screen.width/5 * 2 && touch.position.y < Screen.height/3){ 
       touchControlSwitchToJumpMode = false; 

       //if(rigidbody2D.velocity.x < 0.1){ 
       // moveRight = true; 
       //} 
       moveRight = true; 
       moveLeft = false; 
      } 
     } 
    } 
    else { 
     moveLeft = moveRight = false; 
    } 

    //Jumping and Dropping from platforms 
    if (Input.touchCount > 0) { 
     var touchCount = Input.touchCount; 
     for (var i = 0; i < touchCount; i++) { 
      var touch = Input.GetTouch (i); 
      //Make sure we're not touching jump by itself to avoid character running continously 
      if(!touchControlSwitchToJumpMode && touchCount == 1 && guiJump.HitTest (touch.position, Camera.main)){ 
       moveRight = moveLeft = false; 
       touchControlSwitchToJumpMode = true; 
      } 
      if (touch.phase == TouchPhase.Began) { 
       if (guiJump.HitTest (touch.position, Camera.main)) 
       { 
        if (groundHit && !stickmanIsDead) { 
         anim.SetBool ("Ground", false); 
         rigidbody2D.AddForce (new Vector2 (0, jumpForce)); 
        } 
        //Record start time only once 
        holdJumpDownStartedAt = Time.time; 
       } 
      } 
      if(touch.phase == TouchPhase.Stationary){ 
       if (guiJump.HitTest (touch.position, Camera.main)){ 
        if(Time.time - holdJumpDownStartedAt > dropPlatformAt){ 
         holdJumpDownStartedAt = 0.0f; 
         #region Falling Keystroke 
         var playerFalling = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer ("Floor-Air")); 
         //Fall Key 
         if (playerFalling) 
          StartCoroutine (Fall()); 
         #endregion 
        } 
       } 
      } 
     } 
    } 
}