Java - 阻止图块的更好方法(登录问题)

发布于 2024-09-09 10:35:47 字数 504 浏览 3 评论 0原文

好吧,我正在制作这个 2D Tile RPG... 这是一个实时小程序: http://mystikrpg.com/so/

代码也位于 http://mystikrpg.com/so/

无论如何,如果你检查代码,似乎我只能阻止图块 24(这是墙壁)...

你们中的任何一位聪明的java专家是否看到我可以有多个被阻挡的瓷砖的方法?

我已经压缩了所需的文件,以便您可以在这里玩:http://mystikrpg .com/so/2drpg.zip

我希望我可以阻止多个图块...:)

谢谢

Ok so I have this 2D Tile RPG I am making....
here is a live applet:
http://mystikrpg.com/so/

The code is also at http://mystikrpg.com/so/

Anyway, if you examine the code, it seems the I can only block tile 24 (which are the walls)...

Do any of you awesome wise java experts see any way I can have MULTIPLE tiles that are blocked?

I have zipped the required files so that you may play around if you want/can here: http://mystikrpg.com/so/2drpg.zip

I hope I can block more than one tile... :)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一枫情书 2024-09-16 10:35:47

这是您的问题的代码摘要:

int[][] board; // row tiles for ultimate mapping experience!

private static int BLOCKED = 24;

public boolean blocked(int tx, int ty) {
  return board[ty][tx] == BLOCKED;
}

它清楚地表明您只阻止了“24”。您可以通过多种方法来执行此操作。看看你的代码,我同意 Jon Skeet 的观点,即它不是非常面向对象,最好使用封装和对象来帮助你的代码和设计。但要回答您的问题,您只需将被阻止的图块放入哈希集中并检查它们是否包含该图块,然后将其返回为“可阻止”。例如

private static final HashSet<Integer> BLOCKED_TILES = new HashSet<Integer>();
static {
  BLOCKED_TILES.add(24);
  BLOCKED_TILES.add(1);
  //add more tiles here
}

public boolean blocked(int tx, int ty) {
  return BLOCKED_TILES.contains(board[ty][tx]);
}

Here's the code summary of your problem:

int[][] board; // row tiles for ultimate mapping experience!

private static int BLOCKED = 24;

public boolean blocked(int tx, int ty) {
  return board[ty][tx] == BLOCKED;
}

It clearly shows that you are only blocking "24". There are several approaches in which you can do this. Looking at your code, I agree with Jon Skeet that it's not very object oriented and it is better to use encapsulation and objects to help you in your code and design. But to answer your question, you can just simply put the blocked tiles in a HashSet and check if they contain the tile, then return it as "blockable". E.g.

private static final HashSet<Integer> BLOCKED_TILES = new HashSet<Integer>();
static {
  BLOCKED_TILES.add(24);
  BLOCKED_TILES.add(1);
  //add more tiles here
}

public boolean blocked(int tx, int ty) {
  return BLOCKED_TILES.contains(board[ty][tx]);
}
纵山崖 2024-09-16 10:35:47

嗯,有一个被阻止的图块集合或数组,并更改 blocked() 方法以查看给定图块是否在该集合中?

更好的方法是棋盘不仅是整数,而且是对 Tile 类实例的引用,该类知道有关该类型图块的所有信息 - 它的图像、是否被阻止以及其他任何内容相关的。这样您就可以将有关图块的信息封装在一个位置 - 然后以简单的方式询问有关特定图块的信息。

Um, have a collection or array of tiles which are blocked, and change the blocked() method to see whether the given tile is in that collection?

A better way would be for the board not just to be integers, but references to instances of a Tile class which knows everything about that type of tile - its image, whether it's blocked, and anything else which is relevant. That way you can encapsulate the information about tiles in a single place - then ask for information about a particular tile in an easy way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文