Search code examples
swiftsprite-kitgameplay-kit

SpriteKit Array Error SKNode.obstaclesFromNodes


I'm trying to create an array of obstacles from the nodes in my SpriteKit game. I created a new project to try and solve the error and found it still happens.

This is the code in the new project:

import SpriteKit

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        let spriteNode = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 100, height: 100))
        spriteNode.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
        self.addChild(spriteNode)

        let nodes = self.children

        let obstacles = SKNode.obstaclesFromNodeBounds(nodes)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    }

    override func update(currentTime: CFTimeInterval) {
    }
}

I have checked and nodes is not nil. It contains spriteNode.

It crashes on the let obstacles line with the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'

I've searched around but haven't been able to find anything on the matter.

Does anyone know what might the problem be?


Solution

  • The issue was I didn't import "GameplayKit"