我的游戏有得分。分数由游戏对象表示。在碰撞事件中,分数增加了10分。这些事件不能停止。我希望分数不再增加,条件是"GameOver“。
我想知道如何阻止分数的增加,因为触发事件是无法停止的。使得分=0是不好的,因为我想让球员的结束得分显示。我需要从GameOver时的实例化中分离出分数。或者,我需要使分数整数在GameOver时保持不变。这确实是一个概念性的问题,我不知道如何解决这个问题。有什么想法吗?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class ScoreHandler : MonoBehaviour {
public int score = 0;
public List<GameObject> destroyList = new List<GameObject>();
public static GameObject[] Score;
// Use this for initialization
void Start () {
score -= 80;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "carA") {
score += 10;
}
if(col.gameObject.name == "carB")
{
score += 10;
}
if(col.gameObject.name == "carC")
{
score += 10;
}
if(col.gameObject.name == "carD")
{
score += 10;
}
if(col.gameObject.name == "carE")
{
score += 10;
}
if(col.gameObject.name == "carF")
{
score += 10;
}
if(col.gameObject.name == "carG")
{
score += 10;
}
if(col.gameObject.name == "carH")
{
score += 10;
}
foreach (var go in destroyList)
{
Destroy(go);
}
destroyList.Clear();
string scoreText = score.ToString ();
Score = new GameObject[scoreText.Length];
for (int i = 0; i < scoreText.Length; i++) {
Score[i] = (GameObject)Instantiate (Resources.Load (scoreText
[i].ToString ()));
Score[i].layer = 8;
Score[i].transform.localScale = new Vector3 (0.02F, 0.02F,
0.02F);
Score[i].transform.localPosition = new Vector3 (0.013F + i *
0.01F, 0.12F, 0.0F);
Score[i].transform.Rotate (0, 180, 0);
destroyList.Add (Score[i]);
}
}
}*此代码框有一个滚动条。
发布于 2016-01-02 06:05:51
如果你有GameOver标志,事情会变得更容易。
假设您有一个名为“游戏结束时”的标志:
bool gameOverFlag = false;
.... //something else
void OnGameOver(){
.... //something else
gameOverFlag = true;
.... //something else
}而且,只有当旗上的游戏是true on冲突event时,才能添加分数(同时保持其他一切相同),就会非常直截了当:
if (col.gameObject.name == "carA") {
score += gameOverFlag ? 0 : 10; //this is where ternary operator will come really handy
//something else specific for carA, not for score
}通过实现上面的内容,只有你的分数不会在碰撞时改变。
发布于 2016-01-02 04:07:24
如果游戏结束了,为什么物体会相撞呢?
让游戏在后台运行但停止交互的一个简单方法是在游戏结束时禁用玩家的对撞机。
GetComponent<Collider>().enabled = false;另一个解决方案是检查游戏是否运行,然后再加分。
void OnCollisionEnter (Collision col)
{
if(!gameRunning)
return;
// score logic
}但是,我建议您将代码部分分开,以便能够控制游戏的不同状态。如果您不想使用状态机,您可以使用Enum State。这将使它更容易管理,因为你的游戏变得更加复杂。
发布于 2016-01-02 06:23:57
只要检查一下分数是否是< 10,我也建议把你所有的条件放在一个街区内:
void OnCollisionEnter (Collision col)
{
if ((col.gameObject.name == "carA" || col.gameObject.name == "carB" || col.gameObject.name == "carC"
|| col.gameObject.name == "carD" || col.gameObject.name == "carE"
|| col.gameObject.name == "carF" || col.gameObject.name == "carG"
|| col.gameObject.name == "carH") && score < 10 )
{
score += 10;
}
//Rest of the code
}https://stackoverflow.com/questions/34562238
复制相似问题