cocos2d之,创建一个有背景颜色的layer

创建一个带有背景颜色的Layer,我们需要用到CCLayer的一个派生类CCLayerColor,看如下代码:

    CCSize s = CCDirector::sharedDirector()->getWinSize();
    CCLayer *layer = CCLayerColor::create(ccc4(0xff, 0x00, 0x00, 0x80), 200, 200);
    layer->ignoreAnchorPointForPosition(false);
    layer->setPosition(s.width / 2, s.height / 2);
    this->addChild(layer, 1, layer->getTag());

在这里用到了ccc4这个函数,我们看函数原型:

static inline ccColor4B
ccc4(const GLubyte r, const GLubyte g, const GLubyte b, const GLubyte o)

ccColor4B是一个结构体:

typedef struct _ccColor4B
{
    GLubyte r;
    GLubyte g;
    GLubyte b;
    GLubyte a;
} ccColor4B;

ccColor4B是个结构体,由r、g、b、a四个参数组成(a是透明度,即alpha),但和ccColor3B不同的是,它们都是浮点数,取值范围为0~1(1就相当于GLubyte的255)。squareColors_的作用是充当openGL绘制颜色的参数,因为GL的API需要浮点数,所以ccColor3B不能直接用于绘制,当层的颜色发生变化时,squareColors_会根据color_和opacity自动换算。


下面还可以通过ccBlendFunc来改变背景层的颜色:

    CCLayerColor *layer1 = CCLayerColor::create(ccc4(255, 0, 0, 100), 300, 200);
    CCLayerColor *layer2 = CCLayerColor::create(ccc4(255,255,0,110), 100, 100);
    
    addChild(layer1);
    layer1->addChild(layer2);
    layer1->ignoreAnchorPointForPosition(false);
    layer1->setAnchorPoint(ccp(0.5, 0.5));
    layer1->setPosition(ccp(250, 220));
    ccBlendFunc bf = {GL_ONE_MINUS_DST_COLOR, GL_ZERO}; //这里定义背景的颜色
    layer1->setBlendFunc(bf);



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个使用Cocos2d库创建的简单游戏的示例代码: ```c++ #include "cocos2d.h" USING_NS_CC; class HelloWorld : public cocos2d::Scene { public: static cocos2d::Scene* createScene(); virtual bool init(); void menuCloseCallback(cocos2d::Ref* pSender); CREATE_FUNC(HelloWorld); }; Scene* HelloWorld::createScene() { auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if (!Scene::init()) { return false; } auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); label->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - label->getContentSize().height)); this->addChild(label, 1); auto closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width / 2, origin.y + closeItem->getContentSize().height / 2)); auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); auto sprite = Sprite::create("HelloWorld.png"); sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); this->addChild(sprite, 0); return true; } void HelloWorld::menuCloseCallback(Ref* pSender) { Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif } int main(int argc, char** argv) { // 创建应用程序实例 auto application = Application::getInstance(); // 设置设计分辨率和屏幕分辨率匹配 application->setApplicationName("HelloWorld"); application->setCommandLineArguments(argc, argv); // 创建并运行场景 auto scene = HelloWorld::createScene(); application->run(); return 0; } ``` 这是一个简单的 "Hello World" 游戏示例,使用了Cocos2d库来创建一个场景,并在场景中显示一段文字和一个关闭按钮。你可以根据你的需求进行扩展和修改。 请确保你已经安装了Cocos2d库,并配置好开发环境。在编译和运行之前,请确保已将相关资源文件(如字体文件、图片文件等)放置在正确的路径下,并在代码中正确引用它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值