我已经在多个场景中多次运行代码并获得了相同的结果。它应该做的是向右移动,当它到达它应该转动的点时,它就会转向。相反,它给了我一个错误对话框,我指示它在遇到未识别的东西时给出。它似乎正在跳过代码的一部分,告诉它执行一个单独的方法。我还有其他三种方法可以在不同的方向做同样的事情。
详细说明情况如何运作。我开始游戏,它加载了玩家的水平。我使用某种形式的输入来执行下面的代码。角色向右移动,但即使等级表明应该移动,也会拒绝移动。
编辑:我修改了代码以使用.equals()而不是==。我发现这个问题实际上是因为我忘记了java是如何做x轴和y轴的。我感谢那些在编码中修复我的缺陷的人,这真的很有帮助。public static void moveRight(){
move = true;
while(move == true){
Point character = load.getCharacter();
Point space = character;
space.x += 20;
String name = load.getSpace(space);
System.out.println(name);
if(name.equals("empty")){
load.setCharacter(space);
}else if(name.equals("fTurnUp")){
load.setCharacter(space);
move = false;
moveUp();
}else if(name.equals("fTurnDown")){
load.setCharacter(space);
move = false;
moveDown();
}else if(name.equals("fTurnLeft")){
load.setCharacter(space);
move = false;
moveLeft();
}else if(name.equals("fTurnRight")){
load.setCharacter(space);
move = false;
moveRight();
}else if(name.equals("turn")){
load.setCharacter(space);
move = false;
}else if(name.equals("finish")){
load.setCharacter(space);
move = false;
Component frame = null;
JOptionPane.showMessageDialog(frame,
"Congratulations! You have completed the level.",
"Level Completed",
JOptionPane.PLAIN_MESSAGE);
}else if(name.equals("wall")){
move = false;
Component frame = null;
JOptionPane.showMessageDialog(frame,
"The level was not designed correctly. Click 'Ok' to return to main menu",
"Invalid Level",
JOptionPane.ERROR_MESSAGE);
}
}
}
答案 0 :(得分:1)
对于字符串比较的区别,您必须使用以下内容:
var results = {
a: [1, 2],
b: [2, 3]
};
var grouped = Object.keys(results).map(function(key) {
return [key, results[key].length];
});
document.body.innerHTML = JSON.stringify(grouped);
当您使用if(name.equals("empty")) {
load.setCharacter(space);
}
时,您正在比较值指向的引用,而==
正在比较实际值。
答案 1 :(得分:1)
尝试将比较器从==
更改为.equals()
。前者指向字符串的引用,而后者实际上比较值。
因此,它应该是:
if(name.equals("empty")){
load.setCharacter(space);
}else if(name.equals("fTurnUp")){
load.setCharacter(space);
move = false;
moveUp();
}else if(name.equals("fTurnDown")){
load.setCharacter(space);
move = false;
moveDown();
}else if(name.equals("fTurnLeft")){
load.setCharacter(space);
move = false;
moveLeft();
}else if(name.equals("fTurnRight")){
load.setCharacter(space);
move = false;
moveRight();
}else if(name.equals("turn")){
load.setCharacter(space);
move = false;
}else if(name.equals("finish")){
load.setCharacter(space);
move = false;
Component frame = null;
JOptionPane.showMessageDialog(frame,
"Congratulations! You have completed the level.",
"Level Completed",
JOptionPane.PLAIN_MESSAGE);
}else if(name.equals("wall")){
move = false;
Component frame = null;
JOptionPane.showMessageDialog(frame,
"The level was not designed correctly. Click 'Ok' to return to main menu",
"Invalid Level",
JOptionPane.ERROR_MESSAGE);
}
答案 2 :(得分:0)
尝试使用
MyString.equals("otherstring")
而不是==比较if语句中的字符串。
另外,对于布尔值,不需要写==,只需使用while(move)