I am trying to build a Breakout game where I set the bricks exactly how I want them for each level using 1 SKSpriteNode. It all works, as in they spawn but way off screen to the bottom left. I am using the following line right at the start of didMove(to view: SKView):
scene?.anchorPoint = CGPoint(x: 0.0, y: 0.0)
And the code I use for spawning the bricks is:
//MARK: Brick Layout Config
let widthOfTiles = 7
let heightOfTiles = 3
let widthPoints = 50
let heightPoints = 20
// Different tile arrangements (e.g. 7 tiles wide, 3 high, 7*3 = 21 total tiles)
var brickArray = [false, true, false, true, false, true, false,
false, true, true, true, false, true, false,
false, true, false, true, false, true, false] // Says 'HI' using tiles
override func didMove(to view: SKView) {
//MARK: Brick
for tile in 0..<brickArray.count {
if brickArray[tile] == true {
let brick = SKSpriteNode(imageNamed: "red_brick")
brick.position = CGPoint(x: (tile % widthOfTiles) * (widthPoints) - (widthOfTiles / 2 * (widthPoints)),
y: ((tile - (tile % widthOfTiles)) / widthOfTiles) * (heightPoints) - (heightOfTiles / 2 * (heightPoints)))
brick.size = CGSize(width: widthPoints, height: heightPoints)
brick.physicsBody = SKPhysicsBody(rectangleOf: brick.frame.size)
brick.physicsBody?.friction = 0
brick.physicsBody?.allowsRotation = false
brick.physicsBody?.friction = 0
brick.physicsBody?.isDynamic = false
brick.physicsBody?.categoryBitMask = brickCategory
self.addChild(brick)
}
}
}
I cannot for the life of me get this to be in the center of the scene. Maybe someone can help me figure this out. Maths is a bad weak point of mine and while I think I got the equation right for the spawning, the position is way off.
If I am missing something that you need to see please let me know.
Unless you need an animation that depends on the anchor point I would not set it and leave it at its default value or it will change the visual representation of the position of the layer.