当前位置:编程学习 > wap >>

[cocos2d-x]公告栏的实现-文字左右移动

公告栏用的是node节点,锚点位置是(0,0),文字信息使用CCLabelTTF保存,锚点位置是(0,0),使用的时候将它加入到node里面就好了
 
文字移动的思路是:每次都update公告的CCLabelTTF的坐标,为了让它从右往左进行移动,右边栏出来,左边栏消失,需要设置一下CCLabelTTF的可显示区域,CCLabelTTF::setTextureRect函数正是设置Label的可显示区域,因此左右边界需要特殊处理,解决方法:
 
 
// TODO:初始化公告栏  
void InitAnnounceMsg();  
void UpdateTopAnnounce(float dt);  
  
CCRect m_informRect;  
CCLabelTTF *m_inform;   // 公告栏文本  
float m_informScrollX;  
  
// TODO:公告栏  
void CMainMenu::InitAnnounceMsg()  
{  
    string strInform;  
    Struct_Sysnotice notice;  
    CTableCache<Struct_Sysnotice> *table = sDBMgr->GetTable<Struct_Sysnotice>();  
  
    for (int i=1; i<=table->GetNumRows(); ++i)  
    {  
        if (table->GetEntry(i, ¬ice))  
        {  
            strInform += notice.m_noticecontent;  
            strInform += "                   ";  
        }  
    }  
    CCSize size = m_nodes["node_inform"]->getContentSize();  
    m_inform = CCLabelTTF::create(strInform.c_str(), "Arial", 24);  
    m_inform->setAnchorPoint(ccp(0, 0));  
    m_inform->setPosition(CCSize(size.width, 0));  
  
    m_informScrollX = size.width;  
    m_informRect = m_inform->getTextureRect();  
    m_nodes["node_inform"]->addChild(m_inform);  
    this->schedule(schedule_selector(CMainMenu::UpdateTopAnnounce), 0.01f);  
}  
  
void CMainMenu::UpdateTopAnnounce(float dt)  
{  
    CCPoint pt = m_nodes["node_inform"]->getPosition();  
    CCSize size = m_nodes["node_inform"]->getContentSize();  
      
    // 文字X轴的左边界  
    m_informScrollX -= 1.0f;  
    if (m_informScrollX < -m_informRect.size.width)  
    {  
        m_informScrollX = size.width;  
        m_inform->setTextureRect(CCRectMake(0, 0, m_informRect.size.width, size.height));  
    }  
      
    // 文字从右边出来  
    int expose = size.width-m_informScrollX;  
    if (expose < m_informRect.size.width)  
    {   // 文字部分未全部显示出来  
        m_inform->setTextureRect(CCRectMake(0, 0, expose, size.height));  
    }  
    else  
    {   // 文字部分已经从右边全部显示出来  
        m_inform->setTextureRect(CCRectMake(0, 0, m_informRect.size.width, size.height));  
    }  
      
    // 文字从左边消失  
    if (m_informScrollX <= 0)  
    {  
        float offset = fabs(m_informScrollX);  
        m_inform->setTextureRect(CCRectMake(offset, 0, m_inform->getTextureRect().size.width-offset, size.height));  
        return ;  
    }  
  
    m_inform->setPosition(CCSize(m_informScrollX, 0));  
}  

 

 
 
补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,