Pergunta

i have a subclassed CCNode to which I add several of the same small subclassed CCSprite WHICH IN TURN have several subclassed sprite children. I thought I was using CCSpriteBatchNode properly but I'm noticing hundreds of draw calls are registering and I thought this should only be ONE.

My CCNode subclass adds this child:

Segment* segment = [Segment segmentWithState:kState];
[self addChild:segment];

The segment is a subclass of CCSprite with:

+(id) segmentWithState:(SegmentState)segmentState { return [[self alloc] initWithState:segmentState];}

-(id) initWithState:(SegmentState)theSegmentState {

    segmentSpriteBatch = [CCSpriteBatchNode batchNodeWithFile:@"txt.png"];
    [self addChild:segmentSpriteBatch];
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"txt.plist"];

    self = [super initWithSpriteFrameName:theSegmentStateFrameName];
    if (self){
        Subsegment * subsegment = [Subsegment subsegmentWithState:kInvisible];
    } 
    return self;

Then this segment subclass gets a child of the subclass subsegment which has the same code:

+(id) subsegmentWithState:(SubsegmentState)subegmentState { return... ]

-(id) initWithState:(SubsegmentState)theSubsegmentState {
    subegmentSpriteBatch = [CCSpriteBatchNode batchNodeWithFile:@"txt.png"];
    [self addChild:subegmentSpriteBatch];
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"txt.plist"];

    self = [super initWithSpriteFrameName:theSubegmentStateFrameName];
    if (self){

    } 
    return self;

I tried adding one batch node to the CCNodeSUbclass and then adding an instance of a segment as a child to batchnode but this throws an error. I'm also not sure how i'd then add the children (subsegments) the the CCNodesubclass batchnode.

incidentally, is my use of classes rather than instance methods code smell that is coming to light with this?

Foi útil?

Solução

Each instance of segment creates a different batchNode from every other segment, and similarly every instance of subSegment creates a different batchNode from every other subsegment. Thus, you are not batching at all, explaining the large number of draw calls.

Maybe it is possible for you to initialize the batchnode outside of these two classes, and pass it along to the ctor's for both segment and subSegment. I would also seed the spriteframe cache only once with the .plist (as long as you do not remove spriteframes along the way).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top