动态显示文本,然后退出空白

时间:2019-02-06 02:48:36

标签: c# unity3d

我试图统一制作一个简单的亚军风格游戏,但遇到一个问题:当玩家“死亡”时,比分和游戏会在屏幕上闪烁,交替显示帧。

我是C#的新手,真的不知道还能尝试什么。

using UnityEngine;
using UnityEngine.UI;

public class PlayerCollision : MonoBehaviour{
        public PlayerMovement movement;
        private int lives = 0;
        public Transform player;
        public Text scoreText;
        public Rigidbody rb = null;
        private bool abort = false;
    void OnCollisionEnter (Collision collisionInfo) {
        if (collisionInfo.collider.tag == "Obstacle") {
            if (lives >= 3) {
                movement.enabled = false;
                Debug.Log("We Hit the thing");
                Debug.Log(movement);
                rb.AddForce(0, 0, 0);
                transform.position = new Vector3(0,1,-30);
                scoreText.text = "Level failed - Press C to try again";

                rb.AddForce(0, 0, 0);
            } else {
                lives = lives + 1;
            }
        }
    }

void FixedUpdate()
    {
        if (scoreText.text == "-30") {
            //DISPLAY TEXT AND LEAVE THE VOID FIXEDUPDATE()
            scoreText.text = "Level failed - Press C to try again";
            return;
        } else {
            //CONTINUE RUNNING THIS UNTILL -30 IS REACHED
            scoreText.text = player.position.z.ToString("0");
        }
    }

void Update() {
    if ( Input.GetKey("c") ) {
        movement.enabled = true;
            transform.position = new Vector3(0,1,0);

            lives = 0;
        }
    }
}

这是一个github链接 https://raw.githubusercontent.com/jackcrane/jackcrane.github.io/master/PlayerCollision.cs

2 个答案:

答案 0 :(得分:0)

玩家死亡时, def __str__(self): #some code 将设置为“级别失败-按C再试一次”,因此将执行以下代码:

__str__

然后scoreText.text将更改为“ -30”

因此下一个固定更新框架将执行:

 else {
     //CONTINUE RUNNING THIS UNTILL -30 IS REACHED
     scoreText.text = player.position.z.ToString("0");
 }

现在,文本已重新更改为“级别失败-按C再试一次”,循环。

我不知道如何解决此问题,因为逻辑在您脑海中。

答案 1 :(得分:0)

解决方案

始终尝试在PlayerPrefs中得分,这是存储得分的统一性的重要特征。万一你不想显示分数或计数。只需使用文本的对象并将空字符串设置为文本即可。很简单。使用PlayerPref的好处是它将存储播放器的最终得分或结束得分,然后甚至将字符串设为空。分数仍将单独存储。因此,只要您想要该分数值,就使用Playerpref.getInt(“ Score”); 分数将在任何变量中获得,并可以进一步使用。

使字符串为空的代码

if (scoreText.text == "-30") {
    //DISPLAY TEXT AND LEAVE THE VOID FIXEDUPDATE()
    scoreText.text = "";
    return;
}

PlaerPref的代码:-

var myVariable : float;

PlayerPrefs.SetFloat("Player Score", 10.0);

myVariable = PlayerPrefs.GetFloat("Player Score");

print (myVariable);

结论

可以通过playerprefs函数获取并设置结果。它使用关键概念并存储在文件中。