我的loadgamesavegame方法遇到了严重的问题。

问题描述 投票:-2回答:1

我正试图用C#开发一个基于文本的游戏,但我很难让我的加载游戏允许我加载玩家,我的保存游戏可以保存多次。我有一个非常困难的时间让我的加载游戏允许我加载玩家,和我的保存游戏保存多个时间。

它也不能保存玩家的名字。我已经调试了5个小时,但没有成功......下面是代码。

    public static void saveGame()
    {
        PlayerClass p = new PlayerClass();
        //creates bin folder named textadsaves with player id
        BinaryFormatter binForm = new BinaryFormatter();
        string path = "textgameSaves/" + p.id + ".sf";

        //creates file in bin folder with player name or id and closes file
        FileStream file = File.Open(path, FileMode.OpenOrCreate);
        binForm.Serialize(file,p);
        file.Close();
    }

    public static PlayerClass loadGame( out bool newP)
    {
        newP = false;
        string gameSaves = "textgameSaves";
        Console.Clear();
        Console.WriteLine("Choose your Save Game.");
        //to select from a list of saves
        string[] paths = Directory.GetFiles(gameSaves);
        //convert paths into a player instance(file)
        List<PlayerClass> players = new List<PlayerClass>();
        int idCount = 0;
        BinaryFormatter binForm = new BinaryFormatter();
        //check every path 
        foreach (string p in paths)
        {
            FileStream file = File.Open(p, FileMode.Open);
            PlayerClass player = (PlayerClass)binForm.Deserialize(file);
            file.Close();
            players.Add(player);
        }
        //check for player save
        idCount = players.Count;
        while (true)
        {
            Console.Clear();
            Console.WriteLine("Choose your player");
            foreach (PlayerClass p in players)
            {
                Console.WriteLine(p.id + ": " + p.name);
            }
            Console.WriteLine("Please enter player name or id. (id:# or playername).\n Additionally 'Create' will start a new save.");
            string[] data = Console.ReadLine().Split(':');
            try
            {
                if (data[0]== "id")
                {
                    if (int.TryParse(data[1], out int id))
                    {
                        foreach(PlayerClass player in players)
                        {
                            if(player.id == id)
                            {
                                return player;
                            }
                        }
                        Console.Clear();
                        Console.WriteLine("not finding the character in file.\n press enter");
                        Console.ReadKey();
                    }
                    else
                    {
                        Console.WriteLine("Your id needs a number\n press enter to continue");
                        Console.ReadKey();
                    }
                }
                else if (data[0] =="create")
                {
                    PlayerClass newPlayer = startFun(idCount);
                    newP = true;
                    return newPlayer;

                }
                else
                {
                    foreach(PlayerClass player in players)
                    {
                        if(player.name == data[0])
                        {
                            return player;
                        }
                    }
                    Console.Clear();
                    Console.WriteLine("Not finding player in file.\n press enter");
                    Console.ReadKey();
                }
            }
            catch(IndexOutOfRangeException)
            {
                Console.WriteLine("Your id needs a number\n press enter to continue");
                Console.ReadKey();
            }
        }
c# debugging methods game-development
1个回答
0
投票

你最大的问题就在你的顶部。SaveGame 方法。

PlayerClass p = new PlayerClass();

你在实例化一个新的玩家,然后试图保存它。你实际上并没有保存游戏中的玩家。

而你要做的是获取你游戏中每一个玩家的列表,并保存每一个玩家,类似于你在加载这些玩家时的做法。

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