0

I am building an app using circles. I'm fairly new to iOS programming, and I'm digging into SpriteKit. I found a code sample here:

func circlePhysicsDefault() {
    var Circle = SKShapeNode(circleOfRadius: 40)
    Circle.position = CGPointMake(500, 500)
    Circle.name = "defaultCircle"
    Circle.strokeColor = SKColor.blackColor()
    Circle.glowWidth = 10.0
    Circle.fillColor = SKColor.yellowColor()
    Circle.physicsBody = SKPhysicsBody(circleOfRadius: 40)
    Circle.physicsBody!.dynamic = true
    self.addChild(Circle)
}

However, the final line:

    self.addChild(Circle)

results in an error saying the viewController does not have a member named 'addChild'. I'm stumped as to what's going on here after digging through the documentation for SKShapeNode and other SO questions.

Some people think it's an error related to optionals. If so, I'm unclear what to change to fix it. Any ideas?

Thank you so much for your help in advance.

user1186742
  • 389
  • 2
  • 9
  • 14
  • 2
    The error tells you exactly the problem. View controllers do not have the function to add sprites as children, because they are not a sprite themselves. You need to add a sprite as a child of another sprite or a scene. – Andrew Monshizadeh Jun 08 '15 at 19:41
  • @AndrewMonshizadeh Thanks for chiming in. I added a UIView to the scene and tried adding a child to that view instead of "self". E.g. blobView.addChild(Circle). Still getting the same error. Could you elaborate on what you mean by adding it to a scene? – user1186742 Jun 08 '15 at 19:47
  • 2
    @user1186742 you can add sprites to SKScene, you can't add them to UIView – Andrey Chernukha Jun 08 '15 at 19:58

0 Answers0