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