[Cocos2d-x entry practice] WeChat aircraft war 12: scores of Local Storage

Source: Internet
Author: User

Original Work, reprinted, please mark: Http://blog.csdn.net/jackystudio/article/details/12036237

Int score; // Real-Time score

Static int highestHistoryScore; // The highest historical score.

// Add scorescoreItem = CCLabelBMFont: create ("0", "font/font. fnt "); // scoreItem is a member variable of the CCLabelBMFont * type. The Custom font is used here, the first article provides scoreItem-> setColor (ccc3 (143,146,147); scoreItem-> setAnchorPoint (ccp (0, 0.5 )); scoreItem-> setPosition (ccp (pPauseItem-> getPositionX () + normalPause-> getContentSize (). width/2 + 5, pPauseItem-> getPositionY (); this-> addChild (scoreItem );

// Update the score void ControlLayer: updateScore (int score) {if (score> = 0 & score <= MAX_SCORE) // you can set a maximum value of 2 billion {CCString * strScore = CCString:: createWithFormat ("% d", score); // format it as a string scoreItem-> setString (strScore-> m_sString.c_str (); // obtain char * From CCString *, update score }}

// If life = 1, remove enemy3, else if (enemy3-> life = 1) {enemy3-> life --; mutiBulletsToDelete-> addObject (mutiBullets ); enemy3sToDelete-> addObject (enemy3); score + = ENEMY3_SCORE; // increase the score. ENMEY3_SCORE is a macro, the value is the score obtained by shooting down enemy 3. this-> controlLayer-> updateScore (score); // call the updateScore update score of ControlLayer}

Bool WelcomeLayer: isHaveSaveFile () // determine whether the archive exists {if (! CCUserDefault: sharedUserDefault ()-> getBoolForKey ("isHaveSaveFileXml") // you can use the configured bool flag bit to determine whether {CCUserDefault: sharedUserDefault () exists () -> setBoolForKey ("isHaveSaveFileXml", true); // write the bool judgment bit CCUserDefault: sharedUserDefault ()-> setIntegerForKey ("HighestScore", 0 ); // write the initial score 0 CCUserDefault: sharedUserDefault ()-> flush (); // call flush after setting to write data from the buffer to ioreturn false ;} else {return true ;}} void WelcomeLayer: equals () {if (isHaveSaveFile () // if an archive exists {GameOverLayer: highestHistoryScore = CCUserDefault: sharedUserDefault () -> getIntegerForKey ("HighestScore", 0); // the highest score for reading history }}

// Call if (score> highestHistoryScore) {CCUserDefault: sharedUserDefault ()-> setIntegerForKey ("HighestScore", score) in the GameOver layer ); // modify the archive highestHistoryScore = score // reset the highest score... there are other things later}

What should we do?

:

< p>

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.