现在的位置: 首页 > 综合 > 正文

cocos2d-x简单的绘制

2018年03月21日 ⁄ 综合 ⁄ 共 3434字 ⁄ 字号 评论关闭

1.      如何绘制一个字符串

一般使用CCLabelTTF

四个步骤:

1)初始化

CCLabelTTF *pLabel = CCLabelTTF::create(“Hello Cocos2d-x”, “Arial” , 24);

参数1:字符串内容

参数2:字体

参数3:字符串的大小

   2)检查CC_BREAK_IF();

      CC_BREAK_IF(!pLabel);   //判断指针pLabel 是否创建成功,否则break

   3)设置坐标点

      CCSize size = CCDirector::SharedDirector()->getWinSize();

      pLabel->setPosition(ccp(size.width/2 , size.height-50));

   注:ccp为一个点的类

   补充:CCDirector 导演类,整体框架类,导演类为一个单例类

        SharedDirector() 获得导演类的实例

        导演类中一些函数可以获得设置窗体的一些信息

   4)加入当前图层

     this->addChild(pLabel, 1);

   注:参数2为第几图层,从0开始,数字越小越底层图层

 

 

2.      如何绘制一张图片

一般使用CCSprite  (精灵)

四个步骤:

1)     初始化

CCSprite *pSprite = CCSprite::create(“HelloWorld.png”);

2)     检查CC_BREAK_IF();

CC_BREAK_IF(!pSprite);

3)     设置坐标点

CCSize  size =CCDirector::sharedDirector()->getWinSize();

pSprite->setPosition(ccp(size.width/2, size.height/2));

4)     加入当前图层

this->addChild(pSprite, 1);

 

 

3.      如何创建按钮

CCMenu添加CCMenuItem的子类(Menu可以统一管理,方便设置布局)

 

1)文本按钮

CCLabelTTF +CCMenuItemLabel

 

//使用CCLabelTTF和CCMenuItemLabel创建一个文本按钮

CCLabelTTF *pBtn= CCLabelTTF::create(“LabelButton” , “Arial” , 20);

//检查

CC_BREAK_IF(!pBtn);

//设置坐标点

//通过pBtn生成一个CCMenuItemLabel

CCMenuItemLabel* pItemLabel = CCMenuItemLabel::create(pBtn);

CC_BREAK_IF(!pItemLabel);

pItemLabel->setPosition(ccp(100,100));

 

//添加到Menu层

CCMenu *pMenu =CCMenu::create(pItemLabel,NULL);

CC_BREAK_IF(!pMenu);

pMenu->setPosition(CCPointZero);

this->addChild(pMenu, 1);

 

 

 

CCMenuItemFont

 

//使用CCMenuItemFont创建一个文本按钮

CCMenuItemFont*pItemFont = CCMenuItemFont::create(“FontButton” , “this” , NULL);

//参数2:当前的图层,参数3:NULL表示按下不做任何操作

//检查

CC_BREAK_IF(!pItemFont);

//设置坐标

pItemFont->setPosition(300,300);

//添加到Menu层

CCMenu *pMenu =CCMenu::create(pItemFont , NULL);

CC_BREAK_IF(!pMenu);

pMenu->setPosition(CCPointZero);

this->addChild(pMenu, 1);

2)图片按钮

  CCMenuItemImage 图片按钮

 

//使用图片生成一个按钮

CCMenuItemImage *pItemImage =CCMenuItemImage::create(

“CloseNormal.png”,

“CloseSelected.png”,this,NULL);

    //参数1:未选中的图片的地址,参数2:选中后的图片的地址

//检查

CC_BREAK_IF(!pItemImage);

//设置坐标

pItemImage->setPosition(300,200);

//添加到Menu层

CCMenu *pMenu =CCMenu::create(pItemImage , NULL);

CC_BREAK_IF(!pMenu);

pMenu->setPosition(CCPointZero);

this->addChild(pMenu, 1);

 

 

 

4.      如何生成一个动画

图片放入项目

1.      生成纹理CCTexture2D *t=

CCTextureCache::SharedTextureCache()->addImage(“图片路径”);

2.      截取每帧 CCSpriteFrame *f=

CCSpriteFrame::creat(t, CCRectMake(起点x , 起点y , 宽度 , 高度));

3.      所有帧放入CCArray * array = CCArray::creat(总帧数);

4.      生成动画 CCAnimation

5.      初始化CCSprite并添加到图层

6.      使用CCAnimation生成一个CCAnimate动作

7.      CCSprite绑定CCAnimate

 

代码实例:

//读取2d纹理

CCTexture2D * pTexture =CCTextureCache::sharedTextureCache()->addImage(“girl.png”);

//创建第一帧

CCSpriteFrame *frame0 =CCSpriteFrame::create(pTexture , CCRectMake(32*0,48*0,32,48));

//创建第二帧

CCSpriteFrame *frame1 =CCSpriteFrame::create(pTexture , CCRectMake(32*1,48*0,32, 48));

//创建第三帧

CCSpriteFrame *frame2 =CCSpriteFrame::create(pTexture , CCRectMake(32*2,48*0,32, 48));

//创建第四帧

CCSpriteFrame *frame3 =CCSpriteFrame::create(pTexture , CCRectMake(32*3,48*0,32, 48));

CCArray *pArray = CCArray::create(4);

pArray->addObject(frame0);

pArray->addObject(frame1);

pArray->addObject(frame2);

pArray->addObject(frame3);

 

//创建动画

CCAnimation * animation =CCAnimation::create(pArray,0.5f);  //0.5f为每附图跳转的时间间//隔

CC_BREAK_IF(!animation);

//初始化并设置一个Sprite

CCSprite *girlSprite = CCSprite::create(frame0);  //设置精灵默认开始的动作

CC_BREAK_IF(!girlSprite);

//设置坐标位置

girlSprite->setPosition(ccp(size.width/2 ,size.height/2+100));

this->addChild(girlSprite,2);

//使用animation生成animate

CCAnimate * animate =CCAnimate::actionWithAnimation(animation); //循环播放设置

CC_BREAK_IF(!animate);

girlSprite->runAction(CCRepeatPorever::create(animate));

 

 

 

 

 

 

抱歉!评论已关闭.