- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个 2D 平台游戏,并且即将尝试实现跳跃。然而,我对如何让玩家只落在平台上有点迷失。
我正在考虑创建一个名为 Platform 的新类,然后创建一个包含游戏中所有平台的所有坐标的 ArrayList。
然后我创建一个 while 循环,每当玩家跳跃时,当他下落时,除非他站在平台上,否则它会继续下落?
类似于:
while(// This is where you check if the player's y coordinate is above all of the platforms in the game, so WHILE you are above all of the platforms, continue falling.){
playery--;
repaint();
// Some kind of wait here
}
我在这里采取的方法正确吗?我是否把事情复杂化了?有没有更简单的方法来实现这一目标?
最佳答案
通常你的玩家对象中有一个 update() 和 render() 方法。为了方便重力,您可以在每次更新调用时更新玩家的 y 位置,例如posY+=5。对于高级重力,您可以使用 y 速度来更新 y 位置每次更新。此 y 速度会发生变化,以使运动更加平滑。
您应该有一个 Controller 类,它将游戏中的每个实体保存在某个集合中。在我的游戏中,这个类称为 EntityManager,它保存 LinkedList 中的每个实体。我建议为不同的实体类型建立多个集合。在这种情况下,您可以使用 BlockEntities 来表示构成平台的方形 block 。
现在是最重要的概念。每个实体都有一个返回矩形对象作为碰撞盒的方法。这是我制作的游戏的示例:
public Rectangle getBounds() {
return new Rectangle( x, y, width, height);
}
x、y、宽度和高度是实体的属性。
此外,您的播放器需要一个位于播放器底部的碰撞箱。例如,如果玩家尺寸为 32x32 像素:
public Rectangle getBottomBounds() {
return new Rectangle( x, y+30, width, 2);
}
现在是碰撞控制:在你的播放器中,你需要一个像这样的字段:
protected LinkedList<Block> blocks = null;
在您的播放器中实现此方法:
public void checkBottomCollision() {
blocks = game.getEntityManager().getBlocks();
for (int i = 0; i < blocks.size(); i++) {
Block tempBlock = blocks.get(i);
if(this.getBottomBounds().intersects(tempBlock.getBounds())){
this.y = tempBlock.getY() + this.height;
}
}
}
}
game.getEntityManager() 获取包含游戏中所有实体的 ControllerClass。 getBlocks() 方法返回包含所有 block 的 LinkedList。您检查玩家是否对每个方 block 发生碰撞。如果底部命中框发生碰撞,您将更新位置,以便您的玩家站在方 block 上。更新 y 位置后,您可以在 update-method 中调用此方法。
玩得开心;-)
关于java - 让玩家仅落在平台上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39561153/
这个问题在这里已经有了答案: What does the question mark character ('?') mean in C++? (8 个答案) 关闭 7 年前。 这一行我看不懂为什么
在构建模式下甚至可以有两个玩家吗?查看 Roblox 开发者杂志文章“What did you sleigh?”,它在玩家列表中显示了两个“玩家”。 最佳答案 打开 Roblox Studio 转到任
在构建模式下甚至可以有两个玩家吗?查看 Roblox 开发者杂志文章“What did you sleigh?”,它在玩家列表中显示了两个“玩家”。 最佳答案 打开 Roblox Studio 转到任
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
“Clash of Clans”使用 Game Center 对玩家进行身份验证并将其与现有的远程存储游戏状态相关联。 据我所知,游戏仅在客户端提供玩家标识符。是否有支持的技术来安全地验证用户而不是仅
我正在开发多人游戏,但我无法找出如何将其他客户端连接到创建的游戏。我的意思是客户端 A 创建到服务器的套接字连接,其他客户端(A,B ...)如何连接到客户端 A?有人可以帮我吗? 附注我是网络编程新
我正在尝试使用浏览器控制台一步一步地制作井字游戏,并最终改进我的功能。然而,我被困在玩家2回合(ttt_player2_turn()),我必须检查箱子是否为空。看来我在这个例子中的论证有问题。感谢您的
如果我向上移动玩家 1 和玩家 2,假设我按下玩家 1 的向下键,我的玩家将停止向上移动。我找不到问题所在。有人可以帮助我并解释我做错了什么吗? package game; import java.a
我正在创建一个自上而下的 2D 游戏,并且使用 Box2D 来模拟物理,我的问题是: 如何使玩家保持与我的宇宙飞船的相对速度,并且仍然能够在飞船也在移动的情况下围绕我的玩家移动? 我在下面放了一个插图
我是 Java 新手,我正在尝试制作一个简单的游戏来娱乐。我首先尝试将 repaint 放入 PaintComponent() 中,它一直有效,直到我尝试添加一些背景。有谁知道如何让我的玩家在有或没有
//我正在尝试对玩家 2 的代码进行一些编辑,因此我删除了涉及玩家 1 的所有内容。但出于某种原因,如果没有玩家 1 的代码,玩家 2 根本不会执行任何操作。(假设使用 i、j、k 和 l 键 mov
我接到了一项任务,要编写一个由人类玩家和 AI 玩家组成的 NIM 游戏。游戏是“Misere”(最后一个必须拿起一根棍子的人输了)。 AI 应该使用 Minimax 算法,但它正在采取使其输得更快的
我是一名优秀的程序员,十分优秀!