Change CGSize with a variable

2.8k views Asked by At

I want to change the size of my SKNode with a variable so it can be made smaller with a for loop. It is giving me an error:

Cannot find an initializer for type 'CGSize' that accepts an argument list of type '(width:... , height:...)'

for (var i = 0.9; i > 0.0; i -= 0.1){
           for (var k = 1.25; i > 0.0; i -= 0.1){

                self.sun.size = CGSize(width: self.size.width * i, height: self.size.height * k) // error here
        } 
}   
2

There are 2 answers

0
Steven Moseley On BEST ANSWER

I ran this SKAction code instead of a for loop suggested by ABakerSmith and it looks a lot cleaner.

var shrinkSun = SKAction.scaleBy(0.5, duration: 2)
self.sun.runAction(shrinkSun)
1
Andriy Gordiychuk On

Your code doesn't work because self.size.width is CGFloat and i is Double (same with self.size.height and k). CGFloat cannot be multiplied by a Double. Try the code below.

for (var i = CGFloat(0.9); i > 0.0; i -= 0.1){
        for (var k = CGFloat(1.25); i > 0.0; i -= 0.1){

            self.sun.size = CGSize(width: self.size.width * i, height: self.size.height * k) // error here
        }
    }

I have tested it with Xcode 7 and Swift 2.0. In case something goes wrong code below will definitely work

for (var i = 0.9; i > 0.0; i -= 0.1){
       for (var k = 1.25; i > 0.0; i -= 0.1){

            self.sun.size = CGSize(width: self.size.width * CGFloat(i), height: self.size.height * CGFloat(k)) // error here
    } 
}