Cocos2dx 中视觉部分主要由场景,层,精灵组成。层是一个容器, 主要有两点: (1) 响应用户的触摸 (2) 带有颜色的层。
这里探讨一下带有颜色的层(LayerColor):
LayerColor: 创建一个具有颜色层并且可以设置透明度。(用途: 创建暂停界面和模式工作框)
//创建有颜色的层 auto color = LayerColor::create(); color->initWithColor(Color4B(162, 162, 162, 128)); //半透明灰色;
-----------------------------------------------------------------------------------------------------------------------------------------------
我们实现一个例子: 点击游戏场景中的菜单,会使游戏屏幕变灰色(创建了带有颜色的层),然后在点击带有颜色的层,返回到游戏场景中。(如果不了解触摸事件,没有关系,以后会
说的)
思路: a. 游戏场景中创建背景和菜单1 b, 菜单1的回调函数中创建带有颜色的层,在带有颜色的层中添加菜单2,在菜单2的回调函数中使带有颜色的层移动,最后游戏场景中添加这
个带有颜色的层
void Test::onEnter() { Layer::onEnter(); Size visibleSize = Director::getInstance()->getVisibleSize(); //载入背景 auto back = Sprite::create("background.png"); back->setAnchorPoint(Vec2(0,0)); this->addChild(back); //暂停按钮 auto menuItem = MenuItemImage::create("start-up.png","start-down.png",CC_CALLBACK_1(Test::selecedMenu,this)); menuItem->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2)); menuItem->setScale(0.5); auto menu = Menu::create(menuItem,NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu); } void Test::selecedMenu(cocos2d::Ref * ref) { //创建有颜色的层 color = LayerColor::create(); color->initWithColor(Color4B(162, 162, 162, 128)); //半透明灰色; auto menuItem = MenuItemImage::create("dir.png", "dir.png", CC_CALLBACK_1(Test::selecedMenu1, this)); menuItem->setPosition(Vec2(100, 500)); auto menu = Menu::create(menuItem, NULL); menu->setPosition(Vec2::ZERO); color->addChild(menu); this->addChild(color, 5); } void Test::selecedMenu1(cocos2d::Ref * ref) { Size visibleSize = Director::getInstance()->getVisibleSize(); auto moveTo = MoveBy::create(0.5, Vec2(0, visibleSize.height)); color->runAction(moveTo); }
(以后补充在小游戏中这个带有颜色的层的应用)