All we need do is:
- Declare a variable that we'll set equal to the amount of gravity we want
- Each frame, change the object's y-velocity by the amount of gravity
var gravity = 0.25;
MySprite.prototype.Do_Frame_Things = function() {
if (this.visible) ctx.drawImage(this.MyImg, this.x, this.y); // draw the thing
this.x = this.x + this.velocity_x;
this.y = this.y + this.velocity_y; // move the thing
this.velocity_y = this.velocity_y + gravity; // accelerate by gravity
if (this.x < 0) {this.velocity_x= -this.velocity_x};
if (this.x + this.MyImg.width > myCanvas.width) {this.velocity_x= -this.velocity_x};
if (this.y + this.MyImg.height > myCanvas.height) {this.velocity_y= -this.velocity_y};
}
Such a simple change, but such an impressive effect.