0

I have a question about creating custom init methods in my Player class that inherits from CCNode. Not sure if I need to instead take a different route and create class methods instead:

+ (id) addSprite: (CCSprite*) sprite andSpriteBatchNode: (CCSpriteBatchNode*) spriteBatch;
more class methods ....

In Player class, I have a designated init method, along a few convenience init methods.
- (id) init;
- (id) initWithSpriteBatchNode: (CCSpriteBatchNode*) spriteBatch;
- (id) initWithSprite: (CCSprite*) sprite andSpriteBatchNode: (CCSpriteBatchNode*) spriteBatch; //designated init method

When I instantiate this class in my Level class I am not able to call any of my custom init methods directly. What I have to do is:

Player *player = [Player node];
[player initWithSprite: sprite andSpriteBatchNode: spriteBatch;]; //not sure if this is even correct or will it leak memory

I think either should work since they are doing the same thing just on at instance level, the other methods at name.

Please advise.

badboy11
  • 541
  • 1
  • 6
  • 19

2 Answers2

0

The problem here is that you Player inherits from CCNode which doesn't provide functionality for sprites or batch nodes simply because a CCNode is just a logical node.

If you, instead, inherit from a CCSprite you can do easily what you need:

@interface CCPlayer : CCSprite
{

}

- (id) initWithSpriteBatchNode: (CCSpriteBatchNode*) spriteBatch;

@end

@implementation CCPlayer

- (id) initWithSpriteBatchNode: (CCSpriteBatchNode*) spriteBatch
{
  if ((self = [super initWithSpriteBatchNode:spriteBatch]))
  {
    // custom code
  }

  return self;
}

@end

Now you will be able to do

CCPlayer *player = [[[CCPlayer alloc] initWithBatchNode:batchNode] autorelease];
[somewhere addChild:player];

Of course this works if your player sprite will be logically a single sprite, if you have, let's say more pieces then you will need to use composition:

@interface CCPlayer : CCNode
{
  CCSprite *body
}

-(id)init;

@end

@implementation CCPlayer

-(id)init
{
  if ((self = [super init]))
  {
    body = [CCSprite spriteWith:...;
    [self addChild:body];
  }

  return self;
}
Jack
  • 131,802
  • 30
  • 241
  • 343
  • Looking thru your code cleared my confusion, over thinking and confusing myself. I am inheriting from CCNode for a couple different reasons. 1) I only want to inherit what I need(keep objs light) and 2) I will be switching sprites, so composition is my method of choice for using CCSprite. I think I may have confused myself earlier. `Player *p = [Player node];` does both alloc/init, so I can do something like `Player *p = [[[Player alloc] init..] autorelease];`. Instance methods and class methods should not be an issue. Class methods are convenience methods. Thank you, appreciate your help. – badboy11 Mar 14 '13 at 23:58
0

I was over thinking and confusing myself. Cocos2D has a class method e.g.: [CCNode node] method.

I can have as many instance and class methods to create instances of my custom classes. They are just convenience methods.

Instead of using the default inherited method:
Player *player = [Player node]; => basically does alloc/init

I could use one of my custom init methods:
Player *player = [[[Player alloc] initWithSpriteBatchNode: spriteBatch] autorelease];
[spriteBatch addChild: playerSprite];
or something....

badboy11
  • 541
  • 1
  • 6
  • 19