二维瓦片地图上字符的控制和引用方法

发布于 2024-12-01 14:47:17 字数 557 浏览 2 评论 0原文

我正在制作一个角色移动信使

(二维图块地图游戏看起来像信使)

我希望每个图块都能引用哪个角色。

简而言之,我的程序需要逐个字符地引用 x,y 坐标。

我尝试让每个图块都有一个用于角色 obj 结构的空间。

这种方法的优点是每个字符都可以通过 x,y 坐标来引用,

但是将字符从这里移动到那里是相当困难的。

将对象从这里复制到那里并删除原始xy坐标的对象

,最后将obj控制指针(用于控制)从原始x,y的对象更改为dest x,y 的对象。

它不是那么直接,也不容易控制,也不节省内存。

所以我考虑第二种方法。

有tile数组和obj数组。

渲染 obj 时,只需参考 obj 的 x,y 即可。

当用户想要移动对象时,只需更改 x,y 即可。

但是如果在引用 obj 时出现问题,

我必须搜索 obj 数组。

我的问题是这样的。

有没有什么好的方法可以控制二维瓦片地图上的字符(obj)?

任何建议将不胜感激。

I am making a character moving messanger

(2d tile map game looking messanger)

I want each tile to refer which character is on.

in short, my program need to refer character by character's x,y coordinate.

I tried make each tile to have a space for character obj structure.

good point of this method is that each character can be refered by x,y coordinate

but it's been pretty hard to move a character here to there.

copy the object from here to there and delete original x y coordinate's object

and lastly, change obj control pointer(for controlling) from original x,y's object to dest
x,y's object.

it is not that straight forward, nor easy to control, nor memory saving.

so I consider a second method.

there are tile array and obj array.

when render the objs, just refer to the obj's x,y.

when user want to move the obj, just change x,y.

but if when refering to the obj comes in issue,

I have to search obj array all over.

my question is this.

is there any good method for character(obj) controlling on 2d tile map?

any suggestion would be appreciated.

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

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

发布评论

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

评论(1

不如归去 2024-12-08 14:47:17

您没有指定编程语言,因此我将在示例中使用 C#。

地图上存在的角色和其他对象通常存储在单独的列表中,而不是二维数组中。每个角色都会存储他们自己的 x 和 y 坐标。类似的东西:

class Character
{
    int x, y;

    public int X
    {
        get{ return x; }
        set{ x = value; }
    }

    public int Y
    {
        get{ return y; }
        set{ y = value; }
    }

}

你会将它们存储在一个列表中,例如:

List<Character> characters = new List<Character>();

如果你确实需要在特定图块上查找字符,你会做这样的事情(可能有一种 linq 方法可以在 1 行中完成此操作,但这更容易理解和阅读):

int searchX = 10;
int searchY = 15;
Character characterOnLocation = null;
foreach(Character character in characters)
{
    if(character.X == searchX && character.Y == searchY)
    {
        characterOnLocation = character;
        break;
    }
}

您的绘制方法应该循环遍历地形图块(就像它可能已经做的那样),然后循环遍历字符数组并使用它们自己的 X 和 Y 坐标绘制它们。类似于(在本例中使用 XNA):

foreach(Character character in characters)
{
    spriteBatch.Draw(texture, character.X, character.Y);
}

You didn't specify a programming language, so I will use C# in my examples.

Characters and other objects that exist on a map are usually stored in a separate list, not a 2D array. Each character would store their own x and y coordinates. Something like:

class Character
{
    int x, y;

    public int X
    {
        get{ return x; }
        set{ x = value; }
    }

    public int Y
    {
        get{ return y; }
        set{ y = value; }
    }

}

and you would store them in a list like:

List<Character> characters = new List<Character>();

If you really needed to find a character on a particular tile, you'd do something like this (there's probably a linq way to do this in 1 line, but this is easier to understand and read):

int searchX = 10;
int searchY = 15;
Character characterOnLocation = null;
foreach(Character character in characters)
{
    if(character.X == searchX && character.Y == searchY)
    {
        characterOnLocation = character;
        break;
    }
}

Your draw method should loop through the terrain tiles (like it probably does already), then just loop through the Character array and draw them using their own X and Y coordinates. Something like (using XNA in this example):

foreach(Character character in characters)
{
    spriteBatch.Draw(texture, character.X, character.Y);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文