Javascript float 使用后变为 NaN
Javascript Float Becoming NaN After Use
我有以下class
function Sprite( sprite, frameWidth, frameHeight )
{
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
this.sprite = sprite;
this.cycles = new Array( [ new AnimationPoint( 0, 0 ) ] );
this.currentFrame = 0;
this.currentCycle = this.cycles[ 0 ];
this.scaleFactor = 1;
this.pause = false;
this.x = 0.0;
this.y = 0.0;
this.orientation = 0.0;
this.transformQue = new Array();
this.QueRotation = function( radians ) {
this.transformQue.push( radians );
}
this.AddAnimationCycle = function( animationPoints ) {
this.cycles.push( animationPoints );
}
this.Pause = function() {
this.pause = true;
}
this.UnPause = function() {
this.pause = false;
}
this.SelectAnimationCycle = function( cycle ) {
this.currentFrame = 0;
this.currentCycle = this.cycles[ cycle ];
}
this.Forward = function() {
this.x += Math.cos( this.orientation ) * grid.UnitToPixelX( canvas );
this.y += Math.sin( this.orientation ) * grid.UnitToPixelY( canvas );
}
this.Animate = function ()
{
renderContext.save();
var rotation = this.transformQue.pop();
var x = ( ( this.frameWidth * this.scaleFactor ) / 2 );
var y = ( ( this.frameHeight * this.scaleFactor ) / 2 );
renderContext.translate( this.x + x,
this.y + y );
renderContext.rotate( this.orientation += rotation );
renderContext.translate( -( this.x + x ),
-( this.y + y ) );
renderContext.drawImage( this.sprite,
this.currentCycle[ this.currentFrame ].x * this.frameWidth,
this.currentCycle[ this.currentFrame ].y * this.frameHeight,
this.frameWidth, this.frameHeight, this.x, this.y,
this.frameWidth * this.scaleFactor, this.frameHeight * this.scaleFactor );
renderContext.restore();
if( this.pause == false )
{
++this.currentFrame;
if( this.currentFrame >= this.currentCycle.length )
this.currentFrame = 0;
}
}
}
如果我在我的渲染函数中,做类似
的事情
mySprite.QueRotation( .1 )
它永远有效。
但是,如果我有一个调用 QueRotation
的函数,例如
function MySpriteClockWise() {
mySprite.QueRotation( Math.PI / 2 );
}
结果为 NaN。
仍然是陌生人,this.orientation = 0.0;
但是,如果我不触及渲染函数中的 QueRotation
函数,它将变为 NaN!即使在被简单地分配为零并且没有触及它之后!如果我执行以下功能:
starting = 0;
function MySpriteStart() {
if( starting++ < 4 )
MySpriteClockWise();
}
并观察控制台中的值,它计算了我想要 运行 函数的次数,但一旦在渲染函数中完成,它就变成了 NaN!
就好像 JS 认为我不再需要这个值了,即使我做了一个像这样的函数:
this.Forward = function() {
this.x += Math.cos( this.orientation ) * grid.UnitToPixelX( canvas );
this.y += Math.sin( this.orientation ) * grid.UnitToPixelY( canvas );
}
试图哄它不要扔掉价值,它仍然这样做了!这是一个大问题,因为我正在通过 lua 编写脚本,该项目的全部重点是让(编码教程)用户能够编写脚本以通过 [=40] 控制 "mySprite" (一个字符) =]学习!这种行为在 Firefox 和 Chromium 中都存在。请帮忙!
当调用 mySprite.QueRotation( .1 )
时 QueRotation
函数中的 this
不会
指向 mySprite
对象。改用箭头函数
this.QueRotation = ( radians ) => {
this.transformQue.push( radians );
}
编辑:
你好像不支持ES6。在这种情况下,在调用 QueRotation
函数
时使用它
mySprite.QueRotation.call(mySprite, .1)
找到一个解释问题的错误,我是仓促的,这似乎也是为了节省资源,我很抱歉。
我有以下class
function Sprite( sprite, frameWidth, frameHeight )
{
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
this.sprite = sprite;
this.cycles = new Array( [ new AnimationPoint( 0, 0 ) ] );
this.currentFrame = 0;
this.currentCycle = this.cycles[ 0 ];
this.scaleFactor = 1;
this.pause = false;
this.x = 0.0;
this.y = 0.0;
this.orientation = 0.0;
this.transformQue = new Array();
this.QueRotation = function( radians ) {
this.transformQue.push( radians );
}
this.AddAnimationCycle = function( animationPoints ) {
this.cycles.push( animationPoints );
}
this.Pause = function() {
this.pause = true;
}
this.UnPause = function() {
this.pause = false;
}
this.SelectAnimationCycle = function( cycle ) {
this.currentFrame = 0;
this.currentCycle = this.cycles[ cycle ];
}
this.Forward = function() {
this.x += Math.cos( this.orientation ) * grid.UnitToPixelX( canvas );
this.y += Math.sin( this.orientation ) * grid.UnitToPixelY( canvas );
}
this.Animate = function ()
{
renderContext.save();
var rotation = this.transformQue.pop();
var x = ( ( this.frameWidth * this.scaleFactor ) / 2 );
var y = ( ( this.frameHeight * this.scaleFactor ) / 2 );
renderContext.translate( this.x + x,
this.y + y );
renderContext.rotate( this.orientation += rotation );
renderContext.translate( -( this.x + x ),
-( this.y + y ) );
renderContext.drawImage( this.sprite,
this.currentCycle[ this.currentFrame ].x * this.frameWidth,
this.currentCycle[ this.currentFrame ].y * this.frameHeight,
this.frameWidth, this.frameHeight, this.x, this.y,
this.frameWidth * this.scaleFactor, this.frameHeight * this.scaleFactor );
renderContext.restore();
if( this.pause == false )
{
++this.currentFrame;
if( this.currentFrame >= this.currentCycle.length )
this.currentFrame = 0;
}
}
}
如果我在我的渲染函数中,做类似
的事情mySprite.QueRotation( .1 )
它永远有效。
但是,如果我有一个调用 QueRotation
的函数,例如
function MySpriteClockWise() {
mySprite.QueRotation( Math.PI / 2 );
}
结果为 NaN。
仍然是陌生人,this.orientation = 0.0;
但是,如果我不触及渲染函数中的 QueRotation
函数,它将变为 NaN!即使在被简单地分配为零并且没有触及它之后!如果我执行以下功能:
starting = 0;
function MySpriteStart() {
if( starting++ < 4 )
MySpriteClockWise();
}
并观察控制台中的值,它计算了我想要 运行 函数的次数,但一旦在渲染函数中完成,它就变成了 NaN!
就好像 JS 认为我不再需要这个值了,即使我做了一个像这样的函数:
this.Forward = function() {
this.x += Math.cos( this.orientation ) * grid.UnitToPixelX( canvas );
this.y += Math.sin( this.orientation ) * grid.UnitToPixelY( canvas );
}
试图哄它不要扔掉价值,它仍然这样做了!这是一个大问题,因为我正在通过 lua 编写脚本,该项目的全部重点是让(编码教程)用户能够编写脚本以通过 [=40] 控制 "mySprite" (一个字符) =]学习!这种行为在 Firefox 和 Chromium 中都存在。请帮忙!
当调用 mySprite.QueRotation( .1 )
时 QueRotation
函数中的 this
不会
指向 mySprite
对象。改用箭头函数
this.QueRotation = ( radians ) => {
this.transformQue.push( radians );
}
编辑:
你好像不支持ES6。在这种情况下,在调用 QueRotation
函数
mySprite.QueRotation.call(mySprite, .1)
找到一个解释问题的错误,我是仓促的,这似乎也是为了节省资源,我很抱歉。