"Independent Developer ER cocos2d-x combat 007" using COCOS2DX Userdefault.xml

Source: Internet
Author: User

This blog is generated by the following questions-(code 1):
STD:: String str =FileUtils:: getinstance ()Getwritablepath (); Cclog (Str.c_str ());Userdefault:: getinstance ()Setstringforkey ("Keyqq11","Value11");FileUtils:: getinstance ()Setwritablepath ("d:/"); str =FileUtils:: getinstance ()Getwritablepath (); Cclog (Str.c_str ());Userdefault:: getinstance ()Setstringforkey ("Keyqq12","Value22");
At first I thought Userdefault.xml would spawn in two places, and I was wrong. Userdefault.xml only exists in the default path: c:/users/g40-70/appdata/local/canimal/. Note: For the default path, we can print using the method mentioned above. So I went into a long search for answers – my approach is source debugging. Note: If you are using Cocosstudio project, you need to switch to COCOS2DX project, because Cocosstudio does not provide fileutils implementation. Let's look at the Userdefault::getinstance interface first-(Code 2):
UserDefault* UserDefault::getInstance(){    if (!_userDefault)    {        initXMLFilePath();        // only create xml file one time        // the file exists after the program exit        if ((!&& (!createXMLFile()))        {            return nullptr;        }        =new (std::nothrow) UserDefault();    }    return _userDefault;}
Take a closer look at the Initxmlfilepath () interface-(Code 3):
voidUserDefault::initXMLFilePath(){    if (! _isFilePathInitialized)    {        FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME;        true;    }    }
As you can see from the above two pieces of code, the Userdefault is set for the path when it is initialized, and the _filepath value is modified only the first time. So the above code 1 problem solved: Because in code 1 we call a total of two times the following code:
UserDefault::getInstance()->setStringForKey(......);

The Userdefault.xml path is set only on the first call, so the second call, although Fileutils._writablepath has been modified at this time, However, it does not make the Userdefault.filepath modification, so code 1 appears only in one place to generate the userdefault.xml.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Independent Developer ER cocos2d-x combat 007" using COCOS2DX Userdefault.xml

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.