游戏中的一种通用提示框(1)

本文展示了游戏中一种通用提示框的设计,通过效果图呈现其在游戏交互中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    代码如下:
#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);
    }
    

}


效果图如下:







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值