-2

bros

I'm making a simple brick styled game using SpriteKit and I'm having a compile time error which says:

Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

What I'm trying to do, is, attempting to generate the bricks on the screen using nested for loop which has to yield 5 rows and 6 columns of bricks.

Here is my code:

for var row=0; row<5; row++
    {
        for var column = 0; column < 6; column++
        {
            let brick = SKSpriteNode(imageNamed: "brick")
            brick.position = CGPoint(x: 2+(brick.size.width/2) + ((brick.size.width + 3) * CGFloat(column)), y: -(2+(brick.size.height/2) + ((brick.size.height + 3) * CGFloat(row))))
                self.addChild(brick)

        }
    }

I tried, splitting up the addition into 2 parts for each X and Y axis, but then I have other problem, the brick's Y position is wrong and is located on the bottom of my screen and overlapping it. Any ideas what should I do? What causes this compile-time error?

I'm using XCode 7.2.1, Swift 2.1.1

Sulthan
  • 128,090
  • 22
  • 218
  • 270
UFOENGINE
  • 7
  • 1
  • 4
  • This answer might help: http://stackoverflow.com/questions/29707622/bizarre-swift-compiler-error-expression-too-complex-on-a-string-concatenation/29931329#29931329 – Aaron Rasmussen Feb 17 '16 at 23:39
  • 1
    You should add the code how you tried to split the expressions. That's your real problem. – Sulthan Feb 18 '16 at 00:16

3 Answers3

2

Just for legibility, understandability and debuggability split:

brick.position = CGPoint(x: 2+(brick.size.width/2) + ((brick.size.width + 3) * CGFloat(column)), y: -(2+(brick.size.height/2) + ((brick.size.height + 3) * CGFloat(row))))

The line is so long it can't all even all be seen at the same time.

into:

let xPos =   2+(brick.size.width/2)  + ((brick.size.width  + 3) * CGFloat(column))
let yPos = -(2+(brick.size.height/2) + ((brick.size.height + 3) * CGFloat(row))   )
brick.position = CGPoint(x: xPos, y:yPos)

That will also help you find where the error is, see the answer by @Alain T..

zaph
  • 111,848
  • 21
  • 189
  • 228
1

This error is generally caused by the compiler not being able to figure out the type of some part of the expression.

If you use CGPointMake(...) instead, because the type of the parameters is pre-determined, you will not get the error

you will need to remove the x: and y: though

Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • That gave me some hints. I managed to solve this by using, yes CGPointMake but also after I had to replace the values of the int numbers to decimal point type such as CGFloat, and also I casted the var of the both for loops to be CGFloat. – UFOENGINE Feb 17 '16 at 23:59
-1

I suggest :

let brickTemp = SKSpriteNode(imageNamed: "brick")
let brickWidth = brickTemp.size.width
let brickHeight = brickTemp.size.height
for row in 0..<5 {
    let y = 2 + (brickHeight / 2) + ((brickHeight + 3) * CGFloat(row))
    for column in 0..<6 {
        let brick = SKSpriteNode(imageNamed: "brick")
        let x = 2 + (brickWidth / 2) + ((brickWidth + 3) * CGFloat(column))
        brick.position = CGPoint(x: x, y: -y)
        self.addChild(brick)
    }
}
  • You are adding the same instance as a child, over and over. I don't think that's how the display hierarchy works. From the docs for `-addChild:`: "parameter node: The node to add. The node must not already have a parent." – Nicolas Miari Feb 18 '16 at 04:36