cocos2d-x新建一个场景并切换
来源:互联网 发布:阿里云可以用来做什么 编辑:程序博客网 时间:2024/05/15 10:03
在cocos2d-x分有场景,层,精灵的概念,一开始首先来新建一个场景
在项目class中添加如下代码
//fishscene.h
#ifndef __FISH_H_
#define __FISH_H__
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
class fish : public cocos2d::CCLayer//新建的类fish
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); //场景的初始化
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene();//场景的创建
// a selector callback
virtual void menuCloseCallback(CCObject* pSender);//场景中的退出按钮
// implement the "static node()" method manually
LAYER_NODE_FUNC(fish);
};
#endif // __HELLOWORLD_SCENE_H__
在fishscene.cpp中添加声明函数的实现
//fishscene.cpp
#include "fishscene.h"
using namespace cocos2d;
CCScene* fish::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::node();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
fish *layer = fish::node();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool fish::init()
{
bool bRet = false;
do
{
//////////////////////////////////////////////////////////////////////////
// super init first
//////////////////////////////////////////////////////////////////////////
CC_BREAK_IF(! CCLayer::init());
//////////////////////////////////////////////////////////////////////////
// add your codes below...
//////////////////////////////////////////////////////////////////////////
// 1. Add a menu item with "X" image, which is clicked to quit the program.
// Create a "close" menu item with close icon, it's an auto release object.
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
"closeNormal.png",
"CloseSelected.png",
this,
menu_selector(fish::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
// Place the menu item bottom-right conner.
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
// Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu);
// Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, 1);
// 2. Add a label shows "Hello World".
// Create a label and initialize with string "Hello World".
CCLabelTTF* pLabel = CCLabelTTF::labelWithString("qixianghui", "Thonburi", 64);//讲字换为qixianghui
CC_BREAK_IF(! pLabel);
// Get window size and place the label upper.
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width / 2, size.height - 20));
// Add the label to HelloWorld layer as a child layer.
this->addChild(pLabel, 1);
// 3. Add add a splash screen, show the cocos2d splash image.
CCSprite* pSprite = CCSprite::spriteWithFile("fish7.png");//用fish7作为场景
CC_BREAK_IF(! pSprite);
// Place the sprite on the center of the screen
pSprite->setPosition(ccp(size.width/2, size.height/2));
// Add the sprite to HelloWorld layer as a child layer.
this->addChild(pSprite, 0);
bRet = true;
} while (0);
return bRet;
}
void fish::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}
此时 还需要在原先的helloworld场景中添加一个按钮 作为场景切换
在hellowrld.h中添加如下声明
virtual void menuNextScene(CCObject* pSender);
在heeloworld.cpp中添加实现
void HelloWorld::menuNextScene(CCObject* pSender)
{
CCScene *scene=fish::scene();
CCDirector::sharedDirector()->replaceScene(scene);
}
然后将按钮添加之场景中
// Create a "close" menu item with close icon, it's an auto release object.
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
"CloseNormal.png",//点击前的图片
"CloseSelected.png",//点击后的图片
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
// Create a "nextscene" menu item with close icon.
CCMenuItemImage *pNextItem = CCMenuItemImage::itemFromNormalImage(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuNextScene));
CC_BREAK_IF(! pNextItem);
// Place the menu item bottom-right conner.
pNextItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, CCDirector::sharedDirector()->getWinSize().height - 20));//设置按钮的位置
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
// Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, pNextItem,NULL);//将按钮加入到菜单
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu);
// Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, 1);//将菜单添加到场景
- cocos2d-x新建一个场景并切换
- Cocos2d-x 3.0 新建一个场景并设置为项目启动时自动运行的场景
- cocos2d-x切换场景代码并添加各种切换特效
- cocos2d-x 场景切换
- cocos2d-x 场景切换
- cocos2d-x 场景切换
- Cocos2d-x场景切换
- cocos2d-x场景切换
- cocos2d-x 一个自定义场景切换的实例
- cocos2d-x场景效果切换
- cocos2d-x场景动态切换
- cocos2d-x 切换场景动画
- cocos2d-x 场景的切换
- Cocos2d-x之场景切换
- cocos2d-x场景效果切换
- [cocos2d-x]动作+场景切换
- cocos2d-x场景效果切换
- cocos2d-x切换场景效果
- android学习笔记(5)-一个搜索文件的APP(2)-搜索功能的实现
- 五大常用算法之五:分支限界法
- Raspberry Pi(数莓派)国内软件源
- CVTE 2014 面试 武汉
- 如何查看sql server2012的server
- cocos2d-x新建一个场景并切换
- Java应用程序开发
- 网络流-最大流问题 ISAP 算法
- hdu1199Color the Ball (用链表做的,但没有AC,原因超内存)
- 此文胜过你听三年的培训课----海尔集团CEO/张瑞敏•演讲语录!
- 【英语学习】-第三次纠音感悟
- 2013华为校园招聘机试题 (0903)
- 新型的按键扫描程序
- linux-3.2.36内核启动3-setup_arch中的内存初始化2(arm平台 分析建立页表)