代码如下:
#include <iostream>
#include "knet_typedef.h"
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
#include "ClientDefine.h"
using namespace std;
typedef enum _MSG_BOX_BG_COLOR
{
MSG_BOX_BG_COLOR_RED = 1,
MSG_BOX_BG_COLOR_GREEN = 2,
}MSG_BOX_BG_COLOR;
struct MSGBOXAINIT
{
public:
MSGBOXAINIT()
{
m_bTouchDestroy = true;
}
bool m_bTouchDestroy;
MSG_BOX_BG_COLOR m_bgColorType;
int32_t m_bgWidth;
std::string m_strText;
};
class TTMsgBoxA :public CCLayer
{
public:
TTMsgBoxA();
~TTMsgBoxA();
CREATE_FUNC(TTMsgBoxA)
virtual bool init();
virtual void onEnter();
virtual void onExit();
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
void initWithStruct(CCObject * pObj);
//添加 处理handle
void addTouchupInsideHandle(CCNode * target , SEL_CallFuncO selector);
private:
MSGBOXAINIT m_initS;
CCLabelTTF * m_textLabel;
CCScale9Sprite * m_bgSprite;
CC_SYNTHESIZE(SEL_CallFuncO, m_touchupInsideSelector, TouchupSelector);
CC_SYNTHESIZE(CCNode *, m_target, Target);
};
#include "TTMsgBoxA.h"
TTMsgBoxA:: TTMsgBoxA()
{
m_textLabel = NULL;
m_bgSprite = NULL;
m_touchupInsideSelector = NULL;
m_target = NULL;
}
TTMsgBoxA::~TTMsgBoxA()
{
}
bool TTMsgBoxA::init()
{
//自己响应touch事件
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -128 -1, true);
return true;
}
void TTMsgBoxA::onEnter()
{
CCLayer::onEnter();
}
void TTMsgBoxA::onExit()
{
CCLayer::onExit();
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
bool TTMsgBoxA::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
return true;
}
void TTMsgBoxA::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
}
void TTMsgBoxA::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
//如果是 touchRemove
if (m_initS.m_bTouchDestroy )
{
if ( m_target && m_touchupInsideSelector )
{
(m_target->*m_touchupInsideSelector)(this);
}
}
}
void TTMsgBoxA::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
{
}
//添加 处理handle
void TTMsgBoxA:: addTouchupInsideHandle(CCNode * target , SEL_CallFuncO selector)
{
m_target = target;
m_touchupInsideSelector = selector;
}
void TTMsgBoxA::initWithStruct(CCObject * pObj)
{
if (pObj == NULL)
{
return ;
}
m_initS = * (MSGBOXAINIT *) pObj;
if (m_initS.m_strText.empty() )
{
return ;
}
//先创建文本
CCSize textSize = CCSizeMake(
m_initS.m_bgWidth - 2 * 28/TT_SCALE_RATE,
0
);
if (m_textLabel)
{
m_textLabel->removeFromParentAndCleanup(true);
m_textLabel = NULL;
}
//先直接创建一个,如果发现宽高都没超过,那么就用这个
//否则,重新创建
m_textLabel = CCLabelTTF::create(m_initS.m_strText.c_str(), "Helvetica-Bold", 24/TT_SCALE_RATE);
if (m_textLabel->getContentSize().width < textSize.width && m_textLabel->getContentSize().height <= 1.5f * 24/TT_SCALE_RATE)
{
}
else
{
m_textLabel->removeFromParentAndCleanup(true);
m_textLabel = CCLabelTTF::create(m_initS.m_strText.c_str(), "Helvetica-Bold", 24/TT_SCALE_RATE, textSize, kCCTextAlignmentCenter , kCCVerticalTextAlignmentCenter );
}
//自己的size 设置
CCSize bgSize = CCSizeMake(
m_textLabel->getContentSize().width + 2 * 28/TT_SCALE_RATE,
m_textLabel->getContentSize().height + 2 * 15/TT_SCALE_RATE
);
this->setContentSize(bgSize);
if (m_textLabel)
{
m_textLabel->setAnchorPoint(ccp(0, 0));
m_textLabel->setPosition(ccp(
28/TT_SCALE_RATE,
15/TT_SCALE_RATE
));
this->addChild(m_textLabel, 2);
}
//再创建背景
if (m_bgSprite)
{
m_bgSprite->removeFromParentAndCleanup(true);
m_bgSprite = NULL;
}
std::string strBgName = "tips2bg_green.png";
if (m_initS.m_bgColorType == MSG_BOX_BG_COLOR_RED)
{
strBgName = "tips2bg_red.png";
}
else if(m_initS.m_bgColorType == MSG_BOX_BG_COLOR_GREEN)
{
strBgName = "tips2bg_green.png";
}
CCSpriteFrame * bgFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(strBgName.c_str());
if (bgFrame)
{
m_bgSprite = CCScale9Sprite::createWithSpriteFrame(bgFrame);
}
if (m_bgSprite)
{
m_bgSprite->setContentSize(bgSize);
m_bgSprite->setAnchorPoint(ccp(0, 0));
m_bgSprite->setPosition(ccp(
0,
0
));
this->addChild(m_bgSprite ,1);
}
}
效果图如下: