Acces to class object inside CallFunc

Acces to class object inside CallFunc
0.0 0


Hello guys, I have some problems animating a sprite using CallFunc. I will try to reduce my code to focus the problem:

I have a class that contains the sprite nebula I want to animate:

class BackgroundManager{
  BackgroundManager(Scene* scene);

  Sprite* nebula;
  Scene* scene;


In the Constructor, the sprite is created and added to the scene, and then do some animations:

BackgroundManager::BackgroundManager(Scene* scene) {


	nebula =Sprite::createWithSpriteFrameName("nebula.png");

	nebula->setPositionY( SCREEN_SIZE.height/2);


	CallFunc* resetPosition= CallFunc::create([this](){
		this->nebula->setPositionY( SCREEN_SIZE.height/2);
	Sequence* seq = Sequence::create(



The CallFunc function resetPosition can’t acces to the nebula sprite, (not event log the position) and the app crash when the sequence reach this function.

Conversely, if the sprite isn’t a member of the class BackgroundManager, and is created inside the Constructor:

Sprite* nebula = Sprite::createWithSpriteFrameName(“nebula.png”);

modifying the Callback properly to include this guy:

CallFunc* resetPosition= CallFunc::create([this, nebula](){
	nebula->setPositionY( SCREEN_SIZE.height/2);

all goes alright.

The problem is that if I want to do some other stuff with the sprite, I need to include it as an object in the class, so the last solution is not what I’m looking for.

Can you help me finding a way to pass the sprite inside the callFunc, being the sprite a class object? Am I doing wrong the pass of [this] in the CallBack to refer an instance of the class itself?



I dont see you using retain() or anything.


thanks for reply :grinning:
retaining the Sprite and/or the CallFunc continues crashing the app when the sequence reach the CallFunc.
By the way, isn’t
increase the retain count of the Sprite and avoid PoolManager deleting it?


Ok, I don’t retain the class itself, in the sense that I was calling it (in some scene) by :
And so, there was no memory allocated in the heap to save anything inside the class. Doing:
BackgroundManager* bm= new BackgroundManager(this);
Solve the problem.

Thanks for you help :slight_smile: !


FYI: you could also use CallFuncN. It passes in the Node* where the action is running.

Your solution works just fine, and is basically a close equivalent slightly less expressive version.

CallFuncN::create([](Node* nebula) {
    // cast to (Sprite*) or other if you need to call derived members.