0
votes

I'm trying to add animations to my game (iPhone app, using cocos2d).

The game was written in C++, and now I want to run it on iPhone, so most of the classes are in c++.

The thing looks like that. I'm creating CCSprite,CCAction in obj-c class in init function, and then run CCAction on sprite. And animation is working.

But I want to put this CCSprite and CCAction variables in my C++ class. I create *CCSprite in init class and send this pointer to the c++ class. Then, I create CCAction and run it on the sprite.

And after that, when in my init function (obj-c class) do:

return self;

then the app is running, running and running and nothing happens. I receive only this message in console:

* Assertion failure in -[CCSprite setTexture:], /Users/Michal/..../libs/cocos2d/CCSprite.m:898 Terminating in response to SpringBoard's termination.

I dont know what should I do next... Is it possible to keep CCSprite/Action etc. in C++ class succesfully?

1
Look at the setTexture method and look for the NSAssert(). It will give you some clues to find what the problem is.Francescu

1 Answers

3
votes

Make sure you have properly initialized the texture before you try to use it like this:

CCTexture2D *redButtonNormal = [[CCTextureCache sharedTextureCache] addImage:@"RedButtonNormal.png"];

then initalize the sprite (or sprite subclass - in this example spuButton is a subclass of CCSprite) :

spuButton *redButton = [spuButton spuButtonWithTexture:redButtonNormal];

Note: since it is a subclass i had to setup the init methods for it like this (You must do this if you subclass CCSprite):

+ (id)spuButtonWithTexture:(CCTexture2D *)normalTexture
{
    return [[[self alloc] initWithTexture:normalTexture] autorelease];
}

- (id)initWithTexture:(CCTexture2D *)aTexture
{
    if ((self = [super initWithTexture:aTexture]) ) {

        state = kButtonStateNotPressed;
    }

    return self;
}