Transition between scenes

When i need to transfer between my starter scene that does nothing only showing my logo to the main menu scene without touching the screen “automatic” to Main menu scene
Start.h:

#ifndef START_SCENE_H
#define START_SCENE_H

#include “cocos2d.h”

class StartScene : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();

virtual bool init();

// a selector callback
void gotoMainMenuScene();//cocos2d::Ref* pSender
    
// implement the "static create()" method manually
CREATE_FUNC(StartScene);

};

#endif // START_SCENE_H

Start.cpp:

#include “StartScene.h”
#include “MainMenuScene.h”

USING_NS_CC;

Scene* StartScene::createScene()
{
// ‘scene’ is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
auto layer = StartScene::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;

}

// on “init” you need to initialize your instance
bool StartScene::init()
{
if ( !Layer::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

Sprite *logo = Sprite::create( "logo.png");
logo->setAnchorPoint(Vec2(0.5, 0.5));
logo->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));

logo->setOpacity(0);
auto fadeIn = FadeIn::create(3);
logo->runAction(fadeIn);

this->addChild(logo);

//this->scheduleOnce(schedule_selector(StartScene::gotoMainMenuScene());

return true;

}

void StartScene::gotoMainMenuScene()
{
//CCLOG(“Test Called…”);
auto scene = MainMenuScene::createScene();
Director::getInstance()->replaceScene(TransitionFade::create(0.5, scene, Color3B(0,255,255)));

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}

Use scheduleOnce to schedule the function, or use a CallFunc action after a DelayTime action.

can u explain a bit in details cause im new to cocos . thanks

The scheduler allows you to have a function called after a certain amount of time, and do so once, a specified amount of times or forever until you change scene or tell it to stop. I can’t remember the exact way to use it (something like this->scheduleOnce(schedule_selector(HelloWorld::myFunction), but not sure off the top of my head. Look up schedule and scheduleOnce.

Actions usually allow you to manipulate nodes, but you can use them to wait for a certain amount of time and to call functions. Try searching for CallFunc and runAction.

1 Like

How to pause splash screen action for 3 seconds I guess this will help.

I have tried it, but it wont replace the first scene it ended the program for somehow >!!! this is my code:

bool StartScene::init()
{
if ( !Layer::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

Sprite *logo = Sprite::create( "logo.png");
logo->setAnchorPoint(Vec2(0.5, 0.5));
logo->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));

logo->setOpacity(0);
auto fadeIn = FadeIn::create(3);
logo->runAction(fadeIn);

this->addChild(logo);

this->scheduleOnce(schedule_selector(StartScene::gotoMainMenuScene), 3);

return true;

}

void StartScene::gotoMainMenuScene(float dt)
{
CCLOG(“Test Called…”);
auto scene = MainMenuScene::createScene();
Director::getInstance()->replaceScene(TransitionFade::create(0.5, scene, Color3B(0,255,255)));
}

Could you try logo->setAnchorPoint(Vec2(0.5f, 0.5f)); ?

Ive tried but not working. this scene showing but the one i need to transform to is not showing (main menu scene)

also replace it with this: TransitionFade::create(0.5f, scene, Color3B(0,255,255))

no not working

Please share your corresponding .cpp and .h files.

#ifndef START_SCENE_H
#define START_SCENE_H

#include “cocos2d.h”

class StartScene : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();

virtual bool init();

// a selector callback
void gotoMainMenuScene(float dt);//cocos2d::Ref* pSender
    
// implement the "static create()" method manually
CREATE_FUNC(StartScene);

};

#endif // START_SCENE_H

StartScene.cpp (1.3 KB)

“The page you requested doesn’t exist or is private.” StartScene.h is not accessible.

#ifndef START_SCENE_H
#define START_SCENE_H

#include “cocos2d.h”

class StartScene : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();

virtual bool init();

// a selector callback
void gotoMainMenuScene(float dt);//cocos2d::Ref* pSender
    
// implement the "static create()" method manually
CREATE_FUNC(StartScene);

};

#endif // START_SCENE_H

[quote=“Avante, post:15, topic:26651, full:true”]
ifndef START_SCENE_H
define START_SCENE_H
include “cocos2d.h”
endif // START_SCENE_H
[/quote]these must start with # symbol:

#ifndef START_SCENE_H
#define START_SCENE_H
#include "cocos2d.h"

…

#endif // START_SCENE_H

Yeah my version has # but when i copied it here removed for some how !! so still the same :frowning:

Do i need to do something with main menu scene the 1 i need to move to ?

Maybe something is wrong with main menu scene.

I run the main menu scene directly, it works