CCNode inherited class

CCNode inherited class
0.0 0



I’m trying to create a level loader screen.
I would like to show a list of items, each item might have an image, level name, best time, #stars etc.
I would also like to be able to touch the item (effectively the whole row) to load that level.
I would also like to be able to scroll the screen.

I already have a Scene / CCLayer that would show the levels list and load / show the correct level (replaceScene).
I plan on scrolling this by just changing the layer position.

I was thinking of creating a new class inheriting from CCNode (lets say BBLevelListItem).
This class would have the level id / file names, and add the sprites / text etc to itself (this~~>addChild).
The positioning of these sprites / text would be relative to itself.
The LevelLoaderScreen would add an array of BBLevelListItem, passing the info to the BBLevelListItem.
this~~>addChild(new BBLevelListItem(blah)).

I would like BBLevelListItem to notify the LevelLoaderScreen that a level was clicked and pass the required info back.

I could loop round all BBLevelListItems (in onBeginTouch) and do a rectContainsPoint test, if a match occurs, get the info.
This seems an inelegant way to do it.

Is it possible to register the BBLevelListItem for touch and give it a function in LevelLoaderScreen to call passing the BBLevelListItems*

Another option I though of is to register each BBLevelListItem for targetedTouchDelegate and just replace the scene from that BBLevelListItem class. Is there any issue with this?




I believe that all the cocos2d touch code does is check every single “rectcontainspoint” until it finds one that absorbs the touch.
So doing that wouldn’t be so awful.

Maybe there are more elegant ways though.


Ok, assume that I’m just going to loop round my CCNode based objects and see which is touched.
I already do this on my game screen so no biggie.

I’m having another issue with this new class.
I want to store some strings in my class to hold the file names of the level files for this level.
When I find the node I touched, I’ll get the file names from it, push it to my singleton class (GameManager) and call replaceScene (new scene reads from GameManager).
I keep getting a crash when storing these strings in my class.
Setting string does not cause an issue (as I can step through ok), its when I continue after that code.
The error that appears in xcode if EXC_BAD_ACCESS and the code appears to be assembly, I’m not sure where.

I tried using a CCString, char* and std:string but I get the same result in all cases.
What am I doing wrong?

Here is my .h


#include "cocos2d.h"

using namespace std;

class LevelMenuItem : public cocos2d::CCNode

    virtual ~LevelMenuItem();

    int itemId;
    time_t bestTime;
    int bestScore;

    void SetImage(string image);
    void SetText(string text);
    void SetFilenames(string main);

    char* mainFileName;  //CCString also fails


My .cpp

#include "LevelMenuItem.h"

using namespace cocos2d;
using namespace std;


void LevelMenuItem::SetImage(string image)    //THIS WORKS OK
    CCSprite* pSprite = CCSprite::create("Icon-72.png");
    this->addChild(pSprite, 0);

void LevelMenuItem::SetText(string text)      //THIS WORKS OK
    CCLabelTTF* pLabel = CCLabelTTF::create(text.c_str(), "Thonburi", 18);
    pLabel->setPosition(ccp(50, 0));
    this->addChild(pLabel, 0);

void LevelMenuItem::SetFilenames(string main)   
    sprintf(mainFileName, "%s", main.c_str()); //THIS CAUSES A CRASH
//  OR
    mainFileName = main.c_str();               //THIS CAUSES A CRASH


My code that creates the items

    for(int i = 0; i < 5; i++)
        LevelMenuItem* item = (LevelMenuItem*)LevelMenuItem::create();

        char message[100];
        sprintf(message, "Level %i", i);

        item->itemId = i;
        item->SetFilenames("levels/level001.plhs");   //IT WORKS IF I DONT CALL THIS LINE

        item->setPosition(ccp(size.width * 0.05, size.height * 0.9 -(50*i)));


Am I missing something from my class?

Any reason I would get an error when setting a char* std:string or CCString in my class?


Try using const char* instead of just char*

You can also change the type of mainFileName to std::string


I had const char* and string before but both were causing the same issue.

Would it be best to have a public const char* and set it directly from the calling class or make it private and create a setFilename function and call that?

Do I need to do anything extra as I’m using CCNode as the base?


It seems that even if I try and store an int in the CCNode derived class I get some kind of error.

This time, when I close the app I get a heap error.

I changed my int to a CCinteger* as I assume that is an autorelease obejct but I get the same issue.

Can anyone help?


what do you mean storing an integer?
Can i see the code where that happens?


Dont worry,

I changed the class to use the ::create type code that is in classes like CCLabelTTF and its working now.

LevelMenuItem * pRet = new LevelMenuItem();
if (pRet && pRet~~>init)
return pRet;


okay, great.
I was going to recommend you did that when i saw you say about (new BBLevelListItem(blah)) in the first post.