我必须在我的大学课程中为一个项目写一个XNA游戏。我正在玩Gameboy Pokemon游戏中的回合制RPG游戏。我想将菜单选项与屏幕底部的容器对齐,不超过4个选项。这应该让你大致了解我希望战斗画面看起来如何:
我目前有一个菜单系统,可以将所有菜单项与屏幕中心对齐,这意味着菜单最终看起来像这样:
开始游戏
选项
退出
但在屏幕的中央。
以与上图相似的方式定位菜单项的最佳方法是什么?我是否应该在菜单项的位置进行硬编码,我是否应该尝试将某种类作为这些项目的容器并使其位置相对于容器的大小和位置,或者使用其他方法? / p>
答案 0 :(得分:1)
如果您不想对位置进行硬编码,则可能会变得非常复杂,具体取决于您需要多大的灵活性。
我将创建一个容器类,它是树结构的一部分(参见scene graph),包含父容器和子容器列表。这是处理相对定位的最常用方式。这是一个简单的例子:
public class Container
{
public Container Parent { get; set; }
public List<Container> Children { get; set; }
public Vector2 RelativePosition { get; set; }
public Vector2 AbsolutePosition
{
get
{
// The container is the root node
if (Parent == null)
return RelativePosition;
else
return RelativePosition + Parent.AbsolutePosition;
}
}
}
如果您需要更多灵活性,可以创建浮动布局,其中元素根据其大小动态定位。
答案 1 :(得分:0)
正如Lucius所说,创建Container类是最好的解决方案。 目前我正在为XBox开发一个UI应用程序。
因此我需要像定位引擎这样的东西,一切都是相对的,所以我不需要每次都计算像素。
我所做的是创建一个Container类,它包含大致以下属性:
VectorTopLeft (Which the element which contains a Container object uses for drawing)
VectorTopRight
VectorBottomLeft
VectorBottomRight
Align (Enum: Right, Center, Left)
VerticalAlign (Enum: Top, Middle, Bottom)
NewRow (bool)
PreviousContainer (Container)
ParentContainer (Container)
Width (Getter)
Height (Getter)
PercentageHeight (getter/setter) (Percentage of the height of the parent container)
PercentageWidth (getter/setter) (Percentage of the width of the parent container)
PixelHeight (getter/setter) (Absolute height in pixels)
PixelWidth (getter/setter) (Absolute width in pixels)
AspectRatio: Used for setting the width to a ratio of the height, usefull for different screen aspects (4/3 or 16/9 for example)
MarginLeft
MarginRight
MarginTop
MarginBottom
以下向量包括边距,这些是对齐过程使用的向量。
AbsoluteVectorTopLeft
AbsoluteVectorTopRight
AbsoluteVectorBottomLeft
AbsoluteVectorBottomRight
以下尺寸属性还包括边距,可用于计算剩余尺寸
AbsoluteWidth (getter)
AbsoluteHeight (getter)
然后,如果某些重要事项发生变化,则会将某些标志设置为true,并且需要重新计算矢量/大小的内容。
对齐的东西非常复杂,因为它使用递归,并且还调用以前的容器函数来将所有内容移动到正确的位置。
newrow属性告诉系统它需要在父容器的新行启动元素,并用于保持垂直对齐。
系统可能存在一些小缺陷,但此时它可以作为我所有GUI相关定位内容的魅力,并且它的工作速度非常快!