我只是想知道最好的数据结构是什么来表示横向卷轴游戏中的数据对象,基本上用户看不到的东西不在舞台上所以我通常会创建一个自定义类来定义它的属性(x、y、weight、type、derp 等),然后将其插入一个数组,当用户向前移动时,我从数组中转换信息,同时检查它们的 x 位置是否合适。
可能出现的问题是用户可以左右移动,那么跟踪索引的最佳方式是什么?
谢谢
最佳答案
所以你有很多对象的长关卡,你不想线性检查所有对象以查看屏幕上应该显示什么。您需要将数据分成 block 并列出这些 block 。这让我想起了Quadtrees ,如果你的射手是线性的,你的树就会变成一维的,而且实现起来更简单。
您可能会使用的结构:
Node - 包含范围 (xmin, xmax) 和子节点或叶子的列表。所有节点都插入到层次结构中,其中根节点是整个游戏世界/关卡,根子节点是区域,然后是子区域等等。树中的层数取决于对象的数量。
Leaf - 也有范围,包含与该区域对应的数据对象。
构建这棵树后,从给定范围内定位对象应该花费 O(log N) 时间。
编辑:我将尝试对其进行说明以使其更加清晰。
如果你的关卡是一维的,你不需要完整的四叉树,只需要线性树:
root node, xmin = 0, xmax = 1000
contains list of regions:
/ | \
/ | \
region 1, | \
xmin = 0, xmax = 300 | \
| \
region 2, \
xmin = 301, \
xmax = 650 region 3, xmin = 651, xmax = 1000
| |
Leaf, xmin = 652, xmax = 850 |
list of objects Leaf, xmin = 851, xmax = 1000
in that range list of objects
public class GameObject
{
public var x:Number;
//other stuff
}
public class Node {
public var xmin, xmax:Number;
public var children:Vector.<Node>;
public function writeObject(object:GameObject):void
{
for each (var node:Node in children)
{
//find node with corresponding range
if (node.xmin > object.x && node.xmax <= object.x)
{
node.writeObject(object);
return;
}
}
throw new Error("Children not found!");
}
public function findObjects(xmin:Number, xmax:Number):Vector.<GameObject>
{
//there comes the tricky part
//1. build list of leaves in that range
//2. filter first and last leaf (they may be only partially in the range)
//3. copy objects from leaves between first and last as is
//4. return list of objects
}
}
public class Leaf extends Node {
private var objects:Vector.<GameObject>;
public function writeObject(object:GameObject):void
{
objects.push(object);
}
}
首先您需要创建连接在树中的节点,根据您的级别选择区域数量。然后你将你的对象写入结构中,并努力思考以后如何获取它们:)
关于flash - 横向卷轴游戏的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5447279/