2016-04-22 48 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),就取消调用。

如果你的乘数是一个整数,你可以删除余量并使用一个整数。