Javascript 即使条件为';t遇见

Javascript 即使条件为';t遇见,javascript,Javascript,我正在通过制作一个类似流氓的地下城游戏来练习编写Javascript,但是当我想要创建怪物时,有一个问题。我编写了一个while循环,例如: this.getRandomCoordinatesInRoom = function(roomNumber) { var validLocationFound = false; while (!validLocationFound){ // Generate co-ords in room first x = Math.floor(Math.ra

我正在通过制作一个类似流氓的地下城游戏来练习编写Javascript,但是当我想要创建怪物时,有一个问题。我编写了一个while循环,例如:

this.getRandomCoordinatesInRoom = function(roomNumber) {
var validLocationFound = false;

while (!validLocationFound){
  // Generate co-ords in room first
  x = Math.floor(Math.random() * (this.roomList[roomNumber].width) + (this.roomList[roomNumber].posX));
  y = Math.floor(Math.random() * (this.roomList[roomNumber].height) + (this.roomList[roomNumber].posY));

  // Find location of (x,y) in Dungeon Array

  //alert(arrayLocation);
  var tmpX = ((this.roomList[roomNumber]).posX + x);
  var tmpY = ((this.roomList[roomNumber]).posY + y);
  var arrayLocation = ((tmpY * 80) + tmpX);
  //var arrayLocation = ((this.roomList[roomNumber].posX + x) + (80 * (this.roomList[roomNumber].posY + y)));
  if (this.dungeonArray[(tmpY + tmpX)] === "floor") {
    validLocationFound = true;
  };

  if ((x<this.roomList[roomNumber].posX) || (x>(this.roomList[roomNumber].posX + this.roomList[roomNumber].width))){
    alert("x out of bounds");
  };

  if ((y<this.roomList[roomNumber].posY) || (y>(this.roomList[roomNumber].posY + this.roomList[roomNumber].height))){
    alert("y out of bounds");
  };
  writeToScreen("Room upper left corner = " + (this.roomList[roomNumber].posX).toString() + "," + (this.roomList[roomNumber].posY).toString(),10);
  return [x,y];

  if (!(getTileAt(arrayLocation) === "floor")){
    alert("It messed up");
  }
};
this.getRandomCoordinateInRoom=函数(roomNumber){
var validLocationFound=false;
而(!validLocationFound){
//首先在文件室中生成co ORD
x=Math.floor(Math.random()*(this.roomList[roomNumber].width)+(this.roomList[roomNumber].posX));
y=Math.floor(Math.random()*(this.roomList[roomNumber].height)+(this.roomList[roomNumber].posY));
//在副本阵列中找到(x,y)的位置
//警报(阵列定位);
var tmpX=((this.roomList[roomNumber]).posX+x);
var tmpY=((this.roomList[roomNumber]).posY+y);
变量排列位置=((tmpY*80)+tmpX);
//var arrayLocation=((this.roomList[roomNumber].posX+x)+(80*(this.roomList[roomNumber].posY+y));
如果(这个地下城阵列[(tmpY+tmpX)]=“地板”){
validLocationFound=true;
};
如果((x(this.roomList[roomNumber].posX+this.roomList[roomNumber].width))){
警报(“x出界”);
};
如果((y(this.roomList[roomNumber].posY+this.roomList[roomNumber].height))){
警告(“y超出范围”);
};
writeToScreen(“房间左上角=“+(this.roomList[roomNumber].posX).toString()+”,“+(this.roomList[roomNumber].posY).toString(),10);
返回[x,y];
如果(!(getTileAt(arrayLocation)=“floor”)){
警惕(“它搞砸了”);
}
};

代码随机生成一个x,y坐标并将其转换为一个数字(我的地下城数组是一维的,0-79跨,然后80是一个新行)。但是,即使代码生成一个无效的坐标(!=“floor”),它仍然会完成函数,就好像它返回了真的一样。这是为什么?

你的函数返回
[x,y]
从while循环内部。在循环外部声明变量,然后从循环外部返回值。否则,返回值为
validLocationFound
为true时

var x, y ;
while(...) {
    ...
}
return [x, y];

为什么它们都是
if
条件?你不可能让它们成为
else if
?因为你返回[x,y],不管之前发生了什么(没有条件)。所以循环只运行一次,直到返回。@WaKai:看起来像是我的答案…
return
结束了你的循环。我是新手,所以我只是想让基础工作正常,这就是为什么我没有做其他if。而x,y返回将坐标写在我屏幕的右侧,看看坐标是否相同,这样我就知道了它们被复制了谢谢,很多人说是退货把事情搞砸了。没有退货,它似乎不会运行,所以我会在其他地方查找问题。非常感谢大家!:)编辑了答案。措辞非常糟糕。你需要从循环之外返回。