Rimbalzo di gioco

Questo quadrato rosso rebobina quando atterra:


rebound

Un'altra funzione che dobbiamo aggiungere è rebound attributo.

rebound L'attributo indica se il componente rebobina quando la gravità lo fa cadere a terra.

rebound Il valore dell'attributo deve essere un numero. 0 significa che il componente non rebobina affatto, 1 farà sì che il componente torni indietro alla posizione di partenza.

istanza

function componente(larghezza, altezza, colore, x, y, tipo) {
  this.tipo = tipo;
  this.larghezza = larghezza;
  this.altezza = altezza;
  this.x = x;
  this.y = y;
  this.speedX = 0;
  this.speedY = 0;
  this.gravità = 0.1;
  this.velocitàGravity = 0;
  this.rebound = 0.6;
  this.aggiorna = function() {
    ctx = myGameArea.context;
    ctx.fillStyle = colore;
    ctx.fillRect(this.x, this.y, this.larghezza, this.altezza);
  }
  this.nuovaPosizione = function() {
    this.velocitàGravity += this.gravità;
    this.x += this.speedX;
    this.y += this.speedY + this.velocitàGravity;
    this.colpisceFondo();
  }
  this.colpisceFondo = function() {
    var fondo = this.gamearea.canvas.height - this.height;
    if (this.y > fondo) {
      this.y = fondo;
      this.gravitySpeed = -(this.gravitySpeed * this.bounce);
    }
  }
}

Prova da solo