先说一下CCObject派生子类CCNode,不管是CCScene、CCLayer、CCSprint、CCMenu都可以加入到CCNode当中。同时CCNode也可以放入到CCScene和CCLayer中。
游戏中的按钮操作就是通过CCMenu来实现的,说白了就是个菜单,并不叫按钮。
第一个层的代码:
//BackGroundLayer.h
#pragma once
#ifndef _BACKGROUNDLAYER_H_
#define _BACKGROUNDLAYER_H_
#include"cocos2d.h"
class BackGroundLayer:public cocos2d::CCLayer {
public:
CREATE_FUNC(BackGroundLayer);
/*static BackGroundLayer* create() {
BackGroundLayer* pRet = new BackGroundLayer;
if (pRet && pRet->init()) {
pRet->autorelease();
return pRet;
}
else {
delete pRet;
pRet = NULL;
return NULL;
}
}
*/
bool init();
void MenuNextCallBack(cocos2d::CCObject* sender);
};
#endif
//BackGroundLayer.cpp
#include"BackGroundLayer.h"
#include "SecondScene.h"
//using namespace cocos2d;
USING_NS_CC;
bool BackGroundLayer::init() {
if (CCLayer::init()) {
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite* background = CCSprite::create("bj03.jpg");
background->setPosition(winSize.width * 0.5, winSize.height * 0.5);
this->addChild(background);
CCMenuItemImage* pNextItem = CCMenuItemImage::create("next.png", "next.png", this, menu_selector(BackGroundLayer::MenuNextCallBack));
CCMenu* Menu = CCMenu::create(pNextItem, NULL);
Menu->setPosition(winSize.width - 50, 50);
this->addChild(Menu);
return true;
}
return false;
}
void BackGroundLayer::MenuNextCallBack(cocos2d::CCObject* sender) {
SecondScene* secondscene = SecondScene::create();
Director::sharedDirector()->replaceScene(secondscene);
}
第二层代码:
//BackGroundLayer2.h
#pragma once
#ifndef _BACKGROUNDLAYER2_H_
#define _BACKGROUNDLAYER2_H_
#include "cocos2d.h"
class BackGroundLayer2 : public cocos2d::CCLayer {
public:
CREATE_FUNC(BackGroundLayer2);
bool init();
protected:
void DoCloseCallBack(cocos2d::CCObject* sender);
};
#endif
//BackGroundLayer2.cpp
#include "BackGroundLayer2.h"
USING_NS_CC;
bool BackGroundLayer2::init() {
if (CCLayer::init()) {
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite* background = CCSprite::create("bj02.jpg");
background->setPosition(winSize.width * 0.5, winSize.height * 0.5);
this->addChild(background);
CCNode* node = CCNode::create();
this->addChild(node);
CCMenuItemImage* pItemImg = CCMenuItemImage::create("close.png", "close.png", this, menu_selector(BackGroundLayer2::DoCloseCallBack));
CCMenu* menu = CCMenu::create(pItemImg, NULL);
menu->setPosition(winSize.width - 50, 50);
node->addChild(menu);
return true;
}
return false;
}
void BackGroundLayer2::DoCloseCallBack(cocos2d::CCObject* sender) {
Director::sharedDirector()->end();
}
第二个场景的代码:
#pragma once
#include "cocos2d.h"
#include "BackGroundLayer2.h"
class SecondScene : public cocos2d::CCScene {
public:
CREATE_FUNC(SecondScene);
bool init();
protected:
BackGroundLayer2* backgroundLayer2;
};
//SecondScene.cpp
#include "SecondScene.h"
USING_NS_CC;
bool SecondScene::init() {
if (CCScene::init()) {
this->backgroundLayer2 = BackGroundLayer2::create();
this->addChild(backgroundLayer2);
return true;
}
return false;
}
之前说过的就不重复做笔记了,可以去看 Cococs2d-x之第一个层。
一、BackGroundLayer
CCMenuItemImage* pNextItem = CCMenuItemImage::create("next.png", "next.png", this, menu_selector(BackGroundLayer::MenuNextCallBack));
这里添加按钮图片,第一个参数是显示的按钮图片,第二个参数是点击后按钮的图片(可以和第一个相同),第三个参数是层,第四个参数是回调函数,点击按钮的响应就是通过回调函数执行。
CCMenu* Menu = CCMenu::create(pNextItem, NULL);
这里把创建的按钮图像添加到菜单中,有几个添加几个,最后参数以NULL表示结束。
Menu->setPosition(winSize.width - 50, 50);
设置菜单显示的位置
this->addChild(Menu);
将菜单添加到当前层中。
void BackGroundLayer::MenuNextCallBack(cocos2d::CCObject* sender) {
SecondScene* secondscene = SecondScene::create();
Director::sharedDirector()->replaceScene(secondscene);
}
MenuNextCallBack这个回调函数,第一句就是创建一个新的场景,让Director切换场景。
2.BackGroundLayer2
BackGroundLayer2中的init跟BackGroundLayer中是类似的,不重复说明了,就说一下里面的回调函数:
void BackGroundLayer2::DoCloseCallBack(cocos2d::CCObject* sender) {
Director::sharedDirector()->end();
}
这里的回调函数的作用就是让Director去执行关闭操作。
3.SecondScene
this->backgroundLayer2 = BackGroundLayer2::create();
this->addChild(backgroundLayer2);
init函数中这两句,首先创建第二个层,然后加入到当前SecondScene这个场景下,在BackGroundLayer调用回调函数切换场景的时候就可以看到第二个层了。
最后运行一下:
初始是这样的:
点击右下角的按钮后,变成这样:
再点击右下角按钮就关闭了。