Noob in XCode needing help: Inheritance and Polymorphism - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Noob in XCode needing help: Inheritance and Polymorphism (/thread-10834.html)
Noob in XCode needing help: Inheritance and Polymorphism - HarpoonStudios - Nov 26, 2013 06:09 PM
Hey! So I'm sorry to ask such a basic question (and mods, if this is in the wrong area, please move it), but I typically code in Java and am having some issues transitioning to XCode. One question I have is this:
So I'm developing a game involving sprites, but I want there to be many types of sprites that each have their own AI, private variables (porperties?), arraylists (NSMutableArrays?), etc, but don't know what class to extend or how to extend it. For instance, if I want a SKSpriteNode that is a pirate and one that is a fish, how do I set up the code such that I can initialize them as separate children?
Thanks for your help! Sorry this is so basic.
RE: Noob in XCode needing help: Inheritance and Polymorphism - funkboy - Nov 27, 2013 05:25 PM
I'm not the best person to answer, but maybe I can kick off a couple more responses.
In general, the proper pattern to follow here in Objective-C would be to have a class of GameObject that you can subclass to be specific types of objects, such as a fish or pirate.
You can have GameObject be a subclass of SKSpriteNode, and then add custom properties to it that work well with your game logic.
I'd recommend putting some kind of prefix before your class name, so perhaps HSGameSpriteNode, or HSSpriteNode, implying that you've taken an SKSpriteNode but added some HarpoonStudios-specific code to it, to help with whatever you need to do in the game.