spritebuilder / SpriteBuilder

Objective-C Game Development Suite
http://www.spritebuilder.com/
Other
742 stars 293 forks source link

Code connections selector/ivar target could use a "parent root" option #418

Open LearnCocos2D opened 10 years ago

LearnCocos2D commented 10 years ago

My problem: I have a "layer" CCB with a button on it. The button's target is set to "document root", which refers to the CCB's root node. However I include that CCB as sub file respectively via CCScrollView contentNode in the "MainScene" CCB.

I don't want to be forced to create a custom subclass for the "layer" CCB with the button on it. Instead I'd like to set the CCB's target to that of the "parent owner", meaning the MainScene CCB file.

This would be most useful to use sub file nodes respectively CCScrollView to design the various states of a menu while implementing all of the button's selectors in the MainScene node class. Especially considering menus where most of the button's functionality will be the same, but their layout or visual appearance should change.


I imagine a "first match" target option would also work, either in addition to the above or as the solution for it.

Quite simply the "first match" target would walk the scene graph recursively and find the first node that implements the given selector, and from then on uses that node as the selector's target. Nodes that do not have their "Custom class" property set can probably be ignored in the search.

This setup will have to be performed post-load to ensure the scene graph is completely set up. Perhaps even on first use of the selector.

haligalaph commented 10 years ago

Face the same problem many times. Here is my solution, I used for CocosBuilder. Unfortunately, I had to rewrite some CocosBuilder and CCBReader code. Instead of one owner object, I pass dictionary into CCBReader loading method. And instead of 'owner' option, I have a text field, where I can set a key representing special object in this dictionary. It is much more flexible. For example, I have social submenu for facebook and twitter posting, that presented in several scenes. In each scene's owner class I have to add postFacebook and postTwitter methods. But if I use dictionary instead of one owner object, I will have only one social manager. It helps to divide scene's functionality into different controllers instead of one big owner. Also dictionary could be used for some code binding. For example in the SpriteBuilder I type "$title" into label's string field. And then I pass { "title" : "This is a title!" } dictionary when loading ccb-file.

LearnCocos2D commented 10 years ago

Same issue exists for "Callbacks" timeline keyframes. For instance "document root" in a "Sprite" CCB refers to the sprite, while "owner" apparently does nothing (perhaps internally owner is nil, no idea - it doesn't even assert when using a non-existing selector).

cocojoe commented 9 years ago

Similar talks have come up in forums, it would be beneficial to have the option to set 'owner' in a CCB to be the parent. (Generally the Scene that's just been dragged into).

oxeron commented 9 years ago

No idea when this will be available ?