How to use a function of an object as an argument of another function

2

This is my function that receives an object and uses the attributes of type " function ".

FrameWork.prototype.loop = function (objectLoop) {

    objectLoop.draw();
    objectLoop.update();
    window.requestAnimationFrame(this.loop.bind(this));
};

This is the object that I pass to the game function loop and my FrameWork FrameWork is in a separate .js file)

game.loop({
        draw : function(){

            game.get.Context.clearRect(0, 0, 100, 100);

            game.get.Context.fillStyle = colider.color;
            game.get.Context.fillRect(colider.x, colider.y, colider.width, colider.height);

            game.get.Context.fillStyle = player.color;
            game.get.Context.fillRect(player.x, player.y, player.width, player.height);
        },
        update: function () {
            updateBlock();
            colide();
            wallCollision();
        }
    });

Error:

Uncaught TypeError: objectLoop.draw is not a function.
    
asked by anonymous 02.11.2017 / 20:40

1 answer

3

When you pass callback to window.requestAnimationFrame the loop function will be rerun. It will run with the right context because you use .bind(this) but no arguments. objectLoop will not be set if you do not pass .bind also;

To pass this object also uses:

window.requestAnimationFrame(this.loop.bind(this, objectLoop));
    
02.11.2017 / 21:14