[Cocos2d-x] announcement board implementation-text move left and right

Source: Internet
Author: User

How to add the announcement board (when the right side is displayed, the left side disappears)
The announcement board uses the node, the anchor position is (0, 0), the text information is saved using CCLabelTTF, and the anchor position is (0, 0). You can add it to the node when using it.
The idea of text movement is: update the coordinates of the CCLabelTTF announced every time. In order to move it from right to left, the right sidebar is displayed, and the left sidebar disappears, you need to set the display area of the CCLabelTTF. The CCLabelTTF: setTextureRect function sets the display area of the Label. Therefore, the left and right boundaries need special processing. solution:

// TODO: Initialize the announcement board void publish (); void publish (float dt); CCRect m_informRect; CCLabelTTF * m_inform; // The announcement board text float m_informScrollX; // TODO: announcement board 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, using 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 (); // left boundary of the text X axis m_informScrollX-= 1.0f; if (m_informScrollX <-m_informRect.size.width) {m_informScrollX. width; m_inform-> setTextureRect (CCRectMake (0, 0, m_informRect.size.width, size. height);} // The text is output from the right int expose = size. width-m_informScrollX; if (expose <m_informRect.size.width) {// text section not all show m_inform-> setTextureRect (CCRectMake (0, 0, expose, size. height);} The else {// text section is displayed from the right. m_inform-> setTextureRect (CCRectMake (0, 0, m_informRect.size.width, size. height);} // text disappears from the left 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 ));}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.