Tepung Balap Permainan

bola merah ini akan bounce kembali saat ia jatuh ke tanah:


bounce

fitur lain yang akan ditambahkan adalah bounce properti.

bounce properti menunjukkan apakah komponen akan bounce kembali saat ia jatuh ke tanah disebabkan oleh gravitasi.

bounce nilai properti harus berupa angka. 0 menunjukkan bahwa komponen tidak bounce kembali, 1 akan membuat komponen kembali ke posisi jatuh mulai.

instansi

function component(lebar, tinggi, warna, x, y, type) {
  this.type = type;
  this.width = width;
  this.height = height;
  this.x = x;
  this.y = y;
  this.speedX = 0;
  this.speedY = 0;
  this.gravity = 0.1;
  this.gravitySpeed = 0;
  this.bounce = 0.6;
  this.update = function() {
    ctx = myGameArea.context;
    ctx.fillStyle = warna;
    ctx.fillRect(this.x, this.y, this.width, this.height);
  }
  this.newPos = function() {
    this.gravitySpeed += this.gravity;
    this.x += this.speedX;
    this.y += this.speedY + this.gravitySpeed;
    this.hitBottom();
  }
  this.hitBottom = function() {
    var dasarBawah = this.gamearea.canvas.height - this.height;
    jika (this.y > dasarBawah) {
      this.y = dasarBawah;
      this.gravitySpeed = -(this.gravitySpeed * this.bounce);
    }
  }
}

Coba Sendiri