Alex Alex - 5 months ago 8
Swift Question

How to scale a radius to fit in all devices?

I am makeing a game in with a ball follows a path, actually is just an illution because the scene is the one that rotates. The problem is that in some devices the ball get out of the screen and in others is the radios looks very small. I have tryed to make the radios equal (orbita.size.width / 2) but it doesnt work. (orbita is the órbita that the ball follows)

class GameScene: SKScene {

let sprite = SKSpriteNode(imageNamed: "circulo")
var rotation:CGFloat = CGFloat(M_PI)
let radius:CGFloat = 168


override func didMoveToView(view: SKView) {
/* Setup your scene here */

scaleMode = .ResizeFill
node.position = view.center
// 3) Add the container to the scene
addChild(node)
// 4) Set the sprite's x position
sprite.position = CGPointMake(radius, 0)
// 5) Add the sprite to the container
node.addChild(sprite)
// 6) Rotate the container
rotate()
sprite.color = UIColor.whiteColor()
sprite.colorBlendFactor = 1.0
sprite.zPosition = 4.0



orbita = SKSpriteNode(imageNamed: "orbita")
let padding2:CGFloat = 32.0
orbita.size = CGSize(width:view.frame.size.width - padding2 , height: view.frame.size.width - padding2)
orbita.color = UIColor.whiteColor()
orbita.colorBlendFactor = 1
orbita.alpha = 1
orbita.position = view.center
self.addChild(orbita)
orbita.zPosition = 3
}

Answer

If your orbita.size is (it seems a circular orbita by "circulo" name):

orbita.size = CGSize(width:view.frame.size.width - padding2 , height: view.frame.size.width - padding2)

your radius would be:

let radius:CGFloat = (view.frame.size.width - padding2)/2