Saltabilidad de juego
- Página anterior Gravedad de juego
- Página siguiente Rotación de juego
Este cuadrado rojo rebota al tocar el suelo:
rebote
Otra función que vamos a agregar es rebote
propiedad.
rebote
La propiedad indica si el componente rebota cuando la gravedad lo hace caer al suelo.
rebote
El valor de la propiedad debe ser un número. 0 significa que no rebota en absoluto, 1 haría que el componente rebote de regreso a la posición de inicio de la caída.
instancia
function componente(ancho, alto, color, x, y, tipo) { this.tipo = tipo; this.ancho = width; this.alto = height; this.x = x; this.y = y; this.velocidad_x = 0; this.velocidad_y = 0; this.gravedad = 0.1; this.velocidad_gravedad = 0; this.rebote = 0.6; this.actualizar = function() { ctx = miGameArea.context; ctx.fillStyle = color; ctx.fillRect(this.x, this.y, this.ancho, this.alto); } this.nueva_posicion = function() { this.velocidad_gravedad += this.gravedad; this.x += this.velocidad_x; this.y += this.velocidad_y + this.velocidad_gravedad; this.chocar_en_fondo(); } this.chocar_en_fondo = function() { var fondo_rojo = this.gamearea.canvas.height - this.height; if (this.y > fondo_rojo) { this.y = fondo_rojo; this.gravitySpeed = -(this.gravitySpeed * this.bounce); } } }
- Página anterior Gravedad de juego
- Página siguiente Rotación de juego