- 说在前面
操作系统:win10 64位
vs版本:2017
python版本:2.7(必须是2.x)
Cocos2d-x版本:3.17
- 实现效果
(这是个gif)
- 流程
1. Helloworldscene::init()中调用定时器
2. 定时器时间到,调用预设函数
3. 预设函数中调用replaceScene,切换至LoginScene
- 代码
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void EnterLoginScene(float dt);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include "LoginScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
printf("Error while loading: %s\n", filename);
printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if (!Scene::init())
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
CCSprite* sp = CCSprite::create("t.jpg");
sp->setPosition(visibleSize.width / 2, visibleSize.height / 2);
sp->setAnchorPoint(Point(0.5, 0.5));
//根据缩放设置实际大小
Size size_pai = sp->getContentSize();
//根据实际宽度和总宽度,计算期望缩放比率
float scaleX_pai = (float)visibleSize.width / (float)size_pai.width;
float scaleY_pai = (float)visibleSize.height*0.8f / (float)size_pai.height;
sp->setScale(scaleX_pai, scaleX_pai);
this->addChild(sp);
//定时器,只发生一次
this->scheduleOnce(schedule_selector(HelloWorld::EnterLoginScene), 2.0f);
return true;
}
void HelloWorld::EnterLoginScene(float dt)
{
//场景切换
Director::getInstance()->replaceScene(TransitionFade::create(1.0f, LoginScene::createScene()));
}
LoginScene.h
#pragma once
#ifndef __LOGINSCNE_SCENE_H__
#define __LOGINSCENE_SCENE_H__
#include "cocos2d.h"
#include "ui\CocosGUI.h"
using namespace cocos2d::ui;
USING_NS_CC;
class LoginScene : public cocos2d::Scene
{
public:
Label * messageBox;
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(LoginScene);
void loginButtonCallback(Ref *pSender);
void registerButtonCallback(Ref *pSender);
private:
TextField * usernameInput;
TextField * passwordInput;
};
#endif
LoginScene.cpp
#include "LoginScene.h"
USING_NS_CC;
Scene* LoginScene::createScene()
{
auto scene = Scene::create();
auto layer = LoginScene::create();
scene->addChild(layer);
return scene;
}
bool LoginScene::init()
{
if (!Scene::init())
{
return false;
}
//下面的都是注册界面相关代码
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto loginButton = MenuItemFont::create("Login", CC_CALLBACK_1(LoginScene::loginButtonCallback, this));
if (loginButton) {
float x = origin.x + visibleSize.width / 2;
float y = origin.y + loginButton->getContentSize().height / 2 + 200;
loginButton->setPosition(Vec2(x, y));
}
auto registerButton = MenuItemFont::create("Register", CC_CALLBACK_1(LoginScene::registerButtonCallback, this));
if (registerButton) {
float x = origin.x + visibleSize.width / 2;
float y = origin.y + registerButton->getContentSize().height / 2 + 100;
registerButton->setPosition(Vec2(x, y));
}
auto menu = Menu::create(loginButton, registerButton, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
usernameInput = TextField::create("username", "arial", 24);
if (usernameInput) {
float x = origin.x + visibleSize.width / 2;
float y = origin.y + visibleSize.height - 100.0f;
usernameInput->setPosition(Vec2(x, y));
this->addChild(usernameInput, 1);
}
passwordInput = TextField::create("password", "arial", 24);
if (passwordInput) {
float x = origin.x + visibleSize.width / 2;
float y = origin.y + visibleSize.height - 130.0f;
passwordInput->setPosition(Vec2(x, y));
this->addChild(passwordInput, 1);
}
messageBox = Label::create("", "arial", 30);
if (messageBox) {
float x = origin.x + visibleSize.width / 2;
float y = origin.y + visibleSize.height - 200.0f;
messageBox->setPosition(Vec2(x, y));
this->addChild(messageBox, 1);
}
return true;
}
void LoginScene::loginButtonCallback(Ref *pSender)
{
}
void LoginScene::registerButtonCallback(Ref *pSender)
{
}