var Car = function (x, y) {
this.x = x;
this.y = y;
};
Car.prototype.draw = function () {
var carhtml = '<img src ="https://nostarch.com/images/car.png">';
this.carElement = $(carhtml)
this.carElement.css({
position: "absolute",
left: this.x,
top: this.y
});
$("body").append(this.carElement);
}
Car.prototype.moveRight = function () {
this.x += 5;
this.carElement.css({
position: "absolute",
left: this.x,
top: this.y
});
};
var tesla = new Car(10, 20)
tesla.draw()
setInterval(tesla.moveRight, 20)
I want an image ot a car to move 5 pixels right every 20 miliseconds using setInterval.
I get this error every time I launch the code:
Uncaught TypeError: Cannot read properties of undefined (reading ‘css’)
at Car.moveR (edu2.html:61:29)
New contributor
Danylo Tereshchenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.