【问题标题】:Getting returned "[object Object] NaN" - Javascript返回“[object Object] NaN” - Javascript
【发布时间】:2015-05-09 03:55:09
【问题描述】:

正如标题所说,当我执行我的代码时,我会在控制台中返回"[object Object] NaN"(在控制台中带有引号)

由于我总是不确定要发送什么代码,因为它们可能涉及其他因素,所以我将把我所有的代码发布在

Main.Js

var ctx = document.getElementById("canvas").getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;

var zombie1 = new Rectangle(300, 300, 20, 50);
var border = new Rectangle(0, 0, window.innerWidth, window.innerHeight);
var debugPlayer = new Rectangle(400, 400, 50, 50);

zombie1.ChangeColour("rgb(0, 0, 255)");
border.ChangeColour("rgb(150, 150, 150)");
border.ChangeThickness(300);
debugPlayer.ChangeColour("rgb(100, 100, 100)")


var playerup = "false"
var playerdown = "false"
var playerleft = "false"
var playerright = "false"

var update = function(){

    if (playerup == "true"){
        debugPlayer.y = debugPlayer.y - 1
    };
    if (playerdown == "true"){
        debugPlayer.y = debugPlayer.y + 1
    };
    if (playerleft == "true"){
        debugPlayer.x = debugPlayer.x - 1
    };
    if (playerright == "true"){
        debugPlayer.x = debugPlayer.x + 1
    };

    DoCollision(debugPlayer, zombie1);

    var processedZombiewidth = parseInt(zombie1.width, 10);
    console.log(debugPlayer.x, zombie1.x, debugPlayer + processedZombiewidth); // I am getting "400, 300, "[object Object] NaN" in console.

};
var makeScreen = function(){
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

    border.Draw(ctx, "true");
    debugPlayer.Draw(ctx, "false")
    zombie1.Draw(ctx, "false");
};

var DoCollision = function(rect1, rect2){
    if (rect1.x > rect2.x){
        if (rect1.x < rect2.x + rect2.width){
            console.log("fat")
        };      
    };
};

var updateFunc = function(){
    update();
    makeScreen();
};



setInterval(function(){updateFunc();}, 1);




$(document).keyup(function(objEvent){
        objEvent ? keycode = objEvent.keyCode : keycode = event.keyCode; 
        console.log(keycode);

        if (keycode == 87){ //W
            playerup = "false"
        };
        if (keycode == 65){ //A
            playerleft = "false"
        };
        if (keycode == 83){ //S
            playerdown = "false"
        };
        if (keycode == 68){ //D
            playerright = "false"
        };

});
$(document).keydown(function(objEvent){
        objEvent ? keycode = objEvent.keyCode : keycode = event.keyCode;
        console.log(keycode);
        if (keycode == 87){ //W
            playerup = "true"
        };
        if (keycode == 65){ //A
            playerleft = "true"
        };
        if (keycode == 83){ //S
            playerdown = "true"
        };
        if (keycode == 68){ //D
            playerright = "true"
        };
});

矩形.js

Rectangle = function(x, y, w, h){

this.colour = "rgb(0, 0, 0)"
this.thickness = 1

this.x = x;
this.y = y;
this.w = w;
this.h = h; 

this.Draw = function(ctx, hollow){
    ctx.fillStyle = this.colour;
    ctx.strokeStyle = this.colour;

    if (hollow == "true"){
        ctx.lineWidth = this.thickness
        ctx.strokeRect(this.x, this.y, this.w, this.h);
    };
    if (hollow == "false"){
        ctx.fillRect(this.x, this.y, this.w, this.h)
    };

};
this.ChangeColour = function(colour){
    this.colour = colour
};
this.ChangeThickness = function(thickness){
    this.thickness = thickness
};

【问题讨论】:

  • 你能发布一个说明错误的小提琴吗?
  • 仅供参考,javascript 实际上有布尔值,你不必使用字符串!
  • debugPlayer 是一个对象,而processedZombiewidth 似乎是NaN。而({} + NaN) 产生"[object Object]NaN"
  • 至于问题,如果返回,你有debugPlayer + processedZombiewidth,如果你看debugPlayer,它显然是一个对象,然后你有processedZombiewidth,它应该是一个数字,你不能把一个数字和一个对象加在一起,然后期望得到一些有意义的东西。然后你有parseInt(zombie1.width, 10),当然zombie1没有在代码中的任何地方定义,所以你得到NaN
  • 这是错误的,因为我使用的 objects x 变量是一个 int 并且 processeszombiewidth 也是一个 int...所以我不知道为什么我会得到我正在得到的东西

标签: javascript canvas nan parseint


【解决方案1】:

这里有两个错误:

1) zombie1 没有属性width,只有w。使用parseInt() 转换非数字值将产生NaN

2) 您正在将数字(或者更确切地说是 NaN)添加到对象 debugPlayer。这无论如何都会产生NaN

要解决此问题,请尝试将 w 添加到 debugPlayer 的 x

//var processedZombiewidth = parseInt(zombie1.width, 10);  not needed
console.log(debugPlayer.x, zombie1.x, debugPlayer.x + zombie1.w);

【讨论】:

  • 谢谢!我希望这是一个合理的问题,我没有浪费你的时间,但你帮了我很多。谢谢!
猜你喜欢
  • 2013-09-13
  • 2020-07-07
  • 1970-01-01
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2015-07-16
相关资源
最近更新 更多