jQuery:限制对象的按键移动
我正在尝试用 jQuery 开发乒乓球类游戏,我想知道如何限制容器内的滑块位置。
我还需要有关如何生成球/盒子以及如何使其在容器的侧面和顶壁上弹跳的帮助。
我已经成功地解决了这个问题,但我仍然在努力将滑块保留在容器内,并在球的弹跳上挣扎。
任何帮助将不胜感激。 MyCodeSoFar
I am trying to work on a ping pong kind of game in jQuery and I was wondering how can I restrict the slider position inside a container.
I also need help with how to generate the ball/box and how to make it bounce on the side and top wall of the container.
I have managed to work on it but I am still struggling to retain the slider inside the container and struggling on the bouncing the ball around.
Any help would be highly appreciated.
MyCodeSoFar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,您的代码对我来说有点混乱。不管怎样,我发现像 js pong 这样的游戏的想法很有趣,我调整了你的代码来实现你的意图。
只需看看这个小提琴,并随时向我发送一些反馈。
I'm sorry but your code is a bit messy for me. Whatever, I found this idea of a js pong like game funny and I adapted your code to do what you intended.
Just have a look at this fiddle and feel free to send me some feedback.
您问了很多一般性问题,所以我会给您一些一般性指导。 :)
要约束桨,只需检查一些偏移即可。
要确定它是否接触左边框,请检查它相对于左侧的偏移量。如果是
0
,则忽略 ← 键。要确定它是否接触右边框,请获取左偏移量并添加桨的宽度。如果它等于游戏区域的宽度,则忽略→按键。
由于您使用的是元素而不是
canvas
,因此您可以使用 CSS 生成一个球...这里的关键是
border-radius
等于width< /code> 和球的
高度
。要让球弹起,请设置
x
和y
速度,该速度随着游戏计时器的增加而增加。当球与球拍或游戏区域边缘碰撞时,翻转这些符号。为了使其更像乒乓球,您可以在击中球拍的某些区域时修改弹跳方式,例如,如果它击中远边缘,则当它击中时它会具有更锐利的角度逆转。要确定球是否击中游戏区域的边缘,请使用与我上面描述的类似的技术。
要确定您的球是否撞击了球拍,请使用一些碰撞检测代码。
检查球的 Y 偏移量是否高于桨的 Y 偏移量(我假设您的桨在水平轨道上线性移动)。然后,如果满足该条件,请查看球的 X 偏移量是否在球拍的 X 偏移量和 (
+
) 球拍宽度的范围内。如果您的球拍和游戏区域永远不会改变,那么缓存这些宽度将是有益的。
You have asked a lot of general questions so I'll give you some general pointers. :)
To constrain the paddle, just check some offsets.
To determine if it's touching the left border, check its offset from the left. If it's
0
, ignore the ← keydown.To determine if it's touching the right border, take the left offset and add the width of the paddle. If it's equal to the width of the game area, ignore the → keydown.
Since you are using elements and not
canvas
, you can generate a ball with CSS...The key here is the
border-radius
being equal to thewidth
andheight
of the ball.To get the ball to bounce, have a
x
andy
velocity that is incremented with your game timer. Flip the sign of these when the ball collides with either the paddle or the edges of the game areas. To make it more Pong-like, you can mess with how you modify the bounce when it hits certain regions of the paddle, for example, if it hits the far edge, it has a sharper angle when it reverses.To determine if the ball has hit the edge of the game area, use a technique similar to what I described above.
To determine if your ball has impacted your paddle, use some collision detection code.
Check to see if the ball's Y offset is higher than the paddle's Y offset (I assume your paddle moves linearly on a horizontal track). Then, if that condition is met, see if the ball's X offset is within the range of the paddle's X offset and (
+
) the width of the paddle.If your paddle and game area never change, it would be beneficial to cache these widths.