回合结束后如何获取玩家的分数?

问题描述 投票:0回答:1

我是一个相当新的程序员,试图通过在C#中使用MonoGame来制作一个非常简单的游戏。我的问题是,我想在玩家死亡后访问该玩家的分数,但是我不确定该怎么做。我正在特别努力的是这条线:if (highscore.EnterUpdate(gameTime, player.Points)) //'player.Points' is my guess on how I access the points the player got in the game, but it's probably supposed to be something else.我想访问这些点,因为然后可以将它们添加到我的高分列表中。

[运行游戏时,我收到错误消息:“ System.NullReferenceException:'对象引用未设置为对象的实例'“玩家”为空。“,在上述行中,player.Points

非常感谢您的帮助! (我不知道下面的所有代码是否都与解决此问题有关,但是我已经尽力了。也许您需要更多?)]] >>

//File name: Game1.cs

    public class Game1 : Game
    {
        HighScore highscore;
        Player player;

        protected override void Update(GameTime gameTime)
        {
            switch (GameElements.currentState)
            {
                //...
                    switch (currentState)
                    {
                        case State.EnterHighScore:
                            if (highscore.EnterUpdate(gameTime, player.Points)) //here
                                currentState = State.PrintHighScore;
                            break;
                        default:
                            //...
                    }
            }
        }
    }

//File name: GameElements.cs
//Example

            foreach(GoldCoin gc in goldCoins.ToList()) //everytime the player collides with a coin
            {
                if (gc.IsAlive)
                {
                    gc.Update(gameTime);
                    if (gc.CheckCollision(player))
                    {
                        goldCoins.Remove(gc);
                        player.Points++; //the score increases when you collect a coin, because of this i assumed 'player.Points' was to be used in the code above.
                    }
                }
                else goldCoins.Remove(gc);
            }
//File name: Player.cs
    public class Player : PhysicalObject
    {
        int points = 0;
        //...
        public int Points //here, I assumed I could get the total score from this property?
        {
            get { return points; }
            set { points = value; }
        }
    }
//File name: HighScore.cs

class HSItem //might not be relevant, HSItem contains data about a person in the highscore list
{
    string name;
    int points;

    public string Name { get { return name; } set { name = value; } }

    public int Points { get { return points; } set { points = value; } }

    public HSItem(string name, int points)
    {
        this.name = name;
        this.points = points;
    }
}
//...
    void Add(int points) //here, is called from EnterUpdate()
    {
        HSItem temp = new HSItem(name, points);
        highscore.Add(temp); //Add() in this case belongs to List
        Sort();

        if (highscore.Count > maxInList)
        {
            highscore.RemoveAt(maxInList);
        }
    }

    public bool EnterUpdate(GameTime gameTime, int points) //here, 
    { 
        char[] key = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                       'L', 'M', 'N', 'O', 'P',  'Q', 'R', 'S', 'T', 'U',
                       'V', 'X', 'Y', 'Z'};

        if (CheckKey(Keys.Down, gameTime))
        {
            key_index++;
            if (key_index >= key.Length)
                key_index = 0;
        }

        if (CheckKey(Keys.Up, gameTime))
        {
            key_index--;
            if (key_index <= 0)
                key_index = key.Length - 1;
        }

        if (CheckKey(Keys.Enter, gameTime))
        {
            name += key[key_index].ToString();
            if (name.Length == 3)
            {
                Add(points); //here
                name = "";
                currentChar = "";
                key_index = 0;
                return true;
            }
        }
        currentChar = key[key_index].ToString();
        return false;
    }
//...

我是一个相当新的程序员,试图通过在C#中使用MonoGame来制作一个非常简单的游戏。我的问题是我想在玩家死亡后访问该玩家的分数,但是我不确定该怎么做...

c# monogame
1个回答
0
投票

请确保已在代码中的某个位置分配了Game1.player。 C#中的变量具有默认值,直到您显式分配它们为止;对于类,默认值为null。如果您尚未访问player = new Player(),则需要先在某个地方致电player

© www.soinside.com 2019 - 2024. All rights reserved.