[英]Plus equals two variables returns isNan( ) = true when the variables themselves return isNan() = false - edited
I'm fairly new to Javascript and I'm running into an issue I can't explain.我对 Javascript 还很陌生,遇到了一个我无法解释的问题。 Two variables that are numbers, and isNaN() returns false for both, when added together using += the result is returning isNaN() = true.
两个变量是数字,并且 isNaN() 对两者都返回 false,当使用 += 相加时,结果返回 isNaN() = true。 Is there some nuance that I'm missing here?
我在这里缺少一些细微差别吗?
The code is to set fps on frame rate in a side-scrolling game.该代码是在横向滚动游戏中设置帧速率的 fps。 I'm trying to determine why this.frameTimer += deltaTime is equating to isNaN() = true, where the variables individually are equating to false.
我试图确定为什么 this.frameTimer += deltaTime 等于 isNaN() = true,其中变量单独等于 false。 The end goal is to get a valid number to compare against another variable.
最终目标是获得一个有效数字来与另一个变量进行比较。 Thanks for your help.
谢谢你的帮助。
draw(context,deltaTime) {
if (this.frameTimer > this.frameInterval) {
if (this.FrameX < this.maxFrame) { this.FrameX++
this.frameTimer = 0 }
else this.frameX = 0
} else {
this.frameTimer // isNaN(this.frameTimer) returns false
deltaTime // isNaN(deltaTime) returns false
this.frameTimer += deltaTime // isNaN(this.frameTimer) returns true
}
context.drawImage(this.image, this.width * this.frameX, this.height * this.frameY, this.width, this.height, this.x, this.y, this.width, this.height)
}
deltaTime is passed to the draw method, under the Player class, from an animate function in a separate module: deltaTime 从单独模块中的 animate 函数传递给 Player 类下的 draw 方法:
function animate(timeStamp) {
const deltaTime = timeStamp - lastTime
lastTime = timeStamp
ctx.clearRect(0,0,canvas.width, canvas.height)
gameObjects.forEach(object => {
object.update()
object.draw()
})
player.update(input.lastKey)
player.draw(ctx, deltaTime)
drawStatusText(ctx, input, player)
requestAnimationFrame(animate)
}
The variables for frameTimer, fps and frameInterval are set in a Player class: frameTimer、fps 和 frameInterval 的变量在 Player 类中设置:
export class Player {
constructor(gameWidth, gameHeight) {
this.gameWidth = gameWidth - 200
this.gameHeight = gameHeight
this.states = [new StandingLeft(this), new StandingRight(this), new SittingLeft(this), new SittingRight(this), new RunningLeft(this), new RunningRight(this), new JumpingLeft(this), new JumpingRight(this), new FallingLeft(this), new FallingRight(this)]
this.currentState = this.states[1]
this.image = document.getElementById("player")
this.width = 100
this.height = 104
this.x = this.gameWidth/2 - this.width/2
this.y = gameHeight - this.height - 25
this.vy = 0
this.weight = 0.5
this.frameX = 0
this.frameY = 0
this.maxFrame = 0
this.speed = 0
this.maxSpeed = 5
this.fps = 15
this.frameTimer = 0
this.frameInterval = 1000/this.fps
}
You are missing keyword this before .deltaTime, the parameter may not have been given a value and thats why its giving true
, here is an expample:您在 .deltaTime 之前缺少this关键字,参数可能没有被赋予一个值,这就是它给出
true
的原因,这里是一个例子:
draw(a, b){
console.log(isNaN(b)) //Return true if b is left without value
console.log(isNaN(this.c)) //Returns true if this.c is undefined too
}
Your mistake is right here:你的错误就在这里:
draw(context,deltaTime) {
if (this.frameTimer > this.frameInterval) {
if (this.FrameX < this.maxFrame) { this.FrameX++
this.frameTimer = 0 }
else this.frameX = 0
} else {
this.frameTimer // isNaN(this.frameTimer) returns false
this.deltaTime // isNaN(this.deltaTime) returns false
this.frameTimer += ------> deltaTime <-------// isNaN(this.frameTimer)returns true
}
context.drawImage(this.image, this.width * this.frameX, this.height * this.frameY, this.width, this.height, this.x, this.y, this.width, this.height)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.