bool LoginScene::init() { if (!Layer::init()) { return false; } //获取图层大小,this->getContentSize() 在这里和 Director::getInstance()->getVisibleSize() 效果类似 Size wndSize = getContentSize(); //加载一个精灵当作背景 auto spriteBack = Sprite::create("bg.png"); spriteBack->setPosition(Vec2(wndSize.width / 2, wndSize.height / 2)); addChild(spriteBack); //登录底图 auto spriteLoginBack = Sprite::create("LoginBack.png"); spriteLoginBack->setPosition(Vec2(wndSize.width / 2, wndSize.height / 2 + 100)); addChild(spriteLoginBack); //创建进入游戏和退出的菜单 //MenuItemImage::create如果第一个是默认图片,第二个是点击图片,第三个是不可用图片 auto menuItemImageEnter = MenuItemImage::create("EnterGame_Normal.png", "EnterGame_Push.png", CC_CALLBACK_1(LoginScene::EnterGame, this)); menuItemImageEnter->setPosition(Vec2(wndSize.width / 2 - menuItemImageEnter->getContentSize().width / 2 - 20, 100)); auto menuItemImageExit = MenuItemImage::create("Exit_Normal.png", "Exit_Pushed.png", CC_CALLBACK_1(LoginScene::ExitGame, this)); menuItemImageExit->setPosition(Vec2(wndSize.width / 2 + menuItemImageExit->getContentSize().width / 2 + 20, 100)); auto menu = Menu::create(menuItemImageEnter, menuItemImageExit, NULL); menu->setPosition(Vec2::ZERO); addChild(menu); //设置开始菜单不可用 //menuItemImageEnter->setEnabled(true); //为了使用TextField需要使用命名空间cocos2d::ui TextField* textField = TextField::create(); textField->setPlaceHolder("enter accout"); textField->setPosition(Vec2(wndSize.width / 2, 535)); //设置为密码输入模式 //textField->setPasswordEnabled(true); addChild(textField); return true; } //进入游戏 void LoginScene::EnterGame(Ref* obj) { auto scene = HelloWorld::createScene(); //切换场景使用replaceScene而不是runWithScene,并且replaceScene会把当前显示的场景删除 //TransitionCrossFade是场景特效中的一种 //Director::getInstance()->replaceScene(TransitionCrossFade::create(1, scene)); Director::getInstance()->pushScene(scene); } //退出游戏 void LoginScene::ExitGame(Ref* obj) { Director::getInstance()->end(); } void HelloWorld::ToLoginScene(Ref* obj) { //auto scene = LoginScene::createScene(); //Director::getInstance()->replaceScene(TransitionFlipX::create(1, scene)); Director::getInstance()->popScene(); }