首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript浮点在使用后变成NaN

Javascript浮点在使用后变成NaN
EN

Stack Overflow用户
提问于 2020-02-09 11:43:21
回答 2查看 70关注 0票数 0

我有以下课程

代码语言:javascript
复制
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;
        }
    }
}

如果我在我的渲染功能中,做一些类似的事情

代码语言:javascript
复制
mySprite.QueRotation( .1 )

它永远有效。

但是,如果我有一个调用QueRotation的函数,如

代码语言:javascript
复制
function MySpriteClockWise() {
    mySprite.QueRotation( Math.PI / 2 );
}

它会传到NaN。

更奇怪的是,this.orientation = 0.0;,但是如果我不接触渲染函数中的QueRotation函数,它将转到NaN!即使在被简单地分配到零并且没有触及它之后!如果我执行如下功能:

代码语言:javascript
复制
starting = 0;

function MySpriteStart() {
    if( starting++ < 4 )
        MySpriteClockWise();
}

然后观察控制台中的值,它计算的次数与我想运行函数的次数一样好,但一旦在render函数中完成,它就变成NaN!

就好像JS不再认为我需要这个值一样,即使我做了如下的函数:

代码语言:javascript
复制
this.Forward = function() {
    this.x += Math.cos( this.orientation ) * grid.UnitToPixelX( canvas );
    this.y += Math.sin( this.orientation ) * grid.UnitToPixelY( canvas );
}

为了说服它不要丢弃价值,它仍然是这样做的!这是一个大问题,因为我是通过lua编写脚本的,整个项目的目的是让(编码教程)用户能够通过脚本来控制"mySprite“(一个字符)通过lua来学习!这种行为在firefox和is中都有。请帮帮我!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-09 12:05:05

发现一个错误解释了这个问题,我太仓促了,好像是为了节约资源,我很抱歉。

票数 -1
EN

Stack Overflow用户

发布于 2020-02-09 11:46:40

调用mySprite.QueRotation( .1 )时,QueRotation函数中的this不指向mySprite对象。使用箭头函数代替

代码语言:javascript
复制
this.QueRotation = ( radians ) => {
   this.transformQue.push( radians );
}

编辑

您似乎没有ES6支持。在本例中,在调用QueRotation函数时使用此方法

代码语言:javascript
复制
mySprite.QueRotation.call(mySprite, .1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60136229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档