I’m a completely self-taught Objective-C programmer, and I need advice on how to best structure a project that I’m working on.
I’m dealing with CCSprite‘s, .tags, and some general program structure issues that I’m having trouble figuring out the best way to go about structuring the program.
As of right now, the program is running exactly how I want it to, but it seems like I’m wasting lines of code and being overly redundant. I want to save myself some trouble in the future and get everything done right up front.
I am essentially creating a fully-animatable rigged character out of CCSprites. I need each part of his body to animate independently of other body parts. To achieve this, I’ve created a method that I call in the -init method of my CCScene, which goes through and creates the CCSprites and parents them to the appropriate part. Here is the code for the method:
-(void) characterInit{
CGSize size = [[CCDirector sharedDirector] winSize];
//Torso
CCSprite * torso = [CCSprite spriteWithFile:@"Torso.png"];
torso.anchorPoint = ccp(.5, .5);
torso.position = ccp(size.width/2, size.height/2);
torso.scale = 1;
torso.tag = torsoTag;
[self addChild:torso];
//Head
CCSprite * head = [CCSprite spriteWithFile:@"Head.png"];
head.anchorPoint = ccp(.5, 0);
head.position = ccp(50,60);
head.tag = headTag,
[torso addChild:head z:1];
//Eyes
CCSprite * eyes = [CCSprite spriteWithFile:@"Eyes.png"];
eyes.anchorPoint = ccp(.5, .5);
eyes.position = ccp(35, 45);
eyes.color = ccc3(255, 255, 255);
eyes.tag = eyesTag;
[head addChild:eyes];
...
...
...
…this continues on for each body part.
The key here is that I am using just plain old ints with a number value for the .tag for each CCSprite. I declare the ints above this method, outside of any other methods so that they are accessible inside the init method as well.
Question 1: What would be a better way to do this? Maybe as an enum? What would that code look like, and is there a better way to generate that code as opposed to just writing out each value, like i did when I had them as ints?
Now my next question comes in after I’ve gone through and created all the CCSprites. When I want to use them outside of the method I created them in, I use code like this from my init method:
CCSprite * torso = (CCSprite *) [self getChildByTag:torsoTag];
CCSprite * head = (CCSprite *) [torso getChildByTag:headTag];
CCSprite * eyes = (CCSprite *) [head getChildByTag:eyesTag];
[eyes runAction:...];
I originally tried simply calling…
CCSprite * eyes = (CCSprite *) [self getChildByTag:eyesTag];
…but to no avail. I assumed that I would be able to call this individual body part with out having to call in every parent of that body part so that I can animate it. because…well…
That Sucks.
The point is this; I feel like I am just thinking about this incorrectly. I want to organize my code, but the whole point of organizing is so that you don’t have to be redundant.
Question 2: What would a better programmer than myself do to handle this whole freaking situation?
Thanks for your time guys, and sorry for the WALL OF TEXT.
You could forget the whole
tagbusiness and store the sprites as instance variables of your class.Your method would be a little simpler:
And you can use your body parts directly: