我在学校创建的游戏的highscore
代码存在问题。
当游戏结束时,由于其保护级别,它无法访问HighScore
。
这是给我一个错误的代码:
public static HighScore LoadHighScores(string filename)
{
HighScore data;
// Get the path of the save game
string fullpath = "highscores.xml";
// Open the file
FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate, FileAccess.Read);
try
{
// Read the data from the file
XmlSerializer serializer = new XmlSerializer(typeof(HighScore));
data = (HighScore)serializer.Deserialize(stream);
}
finally
{
// Close the file
stream.Close();
}
return (data);
}
当我运行代码并死掉时,我调用此代码来保存得分:
public void SaveHighScore(int score)
{
// Create the data to saved
HighScore data = LoadHighScores(HighScoresFilename);
int scoreIndex = -1;
for (int i = data.Count--; i > -1; i--)
{
if (score >= data.Score[i]) //score.getScore();
{
scoreIndex = i;
}
}
if (scoreIndex > -1)
{
//New high score found ... do swaps
for (int i = data.Count--; i > scoreIndex; i--)
{
data.PlayerNames[i] = data.PlayerNames[i - 1];
data.Score[i] = data.Score[i - 1];
}
data.PlayerNames[scoreIndex] = PlayerName; //Retrieve User Name Here
data.Score[scoreIndex] = score; // Retrieve score here
SaveHighScores(data, HighScoresFilename);
}
}
SaveHighScores
看起来像这样:
public static void SaveHighScores(HighScore data, string filename)
{
// Get the path of the save game
string fullpath = "highscores.xml";
// Open the file, creating it if necessary
FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate);
try
{
// Convert the object to XML data and put it in the stream
XmlSerializer serializer = new XmlSerializer(typeof(HighScore));
serializer.Serialize(stream, data);
}
finally
{
// Close the file
stream.Close();
}
}
答案 0 :(得分:0)
请检查此链接 Public Class - "is inaccessible due to its protection level. Only public types can be processed."
你可以在那里找到答案....作为摘要,将您的HighScore标记为公开。