cocos2d创建带有背景颜色的layer
来源:互联网 发布:linux查询ip 编辑:程序博客网 时间:2024/05/21 10:20
这次笔者创建了一个带有背景颜色的layer,同时生成了一个Sprite和一个CCLabelTTf,字体显示在Sprite的中心所在。
代码如下:
GameScene.h
#ifndef __GAMASCENE__H#define __GAMASCENE__H#include "cocos2d.h"class CGameScene : public cocos2d::CCLayerColor{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 recommend returning the class instance pointer static cocos2d::CCScene* scene(); // a selector callback // void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually CREATE_FUNC(CGameScene);};#endif // __HELLOWORLD_SCENE_H__
GameScene.cpp文件内容如下:
#include "GameScene.h"#include "AppMacros.h"USING_NS_CC;CCScene* CGameScene::scene() {// 'scene' is an autorelease objectCCScene *scene = CCScene::create();// 'layer' is an autorelease objectCGameScene *layer = CGameScene::create();//默示获得视口(可视区域)的大小CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();//表示可视区域的起点坐标,这在处理相对位置的时候非常有用,确保节点在不同分辨率下的位置一致CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();//将该层设置为屏幕大小layer->setContentSize(CCSize(visibleSize.width,visibleSize.height));//将该层的起点设置为屏幕原点layer->setPosition(origin.x,origin.y);// add layer as a child to scenescene->addChild(layer);// return the scenereturn scene;}bool CGameScene::init() { if (!CCLayerColor::initWithColor(ccc4(0xff, 0xff, 0xff, 0xff), 200, 200)) { return false; }CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); //默示获得视口(可视区域)的大小CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); //表示可视区域的起点坐标,这在处理相对位置的时候非常有用,确保节点在不同分辨率下的位置一致for (int i = 0; i < 20; i++) {CCSprite* pSprite = ::CCSprite::create("qiu1.png");pSprite->setScale(0.25); //缩放为原来的1/2pSprite->setPosition(ccp(visibleSize.width / 2 + origin.x+i*60,visibleSize.height / 2 + origin.y)); //确保该图在屏幕中心CCLabelTTF*pLabel = CCLabelTTF::create("4", "Arial", TITLE_FONT_SIZE);CCSize pSize = pSprite->getContentSize();CCPoint pPoint = pSprite->getPosition();pLabel->setScale(4);pLabel->setPosition(ccp(pSize.width / 2, pSize.height / 2)); //如果想要子节点在父节点的中心,则只是需要获得父节点的尺寸,子节点的位置是父节点的中心即可int a = pSprite->getZOrder();pLabel->setZOrder(a + 1);//设置颜色为粉红pLabel->setColor(ccc3(255,192,203));pSprite->addChild(pLabel);this->addChild(pSprite, 0);}return true;}这样就完成了一个背景层颜色设置。以及两个父子关系的CCNode的相对位置确定
0 0
- cocos2d创建带有背景颜色的layer
- cocos2d之,创建一个有背景颜色的layer
- 创建有颜色的Layer
- Cocos2d改变背景颜色
- cocos2d-iPhone 基础之一: director、layer、sprite的初级创建
- 如何创建一根带有设定颜色的软管
- cocos2d-x设置背景颜色
- cocos2d-x设置背景颜色
- cocos2d-x设置背景颜色
- 【cocos2d-js】创建单一颜色的精灵
- 去掉网页从网页中拷贝到word中段落带有的背景颜色
- cocos2d-html5 Layer 和 Scene 创建模式
- cocos2d-x 3.2 创建一个无限滚动的背景
- 输出带有颜色的echo
- 绘制带有颜色的模型
- Cocos2d-x从入门到精通第12课《layer的创建与使用》
- 颜色背景的CSS
- 桌面图标的背景颜色
- 多线程14__Java同步代码块,同步方法
- C和指针读书笔记-第10章(结构和联合)
- 多继承(含虚函数覆盖)vftb中虚函数排列
- 用Python和Pygame写游戏-从入门到精通(5)
- git解决冲突与merge
- cocos2d创建带有背景颜色的layer
- 天津网站建设-SEO
- 解题报告 之 POJ2699 The Maximum Number of Strong Kings
- 【Python】豆瓣FM API分析
- Android开发——Activity间的值传递
- 遗留代码
- HTML5数据推送SSE原理及应用开发
- 基于nrf51822的蓝牙低功耗温湿度节点设计
- 对话框窗口最大最小化