खेल उछल
जब यह फर्श से टकराता है तो यह लाल वर्ग उछलता है:
उछाल वाली
एक और कार्यक्षमता जिसे हम जोड़ना चाहते हैं वह है 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);
}
}
}