How To Create Android Cocos2dx Splash Screen?
This is my code I don't know how to create splash screen and how it will direct in my menu screen. All .h must be connected to BaseScreen and the BaseScreen will be the one connect
Solution 1:
Here are three steps to add a splash screen scene on android. Using cocos2d-x 3.4. Hope this helps!
1 Create a SplashScene.cpp
#include "SplashScene.h"#include "HomeScene.h"
using namespacecocos2d;
cocos2d::Scene* SplashScene::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = SplashScene::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scenereturn scene;
}
// on "init" you need to initialize your instancebool SplashScene::init()
{
if ( !Layer::init() )
{
returnfalse;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
auto sprite = Sprite::create("splash.png");
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 , visibleSize.height/2 ));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
returntrue;
}
void SplashScene::onEnter() {
Layer::onEnter();
// Wait for 0.5 seconds to load main scene
this->scheduleOnce(schedule_selector(SplashScene::finishSplash), 0.5f);
}
void SplashScene::finishSplash(float dt) {
// ... do whatever other initializations here// Show the actual main scene
Director::getInstance()->replaceScene(HomeScene::createScene());
}
2 Create SplashScene.h
#ifndef __SplashScene__#define __SplashScene__#include<stdio.h>#include"cocos2d.h"
USING_NS_CC;
classSplashScene : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointerstatic Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphonevirtualboolinit();
voidonEnter();
voidfinishSplash(float dt);
// implement the "static create()" method manuallyCREATE_FUNC(SplashScene);
};
#endif/* defined(__stickerPuzzle__SplashScene__) */
3 Modify AppDelegate.cpp
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLViewImpl::create("My Game");
director->setOpenGLView(glview);
}
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0 / 60);
// ... other stuff ... #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)// On Android - start the splash scene first
auto scene = SplashScene::createScene();
director->runWithScene(scene);
#endif#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)// On iOS, no need to add splash scene, start main scene directly
auto scene = HomeScene::createScene();
director->runWithScene(scene);
#endifreturntrue;
}
Solution 2:
I am using the scheduleOnce
function.
void Splash::onEnter(){
CCLayer::onEnter();
CCLog("onEnter");
this->scheduleOnce(schedule_selector(Splash::finishSplash),2.0f);
}
void Splash::finishSplash(float dt){
CCDirector::sharedDirector()->replaceScene(GameWall::scene());
}
after 2 seconds, finish the splash screen and start GameWall Screen. That's all I have done.
Solution 3:
You can make class inherit from CCLayer which start your applications and display your sprite. Then you can override onEnterTransitionDidFinish method:
void SplashScreen::onEnterTransitionDidFinish(){
CCScene * sceneAfterSplashScreen = StartLayer::scene();
CCDirector::sharedDirector()->replaceScene(sceneAfterSplashScreen);
}
When the splash screen arrived to the screen, then it's start to load new scene and replace it.
Post a Comment for "How To Create Android Cocos2dx Splash Screen?"