2016-05-30 38 views
0

聲音沒有爲第二次觸摸播放它只爲第一次觸摸播放的聲音只有0.02持續時間,它的MP3播放只用於第一次觸摸,但我必須爲每次點擊它應該覺得汽車加速在統一中添加聲音

using UnityEngine; 
    using System.Collections; 
    public class Player : MonoBehaviour 
    { 
     // The force which is added when the player jumps 
     // This can be changed in the Inspector window 
    public Vector2 jumpForce = new Vector2(0, 300); 
    public AudioClip imp; 
     public AudioSource clk; 
     // Update is called once per frame 
     void Update() 
     { 
      // Jump 
      if (Input.GetMouseButtonDown(0)) 
      { 

       GetComponent<Rigidbody2D>().velocity = Vector2.zero; 
       GetComponent<Rigidbody2D>().AddForce(jumpForce); 
       clk.PlayOneShot (imp, 0.7f); 

      } 
      Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position); 
      if (screenPosition.y > Screen.height || screenPosition.y < 0) 
      { 
       Die(); 
      } 
     } 

     // Die by collision 
     void OnCollisionEnter2D(Collision2D other) 
     { 
      Die(); 
     } 


     void Die() 
     { 

      Application.LoadLevel(0); 

     } 
    } 
+0

有什麼錯誤?你的調試說什麼?我看到你的班級裏沒有調試代碼 – Martin

+0

他們沒有錯誤 –

+0

它播放automaticaly如果我把它打起來不玩聲音它甚至不發聲 –

回答

1

聲音不打第二觸摸,因爲Input.GetMouseButtonDown(0)將只檢測一個觸摸。你循環播放Touches,然後播放聲音。在檢測到第一次觸摸後斷開循環,因爲只有一個人在屏幕上有觸摸時播放一個聲音。

void Update() 
{ 
    int touches = Input.touchCount; 
    Debug.Log(touches); 

    for (int i = 0; i < touches; i++) 
    { 
     if (touches > 0 && Input.GetTouch(i).phase == TouchPhase.Began) 
     { 
      clk.PlayOneShot(imp, 0.7f); 
      break; 
     } 
    } 

    if (Input.GetMouseButtonDown(0)) 
    { 

     GetComponent<Rigidbody2D>().velocity = Vector2.zero; 
     GetComponent<Rigidbody2D>().AddForce(jumpForce); 
    } 
    Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position); 
    if (screenPosition.y > Screen.height || screenPosition.y < 0) 
    { 
     Die(); 
    } 
} 
+0

它甚至不播放聲音 –

+0

@GBGolden測試它,它的工作原理。該代碼僅適用於移動設備。當你說第二次觸摸時,你是在移動設備上這樣做的,對吧? – Programmer

+0

沒有它在我的電腦嘿讓我試試我的手機 –