SetInterval missing property in js class

Posted by sebastian on Stack Overflow See other posts from Stack Overflow or by sebastian
Published on 2011-01-09T18:45:28Z Indexed on 2011/01/09 18:53 UTC
Read the original article Hit count: 226

Filed under:
|
|

I wrote simple class in JS witch works, but i had problem when i try use setInterval with it. Ex. if i do something like that

ball = new ball(5,10,0, '#canvas');
ball.draw();
ball.draw();
ball.draw();
ball.draw();

It works. But this:

ball = new ball(5,10,0, '#canvas');
setInterval(ball.draw, 100);

Not work. I get error that values are undefined.

function ball (x,y,z,holdingEl) {
    this.r = 5; //zmienna przechowujaca promien pilki
    this.size = this.r *2; // zmienna przechowujaca rozmiar
    this.ballSpeed = 100; // predkosc pilki
    this.ballWeight = 0.45; // masa pilki
    this.maxFootContactTime = 0.2; // maksymalny czas kontaktu pilki z noga - stala
    this.ctx = jQuery(holdingEl)[0].getContext("2d"); // obiekt pilki
    this.intVal = 100 // predkosc odswiezania
    this.currentPos = { // wspolrzedne pozycji
        x: x,
        y: y,
        z: z
    }
    this.interactionPos = { // wspolrzedne pozycji ostatniej interakcji
        x: -1,
        y: -1,
        z: -1
    }
    this.direct = { // kierunek w kazdej plaszczyznie
        x : 1,
        y : 0,
        z : 0
    }
    this.draw = function (){
      this.ctx.clearRect(0,0,1100,800);
      this.ctx.beginPath();
      this.ctx.arc(this.currentPos.x, this.currentPos.y, this.r, 0, Math.PI*2, true);
      this.ctx.closePath();
      this.ctx.fill();
    }
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html