Reputation: 679
I'm creating a level-based game using two SKSpriteNodes as buttons to go to either the next or previous level which will cause the GameViewController to present a new scene. I made the SKScene to have a property that directly references the gameviewController in order to call a present new scene function, but from what I understand, that breaks the MVC model. Is this bad? What is an alternative?
Upvotes: 1
Views: 376
Reputation: 6635
The standard alternative for iOS programming is a pattern called delegation. First, you create a protocol to abstract away the implementation of switching levels.
protocol LevelSelector: class {
func didSelectNextLevel(from scene: SKScene)
func didSelectPreviousLevel(from scene: SKScene)
}
The GameViewController
implements this protocol to change to the scene for the appropriate level. Each scene implementing a level has:
weak var levelSelector: LevelSelector?
The GameViewController
then sets itself as the levelSelector
for each level scene:
scene.levelSelector = self
When the user taps on the SKNode
s representing the next and previous level buttons, call the appropriate method:
levelSelector?.didSelectNextLevel(from: self)
//OR
levelSelector?.didSelectPreviousLevel(from: self)
Upvotes: 7