0

I have a question about used (or Apple recommended) approach. When I need to communicate between SKScene sprites and it`s view controller, is it ok to have SKScene property (inside view controller) and change behavior of sprites belonging to this scene in this SKScene property? Or is there another way for communication between view controller and scene (but I need to pass information TO scene FROM view controller, for passing info from scene to view controller I use NSNotifications). Thank you.

Baerdon
  • 145
  • 8
  • 2
    it is okay but I would recommend to have the view controller notify the scene which then does the necessary work, for instance vc calls [scene userDidSomethingSpecial] and the scene itself dispatches the work to its nodes. Also the scene property in the vc ought to be weak, otherwise presenting a new scene that doesn't set the vc scene property would keep the entire previous scene in memory. – CodeSmile Sep 02 '14 at 07:26
  • Thanks. But it is (for me) kind of complicated - when I touch scene to move figure, I have to let know view controller, make changes in model and send then via view controller back to scene to handle them (if I get it right) :). – Baerdon Sep 02 '14 at 07:39
  • No, not really. The classic Cocoa MVC model does not apply here. Normally the role of the View Controller in Sprite Kit doesn't go further than providing a way to manage the Sprite Kit view amongst other views. If SKView is your only view, then the scene takes the part of the controller, model and view in unison. You can (and should) still separate data from logic, distribute responsibilities to other nodes, etc. You can also have a model in the view controller limited to only persistent state (ie game progress, settings). – CodeSmile Sep 02 '14 at 08:38
  • Also the view controller can manage the global game state and transitioning between other scenes. But it's not rare to find no other code in the VC but to setup the SKView and present the initial SKScene. – CodeSmile Sep 02 '14 at 08:40
  • Have a look at: http://stackoverflow.com/a/22555881/2043580 – ZeMoon Sep 02 '14 at 09:27
  • In order to pass messages from the viewcontroller to the SKScene, add the relevant methods in the SKScene's header file, and call them on the scene object in the viewcontroller – ZeMoon Sep 02 '14 at 09:28
  • So I can put all game mechanism (like playing board, figures and so on) right inside SKScene (except few things belonging to view controller) and all will be ok? – Baerdon Sep 02 '14 at 09:39

0 Answers0