SpriteKit:在场景中居中 SKNode
SpriteKit: center SKNode in scene
我在将 SKNode 置于主场景的中心时遇到问题。这个(下图)是一个包含 16 个 SKLabelNode 的 SKNode。在代码中,它位于位置 (0, 0),但正如您在图像上看到的那样,它似乎没有居中。此外,Apple 文档没有说明 SKNode 上的锚点。
那么,我怎样才能让 SKNode 在场景中居中呢?
这是代码。
我有一个将标签存储在数组中的结构。我将它们初始化为 0.
struct NumberButtonsMatrix {
let rows: Int, columns: Int
var grid: [NumberButton]
init(rows: Int, columns: Int) {
grid = [NumberButton]()
self.rows = rows
self.columns = columns
for _ in 0..<(rows*columns) {
let button = NumberButton(number: 0)
grid.append(button)
}
}
}
func indexIsValid(row: Int, column: Int) -> Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
}
func indexIsValid(index :Int) -> Bool {
return index >= 0 && index < rows*columns
}
subscript(row: Int, column: Int) -> NumberButton {
get {
assert(indexIsValid(row: row, column: column), "Index not valid")
return grid[(row*columns) + column]
}
set {
assert(indexIsValid(row: row, column: column), "Index not valid")
grid[(row*columns) + column] = newValue
}
}
subscript(index: Int) -> NumberButton {
get {
assert(indexIsValid(index: index), "Index not valid")
return grid[index]
}
set {
assert(indexIsValid(index: index), "Index not valid")
grid[index] = newValue
}
}
我在网格中排列节点的结构中也有这个函数。 (下标也在struct上但我觉得没必要放在这里)
func arrangeButtonsInGrid(separation: Int) {
for i in 0..<rows {
for j in 0..<columns {
self[i, j].position = CGPoint(x: -separation*i, y: separation*j)
}
}
}
然后,我有一个名为 GameBoard 的 class,它是所有 SKLabel 所在的 SKNode 的子class。
class GameBoard: SKNode {
var numberButtons: NumberButtonsMatrix
init(rows: Int, columns: Int) {
numberButtons = NumberButtonsMatrix(rows: rows, columns: columns)
super.init()
numberButtons.arrangeButtonsInGrid(separation: 144)
for i in 0..<(rows*columns) {
numberButtons[i].number = i
self.addChild(numberButtons[i])
}
}
最后,我将它们添加到我的场景中。
var gameBoard = GameBoard(rows: 4, columns: 4)
gameBoard.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
self.addChild(gameBoard)
您的设置可能有很多问题,只有您提供一些代码我们才能知道。
使 SKNode 在屏幕上居中 os 非常简单,里面的内容没有区别。
最大的因素是场景定位点设置的内容。
这里是场景锚点设置为0, 0,容器(SKNode)进入画面的例子
let size = CGSize(width: 50, height: 50)
let container = SKNode()
container.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
for row in -1...1 {
for col in -1...1 {
let label = SKLabelNode()
label.text = "0"
label.position = CGPoint(x: size.width * CGFloat(row), y: size.height * CGFloat(col))
container.addChild(label)
}
}
addChild(container)
****编辑****
问题不在于您的节点或锚点。问题出在您的 arrangeButtonsInGrid 函数中。
需要考虑的事项,如果您在 SKNode 的功能有限方面遇到问题,只需将其转换为 SKSpriteNode(我通常这样做是为了测试并将其背景颜色设置为红色,以便我可以准确地看到它的布局位置)
可能有上千种不同的方法来解决您的问题,这里只是一个快速简单的解决方案(另一种选择是使容器成为 SKSpriteNode 并将其锚点设置为零)。看起来您正在为 Swift 2.3 或 3 编译代码,因此您可能需要调整一些内容
func arrangeButtonsInGrid(padding: Int) {
var yPosition = -(rows / 2)
for row in 0..<rows {
var xPosition = -(columns / 2)
for col in 0..<columns {
self[col, row].position = CGPoint(x: padding * xPosition, y: padding * yPosition)
xPosition += 1
}
yPosition += 1
}
}
我在将 SKNode 置于主场景的中心时遇到问题。这个(下图)是一个包含 16 个 SKLabelNode 的 SKNode。在代码中,它位于位置 (0, 0),但正如您在图像上看到的那样,它似乎没有居中。此外,Apple 文档没有说明 SKNode 上的锚点。
那么,我怎样才能让 SKNode 在场景中居中呢?
这是代码。
我有一个将标签存储在数组中的结构。我将它们初始化为 0.
struct NumberButtonsMatrix {
let rows: Int, columns: Int
var grid: [NumberButton]
init(rows: Int, columns: Int) {
grid = [NumberButton]()
self.rows = rows
self.columns = columns
for _ in 0..<(rows*columns) {
let button = NumberButton(number: 0)
grid.append(button)
}
}
}
func indexIsValid(row: Int, column: Int) -> Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
}
func indexIsValid(index :Int) -> Bool {
return index >= 0 && index < rows*columns
}
subscript(row: Int, column: Int) -> NumberButton {
get {
assert(indexIsValid(row: row, column: column), "Index not valid")
return grid[(row*columns) + column]
}
set {
assert(indexIsValid(row: row, column: column), "Index not valid")
grid[(row*columns) + column] = newValue
}
}
subscript(index: Int) -> NumberButton {
get {
assert(indexIsValid(index: index), "Index not valid")
return grid[index]
}
set {
assert(indexIsValid(index: index), "Index not valid")
grid[index] = newValue
}
}
我在网格中排列节点的结构中也有这个函数。 (下标也在struct上但我觉得没必要放在这里)
func arrangeButtonsInGrid(separation: Int) {
for i in 0..<rows {
for j in 0..<columns {
self[i, j].position = CGPoint(x: -separation*i, y: separation*j)
}
}
}
然后,我有一个名为 GameBoard 的 class,它是所有 SKLabel 所在的 SKNode 的子class。
class GameBoard: SKNode {
var numberButtons: NumberButtonsMatrix
init(rows: Int, columns: Int) {
numberButtons = NumberButtonsMatrix(rows: rows, columns: columns)
super.init()
numberButtons.arrangeButtonsInGrid(separation: 144)
for i in 0..<(rows*columns) {
numberButtons[i].number = i
self.addChild(numberButtons[i])
}
}
最后,我将它们添加到我的场景中。
var gameBoard = GameBoard(rows: 4, columns: 4)
gameBoard.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
self.addChild(gameBoard)
您的设置可能有很多问题,只有您提供一些代码我们才能知道。
使 SKNode 在屏幕上居中 os 非常简单,里面的内容没有区别。
最大的因素是场景定位点设置的内容。
这里是场景锚点设置为0, 0,容器(SKNode)进入画面的例子
let size = CGSize(width: 50, height: 50)
let container = SKNode()
container.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
for row in -1...1 {
for col in -1...1 {
let label = SKLabelNode()
label.text = "0"
label.position = CGPoint(x: size.width * CGFloat(row), y: size.height * CGFloat(col))
container.addChild(label)
}
}
addChild(container)
****编辑****
问题不在于您的节点或锚点。问题出在您的 arrangeButtonsInGrid 函数中。
需要考虑的事项,如果您在 SKNode 的功能有限方面遇到问题,只需将其转换为 SKSpriteNode(我通常这样做是为了测试并将其背景颜色设置为红色,以便我可以准确地看到它的布局位置)
可能有上千种不同的方法来解决您的问题,这里只是一个快速简单的解决方案(另一种选择是使容器成为 SKSpriteNode 并将其锚点设置为零)。看起来您正在为 Swift 2.3 或 3 编译代码,因此您可能需要调整一些内容
func arrangeButtonsInGrid(padding: Int) {
var yPosition = -(rows / 2)
for row in 0..<rows {
var xPosition = -(columns / 2)
for col in 0..<columns {
self[col, row].position = CGPoint(x: padding * xPosition, y: padding * yPosition)
xPosition += 1
}
yPosition += 1
}
}