Xcode 场景编辑器似乎没有提供像 Storyboard 那样定义布局约束的方法,以便您可以使用场景编辑器直观地放置和组织静态 Sprite (类似于您如何使用 Storyboard 排列 UIButton)。
有人可以确认这些事情在场景编辑器中是不可能的:
1) 相对于其他 Sprite 和场景使用约束定位 Sprite (即, Sprite A 相对于 Sprite B 居中)?
2) 假设#1 是可能的,然后根据设备调整布局约束和 Sprite 大小?
最佳答案
Sprite-kit 中没有类似于 UIKit 约束的布局约束,例如您标记自动布局和 所有元素被重新定位。
在这个地方,它可能没有意义,因为你可以有长长的背景,人物不断改变他们的大小,消失或重新出现的物体,以及物理定律涉及的其他元素。
正如 Knigh0fDragon 所评论的,这就是原因 SKSceneScaleMode
存在
The modes that determine how the scene’s area is mapped to the view that presents it
enum SKSceneScaleMode : Int {
case Fill
case AspectFill
case AspectFit
case ResizeFill
}
但是在 Sprite-kit 中有
SKConstraints
:An SKConstraint object describes a mathematical constraint on a node’s position or orientation. Constraints are attached to nodes; after a scene processes any actions and physics interactions, it applies constraints attached to nodes in its node tree. Use constraints to ensure that certain relationships are true before a scene is rendered
因此,换句话说,
SKConstraint
用于限制特定节点的位置和方向,例如,当您希望玩家停留或跑到特定位置并且您有一些玩家必须遵循的引用点/元素时,可以使用它。一个例子:
let range = SKRange(lowerLimit:50, upperLimit:150)
let leftConstraint = SKConstraint.distance(range, toNode:guardrail)
car.constraints = [leftConstraint]
关于ios - 使用 Xcode 场景编辑器的布局约束来布局类似于 Storyboard 的 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38536752/