如何更新我的GUI得分一分在一个时间? [英] How can I update my GUI score one point at a time?

查看:139
本文介绍了如何更新我的GUI得分一分在一个时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个球员在我的游戏做的事情接收点的X数量我想要的记分牌显示更新0每个号码 - X很简单。我要为模拟式的计分制像那些老钟,你可以观看数字变化,但变化会很快发生,因为几百点可以同时添加。

If a player receives X amount of points for doing something in my game I want the scoreboard to update showing each number from 0 - X very briefly. I'm going for an analog style scoring system like those old clocks where you can watch the numbers change, except the change will happen quickly because hundreds of points can be added at the same time.

目前,点的X数量的即时更新点奖励时:

Currently, X amount of points are updated instantly when points are awarded:

/****************************** Platform Collision ***************************/

void OnCollisionEnter2D(Collision2D coll)
    {
        foreach(ContactPoint2D contact in coll.contacts)
        {   
            newPlayerHeight = transform.position.y;

            // Don't count the first jump
            if(newPlayerHeight < 0){
                newPlayerHeight = 0;
            }

            // If the player jumps down, don't reduce points
            // Add a tolerance for variable collision positions on same platform
            if(newPlayerHeight < oldPlayerHeight + -0.05f){
                newPlayerHeight = oldPlayerHeight;
            }

            // Send the height to the Score class
            Score.SP.updateScore(newPlayerHeight);

            oldPlayerHeight = newPlayerHeight;
        }
    }

/******************************* Score class *********************************/

public void updateScore (float newScore)
    {
        // Display GUI score
        score = (int)(newScore * 76);
        guiText.text = "Score" + score;

    }



我用一个for循环,试图实现搞砸周围这可是无处接近。

I messed around with a for-loop to try and achieve this but got nowhere close.

推荐答案

我刚刚创造了这个代码,它解决您的问题。我目前正在使用它在我自己的比赛。

I just created this code, which solves your problem. I am currently using it in my own game.

ScoreManager 类的主要思想是保持呼吸道得分和更新每一帧。它采用了的Stack 来保持比分的轨道以复加,这样,我们没有一个神奇的数字增加了比分。

The main idea of the ScoreManager class is to keep tract of the score and update it every frame. It uses a Stack to keep track of the score to be added, that way we don't have a magic number increasing the score.

所以,当玩家赢得一个新的点只需调用 addScore()函数,则 ScoreManager 课程将全部由自己来处理更新。

So when the player earns a new point just call the addScore() function then the ScoreManager class will handle the updating all by itself.

这将有一个线程始终运行将从 currentScore 增加 newScore 按位数字,这样,就像你想要的改变是由用户观察到。

It will have a thread always running which will increase from currentScore to newScore digit by digit, that way the change is observable by users like you wanted.

加入线程减少滞后您遇到其他问题,您发布Why是我的比赛落后时,大我调用一个方法?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

public class ScoreManager : MonoBehaviour 
{
    public GUIText score;

    private int currentScore = 0;
    public int scoreToUpdate = 0;

    private Stack<int> stack;

    private Thread t1;
    private bool isDone = false;

    void Awake() 
    {
        stack = new Stack<int>();
        t1 = new Thread(updateScore);
        t1.Start();
    }

    private void updateScore()
    {
        while(true)
        {
            if(stack.Count > 0)
            {
                int newScore = stack.Pop() + currentScore;

                for(int i = currentScore; i <= newScore; i++)
                {
                    scoreToUpdate = i;
                    Thread.Sleep(100); // Change this number if it is too slow.
                }

                currentScore = scoreToUpdate;
            }

            if(isDone)
                return;
        }
    }

    void Update() 
    {
        score.text = scoreToUpdate + "";
    }

    public void addScore(int point)
    {
        stack.Push(point);
    }

    public void OnApplicationQuit()
    {
        isDone = true;
        t1.Abort();
    }
}



我打算把这个代码放到一个的辛格尔顿,以便有只有一个这个类在我的游戏的实例。我强烈建议你这样做。

I plan to turn this code into a Singleton so that there is only one instance of this class in my game. I highly recommend you do the same.

这篇关于如何更新我的GUI得分一分在一个时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆