Step 13
We must update the Paddle, so it does not run off the sides of the canvas.
Let's override the move
method:
move(canvasWidth) {
super.move();
if (this.x < 0) {
this.x = 0;
} else if (this.x + this.width > canvasWidth) {
this.x = canvasWidth - this.width;
}
}
It seems as if we are overloading the move
method. However, JavaScript does not support method overloading since the number of arguments to a method can be fewer/more than declared parameters. Therefore, what we had done with the move
method is still considered method overriding.
Method overriding in JavaScript means having methods with the same name in parent and subclasses.
Finally, update the main.js
file:
- paddle.move();
+ paddle.move(canvas.width);
Save your code and observer the changes in the browser.