我目前正在为 COD4 中的地图开发脚本。我认为该语言是如此简单,以至于我将其标记为与语言无关,因为问题在于这种情况的算法。
有一个960单位宽的房间。在它里面中间有一个物体,我们将其视为轴。每次击球时,球都应该移动到一个随机位置,但不应超过墙壁。这是一个图表:
据我所知,游戏的 API 只允许相对于其位置移动对象,所以这是我想出的代码。问题是,在第二次调用 head_move() 之后,它开始产生意想不到的结果,这让我很头疼。有人可以帮帮我吗?
运动思考():
while (1)
{
self waittill ("trigger", player); //Wait till player hits the object
head_origin thread head_move();
}
head_move()
{
/* level.prevx is a global variable which I use to store
the distance traveled in the previous shot. Defaults to 0 */
/*This works in the first and second hit, but then it begins to show
incorrect max and min values*/
x_min = (0-480) + level.prevx;
x_max = x_min + 960;
x_units = RandomIntRange( x_min, x_max ); //Create a random integrer
log2screen("MIN: " + x_min + " and MAX: " + x_max + " and MOVED " + x_units);
log2screen("Moved " + x_units);
//Movement function, first parameter is the distance to be traveled, and the second one is the speed
self movex (x_units , level.movespeed);
level.prevx = x_units;
}
编辑: 只是为了澄清。当用户射球时,它的位置会改变到某个值。现在,如果他再次击球,随机整数生成器的最小值和最大值应该改变,以防止球移动到墙外。例子:
- 级别开始。球在房间中央。最小和最大范围分别为 -480 和 480
- 用户击球,球移动了 -200 个单位(向左 200 个单位)。
- 现在,最小和最大范围应该是 -280 和 680。
我希望这足够清楚。
编辑 2:按照 FlipScript 的建议编辑标志。这是 log2screen 函数的输出,实际发生了什么:
- 最小值:-480 和最大值 480。移动 67
- 最小值:-413 和最大值 547。移动 236
- 最小值:-244 和最大值 716。移动 461
只是一个示例案例。我相信有些事情是倒退的,这些不是正确的计算。