上一篇文章写了一个Box2d的例子

春节期间一直也没更新这个博客,其实在那个基础上

后续又写了很多很多的代码,做了一个'平衡棒'小游戏

代码比较多,以后可能会不断完善争取发布一个正式版本

 

关于Box2D的讲解内容非常多,彻底搞懂不容易

自己现在做这个'平衡棒'也是刚入门,之后会系统的分篇讲解Box2D的游戏开发攻略

 

言归正传进入今天的内容!

一个完整的游戏,需要中文显示的支持,还需要本地保存游戏进度

下面就来依次解决这两个'看似简单'的问题

 

1.中文显示

在HelloWorld例子中,我们可以看到

CCLabelTTF *pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

这条语句写出了 程序中的Hello World , 我们把它改一下,改成'你好'

CCLabelTTF *pLabel = CCLabelTTF::create("你好", "Arial", 24);

这时在windows上VS编译运行,我们发现,显示的是乱码!

编译到安卓上,还是乱码!

百度搜这个问题,解决方案真的是五花八门

绕了很多弯路之后,我在这里推荐一种最最简洁的方法,如下:

把源文件(.h .cpp)转存为utf8编码格式!

操作如下

文件->另存为->保存按钮的下拉按钮->选择编码格式为utf-8
 
    
     
     
 
这样一来,编译到安卓上就可以正确显示汉字了,但是windows上依旧乱码
不过也没关系,反正也不是在windows上玩儿,这是最便捷的方法,不用考虑第三方库或者字体或其他什么乱七八糟的
其原因在于VS上汉字编码与Cocos2d-x不一致,需要一次GBK到utf8的转码
以上内容参考LiuYuping的博客[1] 传送门
里面对中文乱码问题写的十分详细
 
2.XML本地存储
读写XML用到了CCUserDefault::sharedUserDefault()这个类
我们先去红孩儿的博客里学习一下[2] 传送门
(写的相当详细)
下面就非常的容易了
直接放出源码
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"

class HelloWorld : public cocos2d::CCLayer, public cocos2d::extension::CCEditBoxDelegate
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  

    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::CCScene* scene();

    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    void test(CCObject* pSender);

    virtual void editBoxEditingDidBegin(cocos2d::extension::CCEditBox* editBox);
    virtual void editBoxEditingDidEnd(cocos2d::extension::CCEditBox* editBox);
    virtual void editBoxTextChanged(cocos2d::extension::CCEditBox* editBox, const std::string& text);
    virtual void editBoxReturn(cocos2d::extension::CCEditBox* editBox);
    
    cocos2d::CCLabelTTF* chinese;

    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"

USING_NS_CC;
USING_NS_CC_EXT;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();

    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
        "CloseNormal.png",
        "CloseSelected.png",
        this,
        menu_selector(HelloWorld::menuCloseCallback));

    pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
        origin.y + pCloseItem->getContentSize().height/2));

    CCMenuItemImage *Item = CCMenuItemImage::create(
        "CloseNormal.png",
        "CloseSelected.png",
        this,
        menu_selector(HelloWorld::test));

    Item->setPosition(ccp(origin.x + pCloseItem->getContentSize().width/2 ,
        origin.y + pCloseItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem,Item, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);

    /////////////////////////////
    // 3. add your codes below...

    chinese=CCLabelTTF::create("1w我们哈哈会哦","Arial",18);  
    ccColor3B c;  
    c.r=0;  
    c.g=0;  
    c.b=255;  
    chinese->setColor(c);  
    chinese->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height - chinese->getContentSize().height));  
    addChild(chinese,1);

    std::string thenum = CCUserDefault::sharedUserDefault()->getStringForKey("integer");    
    //chinese->setString(thenum.c_str());

    CCSize editBoxSize = CCSizeMake(visibleSize.width - 100, 60);

    // top
    CCEditBox *m_pEditName = CCEditBox::create(editBoxSize, CCScale9Sprite::create("green_edit.png"));
    m_pEditName->setPosition(ccp(origin.x+visibleSize.width/2,origin.y+visibleSize.height*3/4));

    m_pEditName->setFontSize(25);
    m_pEditName->setFontColor(ccWHITE);
    m_pEditName->setPlaceHolder("Name:");
    m_pEditName->setPlaceholderFontColor(ccWHITE);
    m_pEditName->setMaxLength(8);
    m_pEditName->setReturnType(kKeyboardReturnTypeDone);
    m_pEditName->setDelegate(this);
    addChild(m_pEditName,1);




    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);

    return true;
}

void HelloWorld::editBoxEditingDidBegin(cocos2d::extension::CCEditBox* editBox)
{
    CCLog("editBox %p DidBegin !", editBox);
}

void HelloWorld::editBoxEditingDidEnd(cocos2d::extension::CCEditBox* editBox)
{
    CCLog("editBox %p DidEnd !", editBox);
}

void HelloWorld::editBoxTextChanged(cocos2d::extension::CCEditBox* editBox, const std::string& text)
{
    CCLog("editBox %p TextChanged, text: %s ", editBox, text.c_str());
}

void HelloWorld::editBoxReturn(CCEditBox* editBox)
{
    CCLog("text is %s ",editBox->getText());
    
    CCUserDefault::sharedUserDefault()->setStringForKey("integer", editBox->getText());
    CCUserDefault::sharedUserDefault()->flush(); 
    chinese->setString(editBox->getText());
}

void HelloWorld::test(CCObject* pSender)
{
        std::string thenum = CCUserDefault::sharedUserDefault()->getStringForKey("integer");
        chinese->setString(thenum.c_str());
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
    CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}

以上分别是头文件源文件和资源图片green_edit.png(图片放到Resources目录下)

特别注意cpp文件要转存为utf8格式

编译至安卓上运行,可以看到一个输入框,输入完字符后执行代码

CCUserDefault::sharedUserDefault()->setStringForKey("integer", editBox->getText());
CCUserDefault::sharedUserDefault()->flush(); //写入XML
chinese->setString(editBox->getText());

写入并更改label

左下角的按钮是读取XML

std::string thenum = CCUserDefault::sharedUserDefault()->getStringForKey("integer");
chinese->setString(thenum.c_str());

可以试着关闭程序再次打开,点击左下角按钮

可以看到label中的文字,即为上次我们存入的

 

本篇到此结束

参考文献

[1].http://blog.csdn.net/smileyuping/article/details/9635365

[2].http://blog.csdn.net/honghaier/article/details/8814551