Javascript Html5画布、继承层次结构可能导致闪烁?

Javascript Html5画布、继承层次结构可能导致闪烁?,javascript,animation,inheritance,canvas,prototype,Javascript,Animation,Inheritance,Canvas,Prototype,我正在画布上处理一个动画场景,其中我有一个基类Sprite——在我的项目中,除了一个对象之外,所有对象都使用单一继承,就像从Sprite继承一样 我有一个从精灵继承的静态动画精灵对象,这个动画精灵只是在精灵表上移动剪辑区域。它按预期设置动画,即在通过图纸过渡时不闪烁 然而,我有一个移动的动画精灵,为了节省代码空间,我从AnimatedSprite继承了这个精灵,所以这个精灵从一个对象继承而来 这个新精灵在每一帧转换中都会闪烁,我想知道双重继承是否会导致这个问题?就像在搜索所有三个对象的原型以查找

我正在画布上处理一个动画场景,其中我有一个基类Sprite——在我的项目中,除了一个对象之外,所有对象都使用单一继承,就像从Sprite继承一样

我有一个从精灵继承的静态动画精灵对象,这个动画精灵只是在精灵表上移动剪辑区域。它按预期设置动画,即在通过图纸过渡时不闪烁

然而,我有一个移动的动画精灵,为了节省代码空间,我从AnimatedSprite继承了这个精灵,所以这个精灵从一个对象继承而来

这个新精灵在每一帧转换中都会闪烁,我想知道双重继承是否会导致这个问题?就像在搜索所有三个对象的原型以查找函数调用时一样

很抱歉,在不尝试的情况下询问有点懒惰,代码库相当大,今晚我没有时间使用单一继承模型重写对象

因此,对此的任何评论都将不胜感激

作为记录,我是双重缓冲

继承功能:

function __extends(child,parent){ //Global function for inheriting from objects
       function Temp(){this.constructor = child;} //Create a temporary ctor
       Temp.prototype = parent.prototype; 
       child.prototype = new Temp(); //Set the child to an object with the child's ctor with the parents prototype
}
交换帧:

SantaSprite.prototype.loopImages = function(){

    if(this.getDirection()){ //If going right set the y frame to the bottom of the sheet
        this.setCurrentFrame(this.getCurrentFrame().getX(),this.getFrameHeight());
    }
    else{ //Else set y to the top of the sheet
        this.setCurrentFrame(this.getCurrentFrame().getX(),0);
    }

    this.setSwitchTime(this.getSwitchTime()+this.getIncrement()); //Increment the timer

    if(this.getSwitchTime() >= this.getSwitchFrame()){ //If the counter has reached the limit

        this.setSwitchTime(0); //Reset the counter

        //Move the frame along one tile in the x axis
        this.setCurrentFrame(this.getCurrentFrame().getX() + this.getFrameWidth());

        //If at the end of x axis, reset back to 0 (left)
        if(this.getCurrentFrame().getX() + this.getFrameWidth() > this.getImage().width){
            this.setCurrentFrame(0,this.getCurrentFrame().getY());
        }
    }

};

干杯

这里的错误就在这一行:

//Move the frame along one tile in the x axis
    this.setCurrentFrame(this.getCurrentFrame().getX() + this.getFrameWidth()); //No Y value set.
修复方法:

//Move the frame along one tile in the x axis
    this.setCurrentFrame(this.getCurrentFrame().getX() + this.getFrameWidth(),
                         this.getCurrentFrame().getY());

1) 双缓冲是allready为所有浏览器的画布实现的,所以您的游戏实际上是三重缓冲。我会在第一个调试/原型阶段取消它。2) 如果没有任何代码,很难说您的继承方案是否有罪:您使用哪种继承方案?纯js的?(基于Object.create,应用父构造函数…)还是基于库的构造函数?它也可能存在于切换帧的方式中。。。很难说!再给我们一些情报!编辑!这些信息够了吗?此外,我取出了缓冲区画布,但“问题”仍然存在。谢谢你的评论!1) 第一次,我只尝试一个方向,评论方向改变部分。2) 无论如何,仅当currentdirection!=你应该做点什么。3) 我很惊讶你没有累计错误=switchTime switchFrame,而只是将其删除(=0)。[我不确定这里是否清楚…]