我正在制作一个2D平台游戏,我试图在平台上添加碰撞,这样当角色击中它时,它就不能通过。我很难找到用来创建这种冲突的语法。到目前为止,这就是我所拥有的。
此外,我仍然希望能够在if语句中使用hitTestObject。
谢谢
public function platform1Collision():void
{
if (fireboy1.hitTestObject(Platform1))
{
//fireboy1 cannot pass through
}
}发布于 2014-12-11 15:55:00
您可能希望阻止fireboy1的y属性扩展过去Platform1的y属性:
function platform1Collision():void
{
if(fireboy1.hitTestObject(Platform1))
{
if(fireboy1.y > Platform1.y)
{
fireboy1.y = Platform1.y + Platform1.height;
}
else
{
fireboy1.y = Platform1.y - fireboy1.height;
}
}
}注意:上面的代码示例对fireboy1和Platform1都采用左上角的方向。
编辑的:上面编辑的代码将允许fireboy1在Platform1下面行走,但不会通过它。
这是一个非常基本的例子,可以让您了解可以使用的逻辑类型。如果希望允许fireboy1从下面通过Platform1,则必须更新逻辑以实现这一点。例如,如果您取出if/else并在每次碰撞时自动将fireboy1放置在Platform1之上,那么当player1从下向下逼近时,似乎是在跳到Platform1上。
https://stackoverflow.com/questions/27425882
复制相似问题