“得到回报”;[对象]楠“Javascript
正如标题所说,当我执行代码时,在控制台中返回“得到回报”;[对象]楠“Javascript,javascript,canvas,nan,parseint,Javascript,Canvas,Nan,Parseint,正如标题所说,当我执行代码时,在控制台中返回“[object]NaN”(在控制台中带有引号) 由于我总是不太确定要发送什么代码,因为它们可能涉及其他因素,所以我将把我所有的代码都发布进来 Main.Js var ctx = document.getElementById("canvas").getContext("2d"); ctx.canvas.width = window.innerWidth; ctx.canvas.height = window.innerHeight; var zom
“[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"
};
});
这里有两个错误: 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);
你能贴一张小提琴来说明错误吗?仅供参考,javascript实际上有布尔值,你不必使用字符串
debugPlayer
是一个对象,processedZombiewidth
似乎是NaN
。而({}+NaN)
产生“[object object]NaN”
对于这个问题,如果返回,跟踪一下,你有debugPlayer+processedZombiewidth
,如果你看debugPlayer
它显然是一个对象,然后你有processedZombiewidth
,它应该是一个数字,你不能把一个数字和一个对象加在一起,期望得到一些有意义的东西。然后你有了parseInt(zombie1.width,10)
,当然zombie1
没有在代码中的任何地方定义,所以你会发现这是错误的,因为我使用的是objects x变量,它是一个int,processedzombiewidth也是一个int。。。所以我不知道为什么我会得到我现在得到的谢谢!我希望这是一个合理的问题,我没有浪费你的时间,但你帮了我很多。谢谢
//var processedZombiewidth = parseInt(zombie1.width, 10); not needed
console.log(debugPlayer.x, zombie1.x, debugPlayer.x + zombie1.w);