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{
public:
BackgroundManager(Scene* scene);
~BackgroundManager();
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) {
scene_=scene;
nebula =Sprite::createWithSpriteFrameName("nebula.png");
nebula->setPositionX(SCREEN_SIZE.width);
nebula->setPositionY( SCREEN_SIZE.height/2);
scene_->addChild(nebula,E_LAYER_BACKGROUND);
CallFunc* resetPosition= CallFunc::create([this](){
this->nebula->setPositionX(SCREEN_SIZE.width);
this->nebula->setPositionY( SCREEN_SIZE.height/2);
});
Sequence* seq = Sequence::create(
MoveBy::create(0.5,Vec2(-SCREEN_SIZE.width,0)),
resetPosition,
DelayTime::create(0.5),
nullptr);
nebula->runAction(RepeatForever::create(seq));
}
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->setPositionX(SCREEN_SIZE.width);
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?
Thanks!