gpt4 book ai didi

Java 2D 游戏 - 仅当玩家在地面上时跳跃

转载 作者:行者123 更新时间:2023-11-30 10:52:52 25 4
gpt4 key购买 nike

我正在制作 2D 平台游戏,只是添加了重力和跳跃。

但是,如果在玩家完成跳跃后空格键仍被按住,玩家就会在半空中继续跳跃。

我知道我需要检查玩家是否真的在地面上,但当我这样做时,它总是返回“false”,如我的跳跃方法中所述:

// Controls both falling and the jumping action.
// The MapObject list is a collection of every object the player can
// collide with on that map. Currently it only contains a single 'ground' object.

public void fall(ArrayList<MapObject> objects)
{
int distance = 0;

if (jumpTicks > 0)
{
float jumpHeight = jumpSpeed * (jumpTicks / maxJumpTicks);
System.out.println(jumpTicks + "/" + maxJumpTicks + " = " + jumpHeight);
y -= jumpHeight;
jumpTicks--;
}
else
{
for (MapObject obj : objects)
{
if (this.y + this.height >= obj.y)
{
// This cancels falling if the player's feet are on top
// of the ground, but for some reason setting an 'isOnGround'
// boolean to 'true' here and checking for it in the 'jump()'
// method does not work, it's always 'false'.
return;
}
distance = obj.y - (this.y + this.height);
}
if (distance > fallSpeed)
{
y += fallSpeed;
}
else
{
y += distance;
}

}
}

// This doesn't make the player jump, it just adds jump time to the player
// if it's not already jumping.

public void jump()
{
if (jumpTicks > 0)
{
return;
}
this.jumpTicks = maxJumpTicks;
this.jumpSpeed = 10;
}

最佳答案

在你改变他的速度之前,检查他的y地板值

function jump() {
if (jumpTicks > 0) {
return;
}
if (this.y === floorY) {
this.jumpTicks = maxJumpTicks;
this.jumpSpeed = 10;
}
}

关于Java 2D 游戏 - 仅当玩家在地面上时跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34244342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com