ゲーム反発
この赤いボックスが地面に落ちると跳び上がります:
ジャンプ
追加するもう一つの機能は bounce
属性。
bounce
属性は、重力がコンポーネントを地面に落下させる場合にコンポーネントが反跳するかどうかを示します。
bounce
属性値は数字でなければなりません。0は完全に反跳しないことを示し、1はコンポーネントが下降から始まる位置まで反跳するようにします。
インスタンス
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 = color; 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 rockbottom = this.gamearea.canvas.height - this.height; if (this.y > rockbottom) { this.y = rockbottom; this.gravitySpeed = -(this.gravitySpeed * this.bounce); } } }