【发布时间】:2023-03-09 21:13:01
【问题描述】:
我正在尝试制作一款多平台的格斗游戏。我已经成功地制作了游戏的控制、移动、(双)跳跃和重力部分。
问题是,当玩家跳跃时,到达地面后,他们似乎比在平台上应该走的更深一些(他们应该着陆并停留在平台表面)。这在玩家二段跳时更加明显。
我知道为什么会这样;这是因为有时hitTestObject 需要一段时间才能在对象来得太快时做出反应。
所以,我首先想到的就是让玩家的脚y axis等于他落地的平台顶部的y axis。
但是,该解决方案导致着陆相当不稳定。
我的问题是:有没有办法让玩家平稳地降落在平台的上表面?
我尝试过的一些事情:
-提高 FPS,它只是产生了相同的效果,但速度更快。
-降低玩家下落的速度,但这会降低游戏的乐趣,所以我把它划掉了。
这是我的相关代码:
stage.addEventListener(Event.ENTER_FRAME, loop);
var jumpConstant:Number = 30;
var gravityConstant:Number = 1.8;
function loop(e:Event):void //happens
{
if(player.leg1.foreleg1.foot1.hitTestObject(platforms.ground)||player.leg2.foreleg2.foot2.hitTestObject(platforms.ground)) //if either of the player's legs are colliding with the platform [ps: nested movieclips]
{
player.ySpeed = 0; //the player stops going downwards
player.y = platforms.y; //the player's y becomes the platform's y. don't worry, it puts the player in the right place, just not smoothly.
if (player.b_up) //if Up control button (W or Up Arrow) is being pressed
{
player.ySpeed = -player.jumpConstant; //make the player jump
}
else //if the player isn't colliding with the platform
{
player.ySpeed += player.gravityConstant; //the player is affected by gravity and is pulled downwards
}
如果你想试试看生涩效果,请链接到游戏:
【问题讨论】:
-
hitTestObject没有根据您的图像速度有任何形式的滞后 - 这完全是您的误解(或错误陈述)。它只是检查两个显示对象是否重叠。 -
在将 player.y 递增 ySpeed 之前还是之后重置 player.y 的代码?从 swf 看来,脚暂时低于 platform.y,导致它反弹回来。如果实施得当,它永远不会那么低
-
@xxbbcc 显然,我误解了为什么 hitTestObject 这样做。我认为这是hitTestObject 中的一个错误... Aaron 的回答正确解释了为什么hitTestObject 会这样做。显然,玩家只是在一段距离后才与平台发生碰撞;感谢 ySpeed 的增量。嗯,很高兴知道。
标签: actionscript-3 flash animation hittest