게임 바운싱
이 빨간색 사각형은 바닥에 닿으면 튕깁니다.
잘 튀는
추가하려는 또 다른 기능은 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);
}
}
}