Oyun Zıplama

bu kırmızı blok zemin üzerine düştüğünde yankılanır:


yankılanma

eklememiz gereken bir diğer özellik bounce öznitelik.

bounce öznitelik, bileşenin yerçekimi nedeniyle zemin üzerine düşmesi durumunda yankılanıp yankılanmayacağını belirtir.

bounce özellik değerleri sayısal olmalıdır. 0 tamamen yankılanmama anlamına gelir, 1 ise bileşenin başlangıç konumuna geri dönmek için kullanılır.

örnek

function component(width, height, color, 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 = renk;
    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 taban = this.gamearea.canvas.height - this.height;
    if (this.y > taban) {
      this.y = taban;
      this.gravitySpeed = -(this.gravitySpeed * this.bounce);
    }
  }
}

Kişisel Deneyim