2013-07-05 115 views
1

嗨,我试图使用requestAnimationFrame为我的游戏,我实际上使用下面的代码,但正如你可以看到“.bind()”创建每个循环一个新的功能,减缓我的游戏。 ..我正在寻找一个最好的性能比较一个“有效”的解决方案,预先感谢您:drequestAnimationFrame对象类型错误

function myClass() { 
    this.loop = function() { 
    window.requestAnimationFrame(this.loop.bind(this)); 

    /* here myGameLoop */ 
    } 

    this.loop(); 
} 

上面的代码的作品,但速度很慢..而不是此“标准”的代码给我“类型错误” :

window.requestAnimationFrame(this); 

我甲肝E也发现é试过这种问答&答:requestAnimationFrame attached to App object not Window作品只是一次再给予相同的“类型错误” :(

尝试,如果你不相信我:http://jsfiddle.net/ygree/1:'(

回答

1

不知道整个你的对象的故事(还有什么);你可以通过只是在做这个生活简单化:

function myClass() { 

    var iHavAccessToThis = 1; 

    function loop() { 

     iHavAccessToThis++; 

     /* here myGameLoop */ 

     requestAnimationFrame(loop); 
    } 
    loop(); 

    //if you need a method to start externally use this instead of the above line 
    this.start = function() { loop() } 

    //... 
    return this; 
} 

现在你不需要绑定任何东西,你访问本地范围是快。

然后调用:

var class1 = new myClass(); 
class1.start(); 
+0

因为我正在写一个HTML5的框架,你建议的方式,我要重复所有的“外部可用”的方法和属性。 所以...如果我有100个属性/方法,我必须编写其他100个方法来返回这个属性...不是很好吗?无论如何感谢您的帮助 –

+1

@ user2488460听起来像一个巨大的项目。如果您发现它有帮助,请考虑upvote /接受答案。 – K3N

+0

这不是我的第一个html5框架;)但是我仍然在为这个问题寻找更好的解决方案:'( –

相关问题