0
votes

EDIT: I got it: I had the wrong initialization process in my subclass. In other words, this method works as described, but if your subclass initialization is not calling the super classes initialization methods properly, you end up with XCode going all confused and blabby.

Case closed.


Thanks for reading. I have an issue where a subclassed CCSprite cannot be type-cast back properly once it's been handed through a Box2D body's userData.

During the update() cycle of the main CCLayer, I iterate through all Box2D objects to update the Cocos2D sprite positions. I store a Cocos2D sprite in the box2D body definition by setting

body->SetUserData((__bridge void*) newSprite);

In the update cycle I retrieve that Sprite by doing this:

CCSprite* thisSprite = (__bridge CCSprite *) body->GetUserData();

That worked nicely in my project so far. But today, I subclassed CCSprite to MySprite, and upon retrieval:

MySprite *sprite = (__bridge MySprite*)body->GetUserData();
if ([sprite isMemberOfClass:[MySprite class]]) {
    CCLOG(@"It's a MySprite.");
} else if ([sprite isMemberOfClass:[CCSprite class] ]) {
    CCLOG(@"It's a CCSprite.");
}
My_SpriteStuff* st = [sprite getMySpriteStuff];

...it turns out that, while the debugger claims that MySprite is indeed a MySprite (it even shows its properties), the Log gets the "It's a CCSprite" written. The code lines that follow otherwise work fine (on the premise that mysprite is a CCSprite) and can access the properties and methods of CCSprite - but not of MySprite.

The code crashes on that last line because it can't access the "getMySpriteStuff" message: Here's an excerpt from the Log:

"It's a CCSprite.
2012-03-29 16:53:27.145 OME-iOS[2290:1c403] -[CCSprite getMySpriteStuff]: unrecognized selector sent to instance 0x10de3930
2012-03-29 16:53:27.153 OME-iOS[2290:1c403] ERROR: Uncaught exception -[CCSprite getMySpriteStuff]: unrecognized selector sent to instance 0x10de3930"

Anyone have an idea what I'm doing wrong here? This code worked until I started subclassing CCSprite... in other words, CCSprite gets put in and retrieved through the Box2D UserData, but my own classes don't?! It's gotta be something obvious. Any help would be greatly appreciated.

1

1 Answers

0
votes

I got it: I had the wrong initialization process in my subclass. In other words, this method works as described, but if your subclass initialization is not calling the super classes initialization methods properly, you end up with XCode going all confused and blabby.