Transition Scene and many headers and .cpp files

Hi.
I’m working in a project that must be organized in many headers and .cpp files (one for each class) and, with it I got some doubts because the way some functions on Cocos were explained in official documentation didn’t work on separated files.

I spent much time with Scene Transitions and I cound’t find a solution for it.
I have 2 scenes in 2 different headers and I want to make a trasition between them. I’ve already tried the proper transition methods but they change to next scene without exhib the first one and I’ve already tried transition with a button but I always have to get the specific name of the current scene and, in the end, I coudn’t do it.

How I can do it correctly?
Do you wonder another problem I can find organizing my code that way? (one header and .cpp for each class)
I’m using Visual Studio on Windows 10.

Hi define_bela!
I don’t understand your problem yet. You have problems with transitioning scenes and with code organization.

“the way some functions on Cocos were explained in the official documentation didn’t work on separated files”, I don’t know what you mean, Any example? remember we have methods, not functions in c++. What do you mean with separated files?

“I have 2 scenes in 2 different headers and I want to make a transition between them”, by headers you mean classes? I mean, a header is just a class definition, so you are inheriting from a cocos2d::Scene, right?

“I’ve already tried the proper transition methods but they change to next scene without exhib the first one”, I don’t understand what you are trying to achieve here. Do you have any code to show us?

“Do you wonder another problem I can find organizing my code that way? (one header and .cpp for each class)” Basically, in c++ every class is composed of a .h and a .cpp

Sorry if I don’t understand your problem if you can show us a simplified version of your problem(with code), it would be great.

Yes good idea. @define_bela can you show us what you are doing? I don’t have any problems transitioning scenes. Are you trying run more than one scene at a time? This currently isn’t an option.

Sorry if I’m confusing, I’ll try to explain it better.
I’m having trouble with Scene Transitions.I have 2 scenes: HelloWorldscene and Testscene and I want to make a transition between them. I create them in appDelegate

    auto scene = HelloWorld::createScene();
    director->runWithScene(scene);
    	
    //transition test
    auto scene2 = Test::createScene();
    director->replaceScene(scene2);

but when I run the program, the first scene (HelloWorld) isn’t displayed, just the second one. I realized that must to be a delay between them or the second scene must to be called by a method.
I couldn’t found a way to put a delay between them so, I tried to use a button. I want to modify the default close button (method code below) to do the scene transition when it’s clicked

void HelloWorld::menuCloseCallback(Ref* pSender)
{
	Director::getInstance()->end();
} 

but I couldn’t do it because all the transition fuctions use as argument the current scene [image] and I don’t know how to get it.

RunWithScene needs the first scene of the game. the later needs a new scene, not the actual one. Answering the main question i would say that you are not seeing the HelloWorld because you are inmediatly transitioning to the next scene. You load it into memory and inmediatly you transition to the next one. I would use the runWithScene in the appDelegate, and then use replaceScene in a Scene class, HelloWorld for example. basically you are not seeing HelloWorld because you are changing it inmediatly.

I would, as i said, create the HelloWorld scene in the appDelegate, then in HelloWorld i would create the button you mentioned, and inside the event of the button i would call Director->ReplaceScene(TestScene);

If you wanna try to use the menuCloseCallback in HelloWorld (in your code), it wont work because by the time you wanna press the button, the scene is no more there, TestScene is running.

That is my best bet, i have 4 days with cocos :stuck_out_tongue: lol

1 Like

You are running and replacing. This can be really fast. Just put a small pause in the first scene to ensure it stays up for your testing.

1 Like

I got it, but how can I do it?

It runs!
Instead of creating the new scene in appDelegate, I created it on HelloWolrd.cpp by a button, just the way you told me.

void HelloWorld::changeScene(Ref* pSender)
{
	auto scene2 = Test::createScene();
	Director::getInstance()->replaceScene(scene2);
} 

Just 4 days but you’re able to help me. Thanks :grinning:

1 Like
#include <thread>
 
std::this_thread::sleep_for(2s);
1 Like