I’m making an iphone app. All you need to know is that there is a main class called ArcherClass, and there is a class called Player.
in the Player class, this is how I set up different body parts (I created a stick figure, and parts of his body move seperately so I created a class just for the stick figure).
Player.h______
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface Player : CCLayer {
CGPoint position;
float angle;
}
@property (nonatomic, assign) CGPoint *position;
@property (nonatomic, assign) CGPoint *angle;
+(id)player;
+(id)bow;
+(id)setHead;
+(id)setBody;
+(id)setLeftArm1;
+(id)setLeftArm2;
+(id)setRightArm1;
+(id)setRightArm2;
-(void)setAngles:(float)angle;
@end
This is the entire header file, later I plan on implementing all this inside the .m file.
But just so that I could give a general idea of what I’m asking, here is part of the unfinished .m file:
Player.m________
#import "Player.h"
@implementation Player
@synthesize position = _position;
@synthesize angle = _angle;
+(id)player{
Player *playerSprite = nil;
return playerSprite;
}
+(id)arm{
Player *playerSprite = nil;
playerSprite = [CCSprite spriteWithFile:@"arm.png"];
return playerSprite;
I plan on making all the Player’s individual body parts this way.
Here is the main class’s implementation:
ArcherClass.m______
//Skipped alot of stuff. Not the point.
Player *person = nil;
CCSprite *bob = [Player arm];
bob.position = ccp(160,160);
[self addChild:bob z:3];
//above shows how I access the Person Class and use the function arm
I feel like I am doing this wrong. Is the way I’ve done this bad practice? Is there a better way of accessing a sprite in a different class and using it? Any feedback would be great, thanls.
From an OO perspective, that doesn’t make sense.
This assumes ARC — which you should be using for any new projects — and, thus, the use of
strongvs.retain(and no need for a-deallocmethod).Typically, you would do something like:
And then:
You would then create an instance of the Player. That instance would encapsulate all the sprites that compose the player, controlling position, etc. You would then write method(s) such that you can move the player. When the player moves, it should internally move all the parts to the appropriate spot.