cocos2d-x场景与层的使用
来源:互联网 发布:linux ping 测试 带宽 编辑:程序博客网 时间:2024/05/05 02:35
今天学习了Cocosd-x中场景和层的使用。
本次实例有两个场景HelloWord , Setting,在HelloWord的游戏设置中可以切换到Setting场景
首先我们新建一个Setting类,继承自cocos2d::Layer类
具体代码如下:
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::Layer{public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // a selector callback //void menuCloseCallback(cocos2d::Ref* pSender); //void menuItemStartCallBack(cocos2d::Ref *pSender);void menuItemSettingCallBack(cocos2d::Ref *pSender);void menuItemHelpCallBack(cocos2d::Ref *pSender); // implement the "static create()" method manually CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__
#include"SettingScene.h"USING_NS_CC;Scene *Setting::createScene(){auto scene = Scene::create();auto layer = Setting::create();scene->addChild(layer);return scene;}bool Setting::init(){if (!Layer::init()){return FALSE;}Size visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin();//背景创建Sprite *bg = Sprite::create("setting-back.png");bg->setPosition(origin.x + visibleSize.width / 2,origin.y + visibleSize.height / 2);this->addChild(bg);auto soundOnMenuItem = MenuItemImage::create("on.png","on.png");auto soundOffMenuItem = MenuItemImage::create("off.png","off.png");auto soundToggleMenuItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(Setting::menuSoundToggleCallBack, this),soundOnMenuItem,soundOffMenuItem,NULL);soundToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(818, 220)));//音乐设置auto musicOnMenuItem = MenuItemImage::create("on.png","on.png");auto musicOffMenuItem = MenuItemImage::create("off.png","off.png");auto musicToggleMenuItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(Setting::menuMusicToggleCallBack, this),musicOnMenuItem,musicOffMenuItem,NULL);musicToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(818, 362)));//ok按钮auto okMenuItem = MenuItemImage::create("ok-down.png","ok-up.png", CC_CALLBACK_1(Setting::menuOkCallBack , this));okMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(600, 510)));Menu* mn = Menu::create(soundToggleMenuItem, musicToggleMenuItem, okMenuItem, NULL);mn->setPosition(Vec2::ZERO);this->addChild(mn);return TRUE;}void Setting::menuSoundToggleCallBack(cocos2d::Ref* pSender){}void Setting::menuMusicToggleCallBack(cocos2d::Ref* pSender){}void Setting::menuOkCallBack(cocos2d::Ref* pSender){Director::getInstance()->popScene();}
以上就完成了对Setting类的封装,下面我们只要在HelloWorld场景中进行场景的切换就可以了
具体代码如下:
// on "init" you need to initialize your instancebool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin();//添加背景图片Sprite *bg = Sprite::create("background.png");bg->setPosition(Vec2(origin.x + visibleSize.width / 2,origin.y + visibleSize.height / 2));this -> addChild(bg); //开始精灵Sprite *StartSpriteNormal = Sprite::create("start-up.png");Sprite *StartSpriteSelected = Sprite::create("start-down.png");MenuItemSprite *StartMenuItem = MenuItemSprite::create(StartSpriteNormal,StartSpriteSelected,CC_CALLBACK_1(HelloWorld::menuItemStartCallBack, this));StartMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(700, 170)));//设置图片菜单MenuItemImage *settingMenuItem = MenuItemImage::create("setting-up.png","setting-down.png",CC_CALLBACK_1(HelloWorld::menuItemSettingCallBack, this));settingMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(480, 400)));//设置帮助菜单MenuItemImage *helpMenuItem = MenuItemImage::create("help-up.png","help-down.png",CC_CALLBACK_1(HelloWorld::menuItemHelpCallBack, this));helpMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(860, 480)));Menu *mn = Menu::create(StartMenuItem, settingMenuItem, helpMenuItem, NULL);mn->setPosition(Vec2::ZERO);this->addChild(mn); return true;}void HelloWorld::menuItemStartCallBack(cocos2d::Ref *pSender){MenuItem *item = (MenuItem*)pSender;log("Touch Start %p ", item);}void HelloWorld::menuItemSettingCallBack(cocos2d::Ref *pSender){auto sc = Setting::createScene();Director::getInstance()->pushScene(sc);}void HelloWorld::menuItemHelpCallBack(cocos2d::Ref *pSender){}
具体使用到的场景切换函数:
void pushScene(); //切换到下一个场景,将当前场景挂起到场景堆栈中,然后切换到下一个场景
void popScene(); //回到上一个场景
以及场景过度动画:
auto sc = Setting::createScene();
auto reScene = TransitionJumpZoom::create(2.0f, sc);
Director::getInstance()->pushScene(reScene);
关键代码就是第二句动画的设置。具体动画类型去查看过渡动画类TransitionScene类和它的自雷
0 0
- cocos2d-x场景与层的使用
- Cocos2d-x 下的场景,层,精灵
- Cocos2d-x 学习----通过OpenGL绘图过程学习场景、层、与精灵的关系。
- cocos2d-x:导演,场景,层,以及精灵的介绍
- cocos2d-x 3.0 导演,场景,层,以及精灵的介绍
- 【cocos2d-x游戏开发】场景,层,精灵的区别
- cocos2d-x的场景
- Cocos2d-JS 场景与层
- cocos2d-x 基础--导演,场景,层,精灵
- 【cocos2d-x游戏开发】场景和层
- 读书笔记Cocos2d-x之场景、层
- cocos2d-x导演,层,场景,精灵
- Cocos2d-x3.0导演、场景、层、精灵的使用
- cocos2d-x 场景的切换
- cocos2d-x 场景的切换
- Cocos2d-x 场景的生命周期
- cocos2d-x 3.1.1 step by step 学习笔记7 场景与层
- cocos2d-x核心概念(三)——精灵,场景与层
- LeetCode OJ Divide Two Integers
- MongoDB安装及介绍
- Unity5 Mac 破解以及安装
- hdfs
- Java中的线程
- cocos2d-x场景与层的使用
- C#第一章
- Zeev Suraski发表于2002年:The Object-Oriented Evolution of PHP
- git & github 使用记录
- Leetcode: Reverse Integer
- Sicily 2711. 模板与STL
- 鼠标事件之游来游去的小鱼
- LeetCode OJ Remove Duplicates from Sorted Array
- Python中负数的除法和取模运算(和C比较)