首页 > 代码库 > 05--创建卡片类

05--创建卡片类

使用VS类向导创建名为CardSprite的类,该类继承自CCSprite,头文件如下:

#ifndef __CARDSPRITE_SCENE_H__#define __CARDSPRITE_SCENE_H__#include "cocos2d.h"#include "ScreenAdapter.h"class CardSprite : public cocos2d::CCSprite{public:    //静态创建卡片的函数    static CardSprite* createCardSprite(int numbers,int width , int height , float CardSpriteX , float CardSpriteY);    virtual bool init();    CREATE_FUNC(CardSprite);    //卡片中数值操作    int getNumber();    void setNumber(int num);private:    //存放卡片数值    int number;    //卡片对象创建初始化使用    void cardInit(int number, int width , int height , float CardSpriteX, float CardSpriteY);    //显示数值的标签    cocos2d::CCLabelTTF* labelTTFCardNumber;    //卡片背景图层    cocos2d::CCLayerColor* layerColorBG;};#endif

          下面再看看各个函数的具体实现

          init函数:调用父类的初始化函数,这里没有添加其他的逻辑

bool CardSprite::init(){    if (!CCSprite::init())    {        return false;    }    return true;}

          cardInit函数:内部初始化卡片函数

void CardSprite::cardInit(int numbers, int width , int height , float CardSpriteX, float CardSpriteY){    //初始化成员值    number = numbers;    //初始化卡片背景颜色    layerColorBG = cocos2d::CCLayerColor::create(                        ScreenAdapter::GetBGColor() ,                        width-15 ,                         height-15);    layerColorBG->setPosition(ccp(CardSpriteX,CardSpriteY));    //创建卡片的内容并添加到背景中    if(number>0)    {        labelTTFCardNumber = cocos2d::CCLabelTTF::create(            ScreenAdapter::GetString(number),            ScreenAdapter::GetFontName(),            ScreenAdapter::GetFontSize(number));        labelTTFCardNumber->setPosition(                ccp(                layerColorBG->getContentSize().width/2,                layerColorBG->getContentSize().height/2)            );        labelTTFCardNumber->setTag(8);        layerColorBG->addChild(labelTTFCardNumber);    }    else    {        labelTTFCardNumber = cocos2d::CCLabelTTF::create(            "",            ScreenAdapter::GetFontName(),            ScreenAdapter::GetDefaultFontSize());        labelTTFCardNumber->setPosition(                ccp(                layerColorBG->getContentSize().width/2,                layerColorBG->getContentSize().height/2)                );        labelTTFCardNumber->setTag(8);        layerColorBG->addChild(labelTTFCardNumber);    }    this->addChild(layerColorBG);}

          createCardSprite函数:前面两个初始化函数已经创建并初始化了卡片,但都是CardSprite内部使用,而createCardSprite函数有外部创建卡片调用函数

CardSprite* CardSprite::createCardSprite(int numbers,int width , int height , float CardSpriteX , float CardSpriteY){    CardSprite* card=new CardSprite();    if(card && card->init())    {        card->autorelease();  // 1        card->cardInit(numbers,width,height,CardSpriteX,CardSpriteY);        return card;    }    CC_SAFE_DELETE(card);  // 2    //  1 , 2 处涉及到引擎的内存管理,先抄下来}

         数值操作函数

int CardSprite::getNumber(){    return number;}void CardSprite::setNumber(int num){    number = num;    //根据数值大小修改字体大小和背景颜色    labelTTFCardNumber->setFontSize(ScreenAdapter::GetFontSize(number));      layerColorBG->setColor(ScreenAdapter::GetColor(number));     labelTTFCardNumber->setString(ScreenAdapter::GetString(number));}

        ScreenAdapter中新加的函数

char* ScreenAdapter::GetFontName(){    return "HiraKakuProN-W6";}cocos2d::ccColor4B ScreenAdapter::GetBGColor(){    return cocos2d::ccc4(200,190,180,255);}