0
votes

I am trying to subclass CCSprite but for some reason I can't seem to get anything to draw on the screen. I am not a pro with cocos2d so I am open to any suggestions. Here is my code. I have a scene like so:

#import "cocos2d.h"

@class Animation;

@interface HelloWorldLayer : CCLayer
{

}

+(CCScene *) scene;

@end

#import "HelloWorldLayer.h"
#import "Animation.h"
@implementation HelloWorldLayer

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    HelloWorldLayer *layer = [HelloWorldLayer node];
    [scene addChild: layer];
    return scene;
}


-(id) init
{
    if( (self=[super init])) {

        CCLOG(@"super init");

        HelloWorldLayer *animation;
        animation = [Animation animation:self];

 }
    return self;
}

- (void) dealloc
{
    [super dealloc];
}
@end

Then I subclass CCSprite like so:

#import <Foundation/Foundation.h>
#import "cocos2d.h"
@class HelloWorldLayer;

@interface Animation : CCSprite {

    HelloWorldLayer     *ivHelloWorld;
}
+ (id) animation:(HelloWorldLayer*) helloWorld;
- (id) initAnimation: (HelloWorldLayer*) helloWorld;
@end

#import "Animation.h"
#import "HelloWorldLayer.h"


@implementation Animation

+ (id) animation:(HelloWorldLayer *) helloWorld{
    CCLOG(@"animation method");
    return [[[self alloc] initAnimation:helloWorld] autorelease];

}

- (id) initAnimation:(HelloWorldLayer *)helloworld {


     CCLOG(@"InitaAnimation method");
    // cache
    CCSpriteFrameCache *cache=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache addSpriteFramesWithFile:@"FootballAtlas.plist"];

    // frame array
    NSMutableArray *framesArray=[NSMutableArray array];
    for (int i=1; i<60; i++) {
        NSString *frameName=[NSString stringWithFormat:@"Football%d.png", i];
        id frameObject=[cache spriteFrameByName:frameName];
        [framesArray addObject:frameObject];
    }
    CCLOG(@" array count = %i", [framesArray count]);
    // animation object
    id animObject=[CCAnimation animationWithFrames:framesArray delay:0.01];

    // animation action
    id animAction=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO];
    CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animAction];

    id animAction2=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO];
    animAction2=[CCRepeatForever actionWithAction:animAction2];

    // sprite
    CCSprite *bird=[CCSprite spriteWithSpriteFrameName:@"Football0.png"];
    bird.position=ccp(60,160);

    CCSprite *bird2=[CCSprite spriteWithSpriteFrameName:@"Football0.png"];
    bird2.position=ccp(80,160);


    // batchNode
    CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:@"FootballAtlas.png"];
    [self addChild:batchNode];
    [batchNode addChild:bird];
    [batchNode addChild:bird2];

    [bird runAction:repeat];
    [bird2 runAction:animAction2];

    return self;
}
@end

this does not draw anything on the screen, but if I move the animation code to the CClayer then everything shows up fine. What am I doing wrong?

2
can you try replacing '[self alloc]' with '[Animation alloc]' in '+ (id) animation:HelloWorldLayer' implementation ? Also, I don't see the reason to pass a HelloWorldLayer insance on initAnimation - yannicuLar
I figured it out thanks. I was creating a HelloWorldLayer object where I needed to have an animation object. - Stephen

2 Answers

1
votes

you just forgot to initialize your Animation sprite:

- (id) initAnimation:(HelloWorldLayer *)helloworld {
   if ((self = [super init]))
   {
      // PUT YOUR CODE HERE
   }
   return self;
}

And be careful into your HelloWorldLayer because you don't add the animation as child (and the variable is not typed correctly). Change that if you want see something in your scene. :)

0
votes

I figured it out thanks. I was initializing a HelloWorldLayer object where I needed to have an animation object.