There is a ball class.

function Ball(x, y, id) { this.x = x; this.y = y; this.id = id; Ball.prototype.draw = function() { ctx.drawImage(sprite1, 0, 0); } } 

How can I use "this.x" and "this.y" instead of zeros in the drawImage () function?

    1 answer 1

    For example:

     Ball = function ( x, y, id){ this.x = x; this.y = y; this.id = id; } Ball.prototype.draw = function (){ ctx.drawImage( sprite1, this.x, this.y ); } 

    Pay attention to ctx и sprite1 , IMHO - if they are some global objects, then:

    1. Or transfer them to the constructor and store in the object
    2. Or make the function parameters