Javascript - 属性 的对象具有 NaN 的值,即使我明确地为其分配了一个数值

Javascript - Property of an object has the value of NaN, even when I explicitly assign it a number value

我在业余时间制作一个基础平台游戏,目前正在尝试添加引力。游戏在 canvas 中运行,因此我使用黑色像素作为实体对象。我正在制作的精灵应该在没有接触到实心黑线时掉落。为此,我使用 context.getImageData() 以及精灵对象的 x 位置、y 位置、宽度和高度属性。当我创建玩家 sprite 时,我为它指定了 10 的 x 位置、10 的 y 位置、50 的宽度和 100 的高度: var player = new Sprite (10, 10, 50, 100); 我的问题是,当我尝试绘制 sprite 或使用它时y position in context.getImageData() 它表示y 位置是Nan。下面的代码是只包含相关变量的简化版本。

//-----------SETUP-------------//
//----GLOBAL VARIABLES---//
var gravityValue = 1;  //amount of change that gravity makes per move
var fallBoxThickness = 1; //thickness of fall-box check

var canvas = document.getElementById("canvas");  //get the canvas object
 canvas.width = 800;  //set the canvas width
 canvas.height = 600; //set the canvas height
var context = canvas.getContext("2d");     //get the canvas's context, 2d


//--------OBJECTS---------//
function Sprite (x,y,width,height) {  //sprite object
 //components
 this.x = x;    //sprite x position
 this.y = y;    //sprite y position
 this.width = width;  //image width
 this.height = height; //image height
 this.dx = 0;   //sprite x movement
 this.dy = 0;   //sprite y movement
 this.gravityEnabled = true; //allows sprite falling, must be manually disabled
 
 //methods
 this.draw = function () { //draw method
    //context.drawImage(this.src, this.x, this.y);
          //draws the sprite to the canvas
      //it used to do the above, now it outputs it to the output span
      document.getElementById("output").innerHTML = this.y;
 };
 
 this.move = function () { //move method
  if (this.gravityEnabled === true) { //if the sprite can fall
   var hit = false;   //a hit is not yet detected
   var checkSpace = context.getImageData( //get pixels to see if the sprite ...
    this.x, parseInt(this.y + this.height),   //... will hit an object below it's lower edge
    this.x + this.width, this.y + this.height, + fallBoxThickness);
   
   var i = 0; //set i to 0
   while (i < checkSpace.length && hit === false) { 
    //while the check hasn't finished and a hit isn't detected
    if (checkSpace[i] < 5  //
      && checkSpace[i+1] < 5 //if there is a black pixel
      && checkSpace[i+2] < 5) { //
     hit = true;  //record that there is a hit
    }
    i = i + 4; //add 4 to i
   }
   if (hit === false) { //if the check didn't hit
    this.dy += gravityValue; //add to the fall of the sprite
   } else {
    this.dy = 0;
   }
  }
  this.y += this.dy;
 };
}

//------------PLAYER CREATION---------//
var player = new Sprite (10, 10, 50, 100); //create the player object

//--------FUNCTIONS--------//
function drawCanvas () { //draw everything on the canvas
 player.draw(); //draw the player
}

function moveSprites () {
 player.move();
}
//----------MAIN-----------//
function main () {
 moveSprites(); //move the sprites
 drawCanvas(); //draw the screen
}

setInterval(main,100);  //run main 10 times a second,
   //start the program
<title>ptf2</title>
<canvas id="canvas" style="border:1px solid black;"></canvas>
there was images here but the javascript never gets far enough to render them because of the y-position error.<br>
Instead I am just outputting the value of the player sprite's y-position to the span below.<br>
:<span id="output">this is to prevent it from occasionally being undefined</span>

另外,我真的不确定为什么这个版本能用而不是我以前的版本,所以现在我将包括完整版本:

//-----------SETUP-------------//
//----IMAGES----//
document.getElementById("map").style.display = "none";  //hide the map image
document.getElementById("sprite").style.display = "none"; //hide the sprite image

//----GLOBAL VARIABLES---//
var gravityValue = 1;  //amount of change that gravity makes per move
var fallBoxThickness = 1; //thickness of fall-box check

var canvas = document.getElementById("canvas");  //get the canvas object
 canvas.width = 800;  //set the canvas width
 canvas.height = 600; //set the canvas height
var context = canvas.getContext("2d");     //get the canvas's context, 2d


//--------OBJECTS---------//
function Sprite (x,y,src,width,height,dx,dy) {  //sprite object
 //components
 this.x = x;    //sprite x position
 this.y = y;    //sprite y position
 this.src = document.getElementById(src); //sprite source image
 this.width = width;  //image width
 this.height = height; //image height
 this.dx = dx;   //sprite x movement
 this.dy = dy;   //sprite y movement
 this.gravityEnabled = true; //allows sprite movement, must be manually disabled
 
 //methods
 this.draw = function () { //draw method
  context.drawImage(this.src, this.x, this.y); //draws the sprite to the canvas
 };
 
 this.move = function () { //move method
  if (this.gravityEnabled === true) { //if the sprite can fall
   var hit = false;   //a hit is not yet detected
   var checkSpace = context.getImageData( //get pixels to see if the sprite ...
    this.x, parseInt(this.y + this.height),   //... will hit an object below it's lower edge
    this.x + this.width, this.y + this.height, + fallBoxThickness);
   
   var i = 0; //set i to 0
   while (i < checkSpace.length && hit === false) { 
    //while the check hasn't finished and a hit isn't detected
    if (checkSpace[i] < 5  //
      && checkSpace[i+1] < 5 //if there is a black pixel
      && checkSpace[i+2] < 5) { //
     hit = true;  //record that there is a hit
    }
    i = i + 4; //add 4 to i
   }
   if (hit === false) { //if the check didn't hit
    this.dy += gravityValue; //add to the fall of the sprite
   } else {
    this.dy = 0;
   }
  }
  this.y += this.dy;
 };
}

var player = new Sprite (10, 10, "sprite", 50, 100); //create the player object
var map = new Sprite (0, 0, "map", 800, 600);   //create the map sprite
 map.gravityEnabled = false;   //prevents the map from falling
//--------FUNCTIONS--------//
function drawCanvas () { //draw everything on the canvas
 map.draw();  //draw the map
 player.draw(); //draw the player
}

function moveSprites () {
 player.move();
}
//----------MAIN-----------//
function main () {
 moveSprites(); //move the sprites
 drawCanvas(); //draw the screen
 alert("run");
}

setInterval(main,100);  //run main 10 times a second,
   //start the program
<title>ptf2</title>
<canvas id="canvas" style="border:1px solid black;"></canvas>
<img id="map" src = "assets/map03.png">
<img id="sprite"src = "assets/sprite.png">
<script src = "main.js"></script>

任何关于为什么不能使用完全相同的值的启发将不胜感激。谢谢。

您的 Sprite 构造函数有 7 个参数,但您仅使用 5 个参数构造它。变量dxdyundefined,所以它们在代数运算后产生NaN