How to get variables of other classes in function Callback

How to get variables of other classes in function Callback
0.0 0


Hi everyone,

I have to classes: the main level and a player class. In the main level class theres a class member called bool isTouched and I got a callback in the player class called after every animation is finished, which calcs if the screen is tocuhed yet. When i got everything in one class, it works fine, but splitted up it looks like some serious business and I cant find the right way. Heres my code:


CCSequence* seq = CCSequence::create(CCAnimate::create(animation3),
        CCCallFunc::create(this->player, callfunc_selector(MainLevel::moveCallback)),

This is calling the main level with the isTouched member:

void MainLevel::moveCallback () {

How can I solve this problem? I’ve tried to assign the current scene, but than I couldnt get to the member variables of “MainLevel”. Even if I tried to put the isTouched in the Player class, I couldnt read them, because the player class is no cocos2dx class.



And why did I recieve pointer errors when I call:

    MainLevel* scene = dynamic_cast(CCDirector::sharedDirector()->getRunningScene());



Mhhh no one? The first issue is still a active question.

Belonging the second one, I found a workaround. Cause bool pointers didn’t work, I found something that did:

CCDirector::sharedDirector()->getRunningScene()->setUserData((void *) 1);


if(CCDirector::sharedDirector()->getRunningScene()->getUserData() == (void *)0)

May it helps someone else!
Greetings Mat


hi, are you resolved the first problem?



as I said, the first problem is still not solved. How can I access not only the current running scene via the CCDirector, but can get access to the custom vars of my running MainLevel class.

Greetings Mat


Let’s have a look at the callback code:

CCSequence* seq = CCSequence::create(CCAnimate::create(animation3),
        CCCallFunc::create(this->player, callfunc_selector(MainLevel::moveCallback)),

I wonder what this->player is. The create(CCObject *, SEL_CallFunc) method of CCCallFunc takes the first argument as the this of the selector, which is MainLevel::moveCallback. Therefore you should pass an instance of MainLevel to it.

Furthermore, if your created the scene (that was passed to CCDirector) in the same way as in HelloWorld example, your MainLevel object would be the first child of CCDirector::sharedDirector()->getRunningScene(), not the scene itself.

You may also want to consider using CCCallFuncN, CCCallFuncO, etc.



thanks for the answer! this->player is a CCSprite* that points to the player Sprite of the MainLevel.

I wanted to pass an instance, but I didn’t now how. Didn’t noticed that I added the MainLevel instance as a child, thanks! But how did I get it? When I browse the functions, the only yet implemented one I found is [CCArray]->lastObject(), so I guess I have to get the first child manually.

Something like:


Didn’t work. Do I have to cast in general? ]

Besides I get an error, when I link the MainLevel class to the Player class:

classes\game\mainLevel.h(40): error C2143: Syntaxerror: Missing ';' before '*'

I’ve checked the code as well as the last ;, also the level class itself works fine, but if I include it to another class (like the player one), I get the error above.



Have you tried:


Or even better, when adding the MainLevel object as a child of the scene, give it a tag.

scene->addChild(yourMainLevelObject, 0 /*zOrder*/, kTagMainLevel /*some unique value*/);



The error you have encountered may be the result of something called mutual inclusion, when two header files include each other. Try not to include MainLevel in Player. Use a forward declaration instead:

class MainLevel; //usually this is enough


The inclusion now works well, thanks a lot :slight_smile:

But if i trie to access some member functions like:

MainLevel test = dynamic_cast(CCDirector::sharedDirector()->getRunningScene()->getChildByTag(kTagMainLevel));

And I trie to access this in this function, I still get an pointer error :confused: Screenshot below!

[edit: Found a workaround I tried before, but that didn’t worked because of the class inclusion:
Added a setLevel Method to the Player class, than call playerClass->setLevel(this) in the inilization of the Level.]


I see. It looks like the running scene is not the scene you put your MainLevel object in. Perhaps, at the time you access the object, the running scene has been changed. I don’t know the flow used in your game. Maybe you should take a deeper look.