2

I'm having trouble centering a SKNode on my main scene. This (photo below) is a SKNode that contains 16 SKLabelNodes. In the code, it sits at position (0, 0), but as you can see on the image, it doesn't seem to be centered. Also, Apple documentation doesn't say anything about anchorPoint on SKNodes.

enter image description here

So, how can I center a SKNode on a scene?

This is the code.

I have a struct that stores the labels in an array. I init them to 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
    }
}

I also have this function in the struct that arranges the nodes in a grid. (The subscript is also on the struct but I don't think it's necessary to put it here)

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)

        }
    }

}

Then, I have a class called GameBoard that is a subclass of SKNode where all the SKLabels are at.

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])

    }



}

Finally, I add them on my scene.

var gameBoard = GameBoard(rows: 4, columns: 4)
    gameBoard.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
    self.addChild(gameBoard)
José María
  • 2,835
  • 5
  • 27
  • 42

1 Answers1

4

there is so many things that could be wrong with you setup, that we won't know until you post some code.

Centering an SKNode on the screen os really simple and makes no difference what is inside of it.

The biggest factor is what your Scene anchor point is set to.

Here is an example when the scene anchor point is set to 0, 0 and the container (SKNode) is entered in the screen

    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)

****EDIT****

The problem isn't with your node or anchorPoints. The problem is in your arrangeButtonsInGrid function.

Somethings to consider, If you are having trouble with limited functionality of an SKNode just turn it into a SKSpriteNode (I usually do this for testing and make its background color red so that I can see exactly where it is laying out)

There are probably a thousand different ways to fix your issue, here is just a quick simple solution (another option could be to make the container a SKSpriteNode and set it's anchor to zero). It looks like you are compiling your code for Swift 2.3 or 3 so you may need to adjust some things

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
    }
}
Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32
  • @José you need to review answers and mark them as correct if they help you. I notice that your last 7 questions were left unmarked on the answers. People spend a lot of time to help on questions and deserve to be rewarded on such. – Ron Myschuk Jun 20 '16 at 15:00
  • Sorry, I've been busy until now. Your answer works perfectly, thanks! – José María Jun 20 '16 at 18:23