MysteryPancake - 9 months ago 73

Swift Question

This should be a very easy question for anyone here who's decent at math... unlike me.

As the title says, I'm trying to get some SKSpriteNodes to align just right with a 24-pixel margin on all sides. The closest I've got so far is with this:

`for i in 1...3 {`

let node = SKSpriteNode(color:UIColor.black,size:CGSize(width:size.width-24,height:size.height/CGFloat(3)-12))

let y = ((size.height/CGFloat(3))*CGFloat(i))-(node.size.height/2)

node.position = CGPoint(x:size.width/2,y:y)

addChild(node)

}

By the way, the 'size' variable in the code above is just set to be the size of the current view.

That code gives me this: imgur.com/fBteyfZ.png

But the problem with the image above is that you can see the top node doesn't have a margin at the top. Anyone know the math I'm after to get the alignment right? Also, here's a more formula-like version of the code before:

`let scrw, scrh = Total screen width and height of the device`

for i in 1...3 {

let h = ( scrh / 3 ) - 12

let y = ( ( scrh / 3 ) * i ) - ( h / 2 )

}

EDIT: I changed what I was doing to this:

`let scrw, scrh = Total screen width and height of the device`

for i in 1...3 {

let divide = ( scrh - 12 ) / 3

let h = divide - 12

let y = 6 + divide * i - ( divide / 2 )

}

Which works, but I feel like it could be simplified