简体   繁体   English

为什么我得到 integer 的 NaN?

[英]Why am I getting NaN for integer?

This is my code:这是我的代码:

function setup() {
  createCanvas(windowWidth, windowHeight);
  noStroke();
}

var momentum = [0, 0]; //x and y speed
var pos = [650, 300]; //player x and y
var enemiesX = []; //enemies x's
var enemiesY = []; //enemies y's

function draw() {
  background(200);
  momentum[0] = (pos[0] - mouseX) / 5;
  pos[0] =+ momentum;
  fill(115);
  ellipse(pos[0], pos[1], 30, 30);
  text(pos[0] + ", " + pos[1], 20, 20);
}

I am using arrays to compact similar x's and y's that are paired.我正在使用 arrays 来压缩配对的类似 x 和 y。 I was testing with the text function, and I realized the first of every array is NaN.我正在使用文本 function 进行测试,我意识到每个数组的第一个是 NaN。 I know its this:我知道它是这样的:

  momentum[0] = (pos[0] - mouseX) / 5;
  pos[0] =+ momentum;

but what is wrong?但有什么问题?

Also, before people spam this with flags saying its a duplicate, the simplicity means no other question of this nature had a relevant answer.此外,在人们用标记说它是重复的垃圾邮件之前,简单意味着没有其他这种性质的问题有相关的答案。

Where is mouseX defined? mouseX在哪里定义? It it's undefined , any number plus undefined will equal zero.它是undefined ,任何数字加上undefined都将等于零。

Additonally, =+ is not the same as += and will cause further issues此外, =++=不同,会导致更多问题

Use parsetInt to avoid NaN and add further check for NaN使用parsetInt避免NaN并添加对NaN的进一步检查

   createCanvas(windowWidth, windowHeight);
    noStroke();
}

var momentum = [0, 0]; //x and y speed
var pos = [650, 300]; //player x and y
var enemiesX = []; //enemies x's
var enemiesY = []; //enemies y's

function draw() {
    background(200);
    mouseX = parseInt(mouseX);
    if(mouseX === NaN){
        alert('Not a number exception');
    }
    else{
        momentum[0] = (pos[0] - mouseX) / 5;
        pos[0] =+ momentum;
        fill(115);
        ellipse(pos[0], pos[1], 30, 30);
        text(pos[0] + ", " + pos[1], 20, 20);
    }
}

There are two issues with your code.您的代码有两个问题。 The first is that you're using =+ instead of += .首先是您使用=+而不是+= The second is that momentum is an array, not a number.第二个是momentum是一个数组,而不是一个数字。 This is what your code is doing right now in the two lines you highlighted:这就是您的代码现在在您突出显示的两行中所做的事情:

  • Evaluate (pos[0] - mouseX) / 5评估(pos[0] - mouseX) / 5
  • Save that value in the 0 position of the momentum array.将该值保存在momentum数组的 0 position 中。
  • Evaluate +momentum评估+momentum
  • Save that value in the 0 position of the pos array.将该值保存在pos数组的 0 position 中。

At the third bullet point, what happens is that +momentum evaluates to NaN .在第三个要点,发生的情况是+momentum评估为NaN Then you're saving that value in the pos array.然后你将该值保存在pos数组中。 The first of the two lines is fine.两行中的第一行很好。 I think that what you're looking to do is to add momentum[0] to pos[0] , which can be done like this:我认为您想要做的是将momentum[0]添加到pos[0] ,可以这样完成:

  pos[0] += momentum[0];

I'm not certain what you intend, but my guess is that you want to make the circle go toward the cursor, not away from it.我不确定你的意图是什么,但我的猜测是你想让圆圈 go朝向cursor,而不是远离它。 In that case, you have the sign of the momentum wrong, it should be like this:在这种情况下,您的动量符号错误,应该是这样的:

  momentum[0] = (mouseX - pos[0]) / 5;
  pos[0] += momentum[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM