UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Project26 from 100 Days of Swift (UIKit) error - Cannot find 'addChild' in scope

Forums > 100 Days of Swift

I'm working on project 26 and got it all working but then I decided to move all the code that dealt with creating a level into its own struct.

Now, I get a compiler error on all the 'load' methods when calling addChild(node). I've searched around but wasn't able to find anything that explained this or how to fix it. I even tried chatGPT but it was no help.

What am I doing wrong?

Here is a gist of the struct

3      

@twostraws  Site AdminHWS+

The calls to addChild() should be inside your game scene, because that's where the method exists. Structs generally work best when they store simple, inert data, rather than trying to take action in your game.

3      

Thanks. I ended up removing the addChild() calls in the struct and then just created the nodes in my game scene by looping through an array created in the struct.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.