游戏重力
కొన్ని గేమ్స్లో గేమ్ కంపోనెంట్స్ ఒక దిశకు ముఖం తిరిగి నెట్టే శక్తి ఉంది, ఉదాహరణకు భారం భూమి వైపు నెట్టే శక్తి.
భారాలు
ఈ లక్షణను మా కంపోనెంట్ కన్స్ట్రక్టర్ లో చేర్చడానికి ముందు ఒక సంయోజనం చేర్చండి. gravity
అంశం దానిని మీరు మొదటిగా సెట్ చేయండి. ఆపై ఒక సంయోజనం చేర్చండి. gravitySpeed
అన్ని ఫ్రేమ్లను నవీకరించినప్పుడు అది పెరుగుతుంది అన్ని లక్షణాలు:
实例
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.05; this.gravitySpeed = 0; 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.newPos = function() { this.gravitySpeed += this.gravity; this.x += this.speedX; this.y += this.speedY + this.gravitySpeed; this.hitBottom(); } this.hitBottom = function() { var rockbottom = myGameArea.canvas.height - this.height; if (this.y > rockbottom) { this.y = rockbottom; } }
వేగంపెంచు
ఆటలో, ప్రతిమ పడిపోయే శక్తిని పిండికినప్పుడు మీరు పరికరాన్ని వేగంగా చేయడానికి పద్ధతిని రూపొందించాలి.
బటన్ నొక్కినప్పుడు ఫంక్షన్ ప్రారంభించి ఎరుపు ప్రతిమ ఆకాశంలో ఎగురుతుంది:
实例
<script> function accelerate(n) { myGamePiece.gravity = n; } </script> <button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.1)">వేగంపెంచు</button>
一个游戏
根据我们迄今为止所学到的知识制作一个游戏:
实例
请单击加速按钮开始游戏。
能活多久?请使用加速按钮保持在空中。