How to get variables of other classes in function Callback

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:

Callback:

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

This is calling the main level with the isTouched member:

void MainLevel::moveCallback () {
    if(!this->isTouched){
        this->stopAllActions();
    }
}

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.

thanks!

And why did I recieve pointer errors when I call:

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

    if(!scene->isTouched)){
...

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:
Setter:

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

Getter:

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

May it helps someone else!
Greetings Mat

hi, are you resolved the first problem?

Hi,

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.

Thanks!
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)),
        NULL);

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.

Hi,

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.

[edit:
Something like:

dynamic_cast(CCDirector::sharedDirector()->getRunningScene()->getChildren()->lastObject())

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.

Thanks!

Have you tried:

dynamic_cast(CCDirector::sharedDirector()->getRunningScene()->getChildren()->objectAtIndex(0))

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*/);

then

dynamic_cast(CCDirector::sharedDirector()->getRunningScene()->getChildByTag(kTagMainLevel))

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));
test->someMemberFunction();

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.