requestAnimationFrame with this keyword

后端 未结 7 829
感动是毒
感动是毒 2020-11-27 15:40

I\'m using webkitRequestAnimationFrame but I\'m having trouble using it inside of an object. If I pass the this keyword it will use window

7条回答
  •  失恋的感觉
    2020-11-27 16:24

    you have not to use "this". Keep it simple.

    var game = {
          canvas:null,
          context:null,
    
        init:function(){
                // init canvas, context, etc
        },      
    
        update:function(){
            //do something
            game.render();                        
            requestAnimationFrame(game.update, game.canvas);        
        },            
    };
    

提交回复
热议问题