反弹球会重置到所需位置,但不会移动
Bouncing ball resets to desired position but does not move
我正在尝试制作壁球游戏,并认为设置球的移动是一个很好的开始。我已经设法让球按我想要的方式反弹,它从左上角50像素处落下,反弹两次,然后重置到开始的位置。重置后,球没有继续移动,而是停留在距离上角50像素的位置
为什么重置后球没有移动?如何使球从重置开始移动
从这里开始,我想设置一个事件监听器来重新开始投球,然后再次"发球"。任何关于把它放在哪里的建议都会很棒。这是我第一次尝试编写自己的游戏,所以请记住,我是个十足的傻瓜。
这是我的画布代码。
<html>
<title>Squash Game V1</title>
<h1>Squash</h1>
<canvas id="gameCanvas" width=800 height=600></canvas>
<script>
var canvas;
var canvasContext;
var ballX = 50;
var ballY = 50;
var gravity = 0.2;
var bounceFactor = 0.6;
var ballSpeedX = 3;
var ballSpeedY = 10;
var ballSpeedY2 = 5;
var ballBounce = 0
var ballStartPos = 50
const resistence = 0.998
const ballWidth = 15;
window.onload = function() {
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
var framesPerSecond = 60;
setInterval(function() {
moveEverything();
drawEverything();
}, 1000/framesPerSecond);
}
function ballReset() {
ballX = ballStartPos;
ballY = ballStartPos;
}
function moveEverything() {
// this moves ball down
ballY += ballSpeedY;
// this moves the ball across
ballX += ballSpeedX;
// this speeds ball up as it's falling plus slows down making it fall
ballSpeedY += gravity;
ballSpeedX = ballSpeedX*resistence;
//this bounes the ball
if (ballY > canvas.height - ballWidth) {
ballSpeedY = -ballSpeedY
//this reduces height of the bounce
ballSpeedY *= bounceFactor;}
//this should count bounces
if (ballY > canvas.height - ballWidth){
ballBounce = ballBounce + 1;
}
//ball will bounce of right wall
if (ballX > canvas.width - ballWidth) {
ballSpeedX = -ballSpeedX}
//ball will bounce off left wall
if (ballX < 0 + ballWidth) {
ballSpeedX = -ballSpeedX}
if (ballBounce >= 2) {
ballReset()}
}
function drawEverything() {
//this draws the pong court
colourRect(0,0,canvas.width,canvas.height, 'black');
//this draws the ball
colourCircle(ballX, ballY, 10, "white")
function colourCircle(centreX, centreY, radius, drawColour) {
canvasContext.fillStyle = drawColour;
canvasContext.beginPath();
canvasContext.arc(centreX, centreY, radius, 0,Math.PI*2, true)
canvasContext.fill();
}
//this function draws a rectangle
function colourRect(leftX, topY, width, height, drawColour) {
canvasContext.fillStyle = drawColour;
canvasContext.fillRect(leftX, topY, width, height);
} }
</script>
</html>
球停止反弹,因为当前代码在第二次反弹后连续调用ballReset()。您应该在重置方法中重置ballBounce
计数器以保持其跳动。Fiddle
function ballReset() {
ballX = ballStartPos;
ballY = ballStartPos;
ballBounce = 0; // Added code
}
相关文章:
- 在不移动元件的情况下从相对位置更改为固定位置
- 如何在鼠标悬停时将对象从起始位置移动到结束位置,然后在鼠标悬停后再次移动
- 将插入符号位置移动到ContentEditable<DIV>
- 我发现了一些只在移动设备上可见的垃圾邮件链接,我可以'找不到包含此垃圾邮件链接的脚本的位置
- float's未返回到移动视图结束后的位置
- 将javascript数组中的项移动到特定位置的有效方法
- 如何将节点移动到所需位置
- Photoshop脚本:将图像移动到位置x,y
- Google Maps InfoBubble pixelOffset (从标记上方的默认位置移动)
- 垂直滚动恒星JS上的水平位置移动
- 根据鼠标位置移动(翻译+过渡)全景照片
- 粘性页脚CSS使jQuery小部件在滚动时从各自的位置移动
- 如何使对象向鼠标位置移动
- 在OpenLayers 3中将要素从一个位置移动到另一个位置
- 使用jQuery在相同的位置移动和删除列表框项到另一个列表框
- 悬停时根据光标的位置移动创建的元素
- 使用jquery/angular从一个位置移动到另一个位置
- 动画-相对于当前位置移动
- MapView.标记在用户当前位置移动时不稳定
- 随光标位置移动