Java浮点数在使用后变为NaN

问题描述 投票:0回答:2

我有以下课程

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();
}

并在控制台中观察该值,它的计算结果与我要运行该函数的次数相同,但是一旦在render函数中完成,它就会变成NaN!

好像JS确实认为我不再需要该值,即使我创建了类似这样的函数:

this.Forward = function() {
    this.x += Math.cos( this.orientation ) * grid.UnitToPixelX( canvas );
    this.y += Math.sin( this.orientation ) * grid.UnitToPixelY( canvas );
}

试图哄骗它不要丢掉价值,但它仍然可以!这是一个很大的问题,因为我正在通过lua编写脚本,整个项目的重点是(编程教程)用户可以通过lua编写脚本来控制“ mySprite”(角色)以学习!在Firefox和铬中都存在这种现象。请帮助!

javascript virtual-machine nan v8
2个回答
1
投票

[调用mySprite.QueRotation( .1 )this函数内部的QueRotation指向mySprite对象。使用箭头功能代替

this.QueRotation = ( radians ) => {
   this.transformQue.push( radians );
}

编辑

似乎您没有ES6支持。在这种情况下,在调用QueRotation函数

时使用它
mySprite.QueRotation.call(mySprite, .1)

0
投票

很抱歉,发现了一个解释该问题的错误,我很仓促,这似乎也可以节省一些资源。

© www.soinside.com 2019 - 2024. All rights reserved.