Hi there,
I’m having a problem since two days and I don’t know how to solve it.
In my first scene, I’m adding a Button “Play” and it has a special effect of movement:
RepeatForever *effectButton = RepeatForever::create(Sequence::createWithTwoActions(ScaleBy::create(1.05, 1.05), ScaleBy::create(0.95, 0.95)));
buttonPlay->runAction(effectButton);
scene->addChild(buttonPlay);
It works fine. But the problem is, when I touch the button, the scene is replaced, but when I return to the initial scene, the button does not have the effect of movement. The runAction does not work when I return to the scene. Which could be the problem?
I tried add this methods before or after replaceScene but it not solve the problem.
Director::getInstance()->getRunningScene()->getChildByName("buttonPlay")->stopAllActions();
Director::getInstance()->getActionManager()->removeAllActions();
Anyone can help me?
Thanks.
Not enough information.
Where is your first piece of code placed? In the init()?
How do you replace the scene? What do you mean by I return to the scene
?
Is buttonPlay
a variable member of the scene 1?
Theoretically you can try resumeSchedulerAndActions()
.
@brightInverse thanks for the answer.
I will respond your questions.
Where is your first piece of code placed? In the init()?
Nope. When I run the game, the first scene is executed ( director->runWithScene(scene); ). After that, I replaced that scene by another one, and that another scene is that have the play button.
How do you replace the scene? What do you mean by I return to the scene?
This is how I replace the first scene.
void Game::initScreen()
{
Scene *scene = Scene::create();
Director::getInstance()->replaceScene(scene);
.
. //Here I add others Childs...
.
cocos2d::ui::Button *buttonPlay = cocos2d::ui::Button::create("buttonPlay.png");
buttonPlay->setName("buttonPlay");
buttonPlay->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2));
buttonPlay->addTouchEventListener(CC_CALLBACK_1(EventHandler::onTouchButtonPlay, EventHandler::getInstance()));
RepeatForever *effectButton = RepeatForever::create(Sequence::createWithTwoActions(ScaleBy::create(1.05, 1.05), ScaleBy::create(0.95, 0.95))); /
buttonPlay->runAction(effectButton);
scene->addChild(buttonPlay);
}
When I say “return to the scene” I refer to call again initScreen() method in other part of the code.
The first time I call initScreen () the button action works fine. But when I replace the initScreen() scene (by other scene, in onTouchButtonPlay()) and in another part of the code I call initScreen () again to return to the init scene, the runAction button do not work.
Is buttonPlay a variable member of the scene 1?
Nope. As I mentioned before, the first scene does not have the button.
Theoretically you can try resumeSchedulerAndActions().
This method is deprecated. Other alternative?
Thanks.
I found my problem.
My Director was in pause mode.
Adding this line, I solved the problem:
Director::getInstance()->resume();
Thanks !!!
I did not do anything useful. But I am glad that you solved your problem.