2016-04-22 44 views
0

這裏是我的分數經理劇本我做:得分乘法器時間

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 

public class ScoreManager : MonoBehaviour { 

    public Text scoreText; 

    public float scoreCount; // What the score is when the scene first loads. I set this as zero. 

    public float pointsPerSecond; // Every second, the points increase by THIS amount. Right now it is 100. 

    // Update is called once per frame 
    void Update() { 

     scoreCount += pointsPerSecond * Time.deltaTime; 

     scoreText.text = "" + Mathf.Round (scoreCount); // It comes out as a float with like 6 decimals; I round to an nice, clean Integer 

    } 
} 

我,我無法弄清楚如何解決的問題是:我如何作出這樣的30後乘以兩三次比分事半功倍然後將分數乘以1分鐘後的三次,然後在1分鐘和30秒後乘以四次,然後在2分鐘後乘以五次?謝謝:)

回答

0

這是使用IEnmurator函數的絕好機會。這些是你可以調用的方法,你可以告訴它等待一段時間後再恢復。所以在你的情況下,你可以有一個IEnumerator函數乘以你的得分每三十秒。例如:

public Text scoreText; 

public float scoreCount; // What the score is when the scene first loads. I set this as zero. 

public float pointsPerSecond; // Every second, the points increase by THIS amount. Right now it is 100. 

private int scoreMultiplier = 1; 

void Start() 
{ 
    StartCoroutine(MultiplyScore()); 
} 

// Update is called once per frame 
void Update() 
{ 

    scoreCount += pointsPerSecond * Time.deltaTime; 

    scoreText.text = "" + Mathf.Round (scoreCount); // It comes out as a float with like 6 decimals; I round to an nice, clean Integer 

} 

IEnumerator MultiplyScore() 
{ 
    while(true) 
    { 
     yield return new WaitForSeconds(30); 
     scoreMultiplier++; 
     scoreCount *= scoreMultiplier; 
    } 
} 

注意,如果只想往上走5次乘法可以用分數乘法變量作爲條件你IEnumerator while循環,如下所示:

IEnumerator MultiplyScore() 
{ 
    while(scoreMultiplier < 5) 
    { 
     yield return new WaitForSeconds(30); 
     scoreMultiplier++; 
     scoreCount *= scoreMultiplier; 
    } 
} 
4
private float multiplier = 2.0f; 
void Start(){ 
    InvokeRepeating("SetScore", 30f, 30f); 
} 
void SetScore(){ 
    score *= multiplier; 
    multiplier += 1f; 
    if(multiplier > 5.5f) // Added 0.5f margin to avoid issue of float inaccuracy 
    { 
     CancelInvoke() 
    } 
} 

InvokeRepeating設置第一個呼叫(第​​二個參數)和頻率(第三個參數),在你的情況下它是30秒,也是30秒。然後一旦乘數太大(大於5),就取消調用。

如果你的乘數是一個整數,你可以刪除餘量並使用一個整數。