游戏设置文件问题
您好,我正在开发一个设置加载器系统,但它不想工作,我尝试了不同的方法,这个看起来最好,除了它不起作用,我可以请一些帮助吗,因为我需要完成这项工作到一天结束时。
这是代码:
public GameSettings getGameSettings() throws Exception
{
Resolution GameResolution;
GameSettings gs = new GameSettings(true, true, false, 50, new Resolution(800, 600));
for(int k = 0; k < lines.size(); k++)
{
String[] s1 = lines.get(k).split("=");
if(s1[0].equals("volume"))
{
gs.setVolume(Integer.parseInt(s1[1]));
}
if(s1[0].equals("musicOn"))
{
gs.setMusicOn(Boolean.parseBoolean(s1[1]));
}
if(s1[0].equals("soundOn"))
{
gs.setSoundOn(Boolean.parseBoolean(s1[1]));
}
if(s1[0].equals("aOpenGL"))
{
gs.setAOpenGL(Boolean.parseBoolean(s1[1]));
}
if(s1[0].equals("GameRes"))
{
String[] s2 = s1[1].split("x");
GameResolution = new Resolution(Integer.parseInt(s2[1]), Integer.parseInt(s2[1]));
gs.setGameResolution(GameResolution);
}
if(s1[0].startsWith("#"))
{
continue;
}
else
{
System.err.println("WARNING: Unknow setting in settings file ('" + s1[0] + "')");
continue;
}
}
return gs;
}
,这是“settings.dat”文件内容:
#************************** Settings
#Settings file version 1.5
musicOn=false
volume=75
soundOn=true
aOpenGL=false
GameRes=1000x800
#end of settings file
最后是控制台输出:
WARNING: Unknow setting in settings file ('musicOn')
WARNING: Unknow setting in settings file ('volume')
WARNING: Unknow setting in settings file ('soundOn')
WARNING: Unknow setting in settings file ('aOpenGL')
WARNING: Unknow setting in settings file ('GameRes')
Hi I'm working on a settings loader system and it doesn't want to work, I have tried different ways of doing it and this one looks the best, except it not working, can I please have some help because I need this done by the end of the day.
Here is the code:
public GameSettings getGameSettings() throws Exception
{
Resolution GameResolution;
GameSettings gs = new GameSettings(true, true, false, 50, new Resolution(800, 600));
for(int k = 0; k < lines.size(); k++)
{
String[] s1 = lines.get(k).split("=");
if(s1[0].equals("volume"))
{
gs.setVolume(Integer.parseInt(s1[1]));
}
if(s1[0].equals("musicOn"))
{
gs.setMusicOn(Boolean.parseBoolean(s1[1]));
}
if(s1[0].equals("soundOn"))
{
gs.setSoundOn(Boolean.parseBoolean(s1[1]));
}
if(s1[0].equals("aOpenGL"))
{
gs.setAOpenGL(Boolean.parseBoolean(s1[1]));
}
if(s1[0].equals("GameRes"))
{
String[] s2 = s1[1].split("x");
GameResolution = new Resolution(Integer.parseInt(s2[1]), Integer.parseInt(s2[1]));
gs.setGameResolution(GameResolution);
}
if(s1[0].startsWith("#"))
{
continue;
}
else
{
System.err.println("WARNING: Unknow setting in settings file ('" + s1[0] + "')");
continue;
}
}
return gs;
}
, this is the 'settings.dat' file content:
#************************** Settings
#Settings file version 1.5
musicOn=false
volume=75
soundOn=true
aOpenGL=false
GameRes=1000x800
#end of settings file
and finally the console output:
WARNING: Unknow setting in settings file ('musicOn')
WARNING: Unknow setting in settings file ('volume')
WARNING: Unknow setting in settings file ('soundOn')
WARNING: Unknow setting in settings file ('aOpenGL')
WARNING: Unknow setting in settings file ('GameRes')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将这些
if
与else
链接起来。否则,每次循环运行时都会对所有 if 进行求值,因此对于所有非注释行都会显示
Unkown
消息,无论它们之前是否匹配某些内容。You need to chain those
if
s withelse
s.Otherwise, all the ifs are evaluated each time the loop runs, so the
Unkown
message will show for all lines that are not comments, regardless of whether they matched something before or not.显然,在您的代码中,对于任何未注释的行,您都会打印一条警告。
我猜你实际上想每次都使用
else if
而不是if
,因为在你的代码中,最后的else
指的是if仅限以上。
Apparently, in your code for any non-commented line you print a warning.
I guess you actually wanted to use
else if
instead ofif
every time, as in your code the finalelse
refers to theif
above only.