0

Here is the problem :( While I am working with Spritekit nodes I need some settings to be accessible inside SKSpriteNode, SKLabelNode classes etc... Settings are stored in pretty basic types (string, array, floats).SpriteNode have property "name" but that is not enough for me, I need some array field instead because I have few more settings needed...

Is there a way , somehow, to add those settings to let's say SKNode class so they can be a part of SKSPriteNode and SKLabelNode classes?

I dont want to make NSDictionary field as a member of MyScene (SKScene) class to store all settings in format key->value,because accessing them requires more unnecessary programming logic.

I just want to, create node with settings inside, read those settings directly from that node, and based on settings make an further action.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • You can use Categories https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html – Guilherme Jun 11 '14 at 20:21
  • @Guilherme you can't add properties with a category – ChrisH Jun 11 '14 at 20:24
  • Why not just subclass? – ChrisH Jun 11 '14 at 20:25
  • @ChrisH from what I understood, he'd like to use the same data that the classes already have, but wants to read "all at once". He could make a method that gathers all that information and spit it out in one single object. In the case he does need a new property, yes, Category is not the solution. – Guilherme Jun 11 '14 at 20:28
  • Tnx Guilherme, but I think ChrisH is right about adding properties with Categories...I need my own settings (settings property) within those classes... I asked for other solution because I think it's little "ugly" to subclass all those classes because of one or two fields. – Whirlwind Jun 11 '14 at 20:37
  • I found this: http://stackoverflow.com/questions/12252725/defining-a-property-in-ios-class-extension – Whirlwind Jun 11 '14 at 20:44
  • @ChrisH You can add properties to a category. What you can't do is add instance variables. – rmaddy Jun 11 '14 at 21:13

2 Answers2

0

Subclassing would be the preferred method.

There is something called the decorator pattern but with obj-c it would prob be a lot harder for you.

Rohan Panchal
  • 1,211
  • 1
  • 11
  • 28
0

If I'm reading your needs correctly, then it sounds like you just want to use the userdata property. Every SKNode has an NSDictionary property called userdata. You just have to initialize it and then add your data.

Cooper Buckingham
  • 2,503
  • 2
  • 15
  • 23