game-maker - 球在乒乓克隆中卡在桨内(游戏制作者)
问题描述
我目前正在游戏制作者中制作 Pong 克隆(我在游戏制作者中的第一个独立项目)。
当球与桨相撞时,我将其反转x_speed
(通过将其反转方向* -1
)。然而,当球击中桨的顶部或底部时,球就会卡在里面,不断地改变它x_speed
,但永远不会离开桨。
我的问题不在于问题是什么,而是有解决这个问题的实用方法吗?
我已经尝试(但失败了)实现 place_meeting 方法,并且我尝试了其他几种方法来检测球是否击中了桨的顶部或底部,以便我可以相应地调整它的位置x
和y
位置。
如果有人有想法(我不一定需要解决方案的代码,只需要这个想法,以便我可以在我的游戏中实现它。这是我到目前为止所拥有的。
我已经尝试过其他解决方案,但它们都没有接近,所以在这里展示它们没有意义。如果您需要我的程序中的任何其他代码片段,请询问。
球的步骤:
//Checking if it touches the borders of the play area
if (x <= 0) {
x_speed *= -1;
obj_score_left.gameScore += 1;
}
if (x + width >= room_width) {
x_speed *= -1;
obj_score_right.gameScore += 1;
}
if (y <= 0) y_speed *= -1;
if (y + height >= room_height) y_speed *= -1;
//Adds the x and y speeds to x and y
x += x_speed;
y += y_speed;
解决方案
为您的桨设置脚本也会很有用,因为导致您的问题的碰撞未在此处显示,但我将尝试您应该尝试实施的广泛解决方案。
无论您的情况如何,第一步都应该有很大帮助,那就是在球发生碰撞时将球推到距您的桨合适的距离,因为无论如何它都不应该在其中。像这样的东西
碰撞时:
ball.x = paddle.x+paddle.width+ball.width
可能也有帮助的方法是确保 x_speed 只能在桨的相反方向上,通过使用 point_direction() 或 abs()
碰撞时:
direction = point_direction(paddle.x,paddle.y,ball.x,ball.y)
//would require using native direction and speed variables rather than x_speed and y_speed
或者:
x_speed = abs(xspeed) //for the left paddle
x_speed = -abs(xspeed) //for the right paddle
在不了解您的实际代码的情况下,这就是我所能建议的全部。
推荐阅读
- jquery - 表转 JSON +rowspan 转海量
- scala - 使用数据帧的 Spark 会话化
- angular - 使用 npm 安装 Angular-cli 时出错
- c# - WSDL 数据类型“xxxx:AuthorizationFilter”不存在
- python - 字典 | 班级 | 来自 YAML 的命名元组
- android - IllegalArgumentException:AdConsent 库
- c# - 如何正确调用 Jira Rest API
- react-native - 在 FLATLIST 中绑定列表项 onpress ( React Native )
- javascript - my = f.input :title 中的标题是什么?
- ios - 如何以编程方式在 iOS 中读取邮件消息?