1
func createPaddles() -> SKNode {
    let container = SKNode()
    let colors = [SKColor.green, SKColor.blue, SKColor.red, SKColor.green, SKColor.blue]
    let width = self.frame.size.width
    let height = self.frame.size.height

    for i in 0...4 {
        let node = SKShapeNode(rectOf: CGSize(width: self.frame.size.width / 3, height: self.frame.size.height * 0.15))
        node.fillColor = colors[i]
        node.strokeColor = colors[i]
        node.zPosition = 100
        node.position = CGPoint(x: -width * 1/6 + width / 3 * i, y: height * 0.075)

        container.addChild(node)
    }

    return container
}

node.position is giving me an error saying it's too complex. Is this because calculating the frame width twice in one equation is too much for the compiler? Seems unlikely and this doesn't seem overly complex to me. Am I missing something here?

EDIT: Great group effort from everyone, thank you! This is what eventually worked (a combination of a few different answers):

node.position = CGPoint(x: -width / 6.0 + width / 3.0 * CGFloat(i), y: height * 0.075)
dulongj
  • 307
  • 2
  • 17
  • It's related to type checking. See this answer for a good explanation: https://stackoverflow.com/a/29931329/6658553 – nathangitter Nov 02 '17 at 19:20
  • I see... any suggestions on "dumbing down" this calculation then? I just need the nodes (1/3 screen width in length) placed along the bottom of the screen so that the middle 3 are initially centered on the screen and the first and last are off screen. – dulongj Nov 02 '17 at 19:23
  • @dulongj I just updated my answer and it no longer gives an error for me. – nathangitter Nov 02 '17 at 19:33

4 Answers4

2

One of the reasons why this happens is that you have a type mismatch. However, instead of printing the type mismatch error, the compiler tries to find a way to match the types... and fails.

In your specific case, i is an Int while other variables in the expression are CGFloat:

-width * 1/6 + width / 3 * CGFloat(i)

Also note that 1/6 will be probably considered to be integer division, with the result being 0 although I am not sure about the priority right now. You probably want:

-width / 6 + width / 3 * CGFloat(i)

to be sure.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
1

Generally it has to do with the compiler type system inferring the proper types to execute the statement. You can often shortcut the process by providing the types for it.

Note that simplifying your equation results in this:

node.position = CGPoint(x: width * (i - 0.5) / 3.0, y: height * 0.075)

You can also expressly convert types so that the compiler has less to do:

node.position = CGPoint(x: CGFloat(width) * (CGFloat(i) - CGFloat(0.5)) / CGFloat(3.0), y: CGFloat(height) * CGFloat(0.075))
0

This error occurs when the compiler needs to do a lot of type checking. See this answer for a good explanation: https://stackoverflow.com/a/29931329/6658553

In your specific case, try reducing the number of operators you are using, and split it into multiple calculations.

node.position = CGPoint(x: -width * 1/6 + width / 3 * i, y: height * 0.075)

could be refactored to something like this:

let newXPosition: CGFloat = -width * 1/6 + width / 3 * CGFloat(i)
node.position = CGPoint(x: newXPosition, y: height * 0.075)
nathangitter
  • 9,607
  • 3
  • 33
  • 42
0

It is not a very clear error, but it's because you are trying to multiply CGFloats with Ints

width is a CGFloat

let width = self.frame.size.width

"i" in your for loop is an int

for i in 0...4 {
    let node = SKShapeNode(rectOf: CGSize(width: self.frame.size.width / 3, height: self.frame.size.height * 0.15))
    node.fillColor = colors[i]
    node.strokeColor = colors[i]
    node.zPosition = 100
    node.position = CGPoint(x: -width * 1/6 + width / 3 * i, y: height * 0.075)

    container.addChild(node)
}

You can separate them to sub clocks as others have mentioned (probably preferred) but at the core of it you need to ensure that all of the values in your equation are of the same type

easiest way to make them of the same type would be to cast i to CGFloat

CGFloat(i)
Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32