【cocos2d-x】公告欄的實現-文字左右移動

來源:互聯網
上載者:User

添加公告欄的方法(右側出來,左側消失)
公告欄用的是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));}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.