吐槽:现在在做一个不是游戏的游戏...选用cocos2dx 来做真心累啊,除了很多问题找不到解决方式之外,就是很多控件没得用!只能靠自己实现...
效果图:
实现方法其实很简单:
下面菜单是一个Menu
,然后选用MenuItemSprite
来填充,(MenuItemSprite
有个selected()
,unselected()
函数,可以方便地切换状态)
然后上面是一个LayerMultiplex
,就是一个存放Layer 的Vector,可以很方便地切换显示哪个Layer。
实现代码如下:
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
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);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
private:
LayerMultiplex *layers;
Menu *tabBarItems;
Sprite *tabBarBg;
void tapBarMenuPressed(Ref *pSender);
virtual void onEnter();
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
#define TOOLBARTAG 10086
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto layer1 = LayerColor::create(Color4B(180,180,180));
auto layer2 = LayerColor::create(Color4B(255,255,180));
auto layer3 = LayerColor::create(Color4B(0,180));
auto layer4 = LayerColor::create(Color4B(255,180));
layers = LayerMultiplex::create(layer1,layer2,layer3,layer4);
this -> addChild(layers);
//Menu背景图
tabBarBg = Sprite::create("tarbarBg.png");
tabBarBg -> setPosition(Director::getInstance() -> getVisibleSize().width * 0.5,0);
tabBarBg -> setAnchorPoint(Point(0.5,0));
this -> addChild(tabBarBg,1000);
auto toolbar1 = MenuItemSprite::create(Sprite::create("toolbar1.png"),Sprite::create("toolbar1_p.png"),CC_CALLBACK_1(HelloWorld::tapBarMenuPressed,this));
toolbar1 -> setTag(TOOLBARTAG + 0);
auto toolbar2 = MenuItemSprite::create(Sprite::create("toolbar2.png"),Sprite::create("toolbar2_p.png"),this));
toolbar2 -> setTag(TOOLBARTAG + 1);
auto toolbar3 = MenuItemSprite::create(Sprite::create("toolbar3.png"),Sprite::create("toolbar3_p.png"),this));
toolbar3 -> setTag(TOOLBARTAG + 2);
auto toolbar4 = MenuItemSprite::create(Sprite::create("toolbar4.png"),Sprite::create("toolbar4_p.png"),this));
toolbar4 -> setTag(TOOLBARTAG + 3);
tabBarItems = Menu::create(toolbar1,toolbar2,toolbar3,toolbar4,NULL);
tabBarItems -> alignItemsHorizontallyWithPadding(30);
tabBarItems -> setPositionY(tabBarBg -> getContentSize().height * 0.5f);
tabBarBg -> addChild(tabBarItems);
return true;
}
void HelloWorld::onEnter()
{
Layer::onEnter();
layers -> switchTo(0);
static_cast<MenuItemSprite *>(tabBarItems -> getChildByTag(TOOLBARTAG + 0)) -> selected();
}
void HelloWorld::tapBarMenuPressed(Ref *pSender)
{
auto toolbar = (MenuItemSprite *)pSender;
toolbar -> selected();
layers -> switchTo(toolbar -> getTag() - TOOLBARTAG);
for (int i = 0; i < tabBarItems -> getChildren().size(); i++) {
auto tmpbar = dynamic_cast<MenuItemSprite *>(tabBarItems -> getChildren().at(i));
if (tmpbar != NULL && tmpbar -> getTag() != toolbar -> getTag()) {
tmpbar -> unselected();
}
}
}
代码+图片资源点击这里下载
欢迎访问我在github搭建的博客:helkyle.tk