日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1.Cocos2dx 3.2中vector,ValueMap,Touch触摸时间的使用.iconv字符编解码

發布時間:2024/9/27 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1.Cocos2dx 3.2中vector,ValueMap,Touch触摸时间的使用.iconv字符编解码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  • Cocos2dx3.2以后使用Vector<T>代替了CCArray。案例如下:

  • 頭文件:T02Vector.h

    #ifndef __T02Vector_H__

    #define __T02Vector_H__

    ?

    #include "T32.h"

    ?

    class T02Vector : public Layer

    {

    public:

    ??? CREATE_FUNC(T02Vector);

    ?

    ??? //Cocos2dx3.2以后使用Vector代替了CCArray

    ??? Vector<Sprite*> _arr;

    ?

    ??? bool init();

    };

    ?

    #endif

    編寫:T02Vector.cpp

    #include "T02Vector.h"

    ?

    //in cocos3.2 Vector代替CCArray

    //如果不是Ref的子類,那不能用Vector,應該用std::vector

    bool T02Vector::init()

    {

    ??? Layer::init();

    ??? Sprite* sprite = Sprite::create();

    ???

    ??? //增加元素

    ??? _arr.pushBack(sprite);

    ?

    ??? //遍歷

    ??? Vector<Sprite*>::iterator it;

    ??? for (it = _arr.begin(); it != _arr.end(); ++it)

    ??? {

    ??????? Sprite* s = *it;

    ??? }

    ??? for (auto it = _arr.begin(); it != _arr.end();++it)

    ??? {

    ????

    ??? }

    ?

    ??? for (auto it: _arr)

    ??? {

    ?

    ??? }

    ?

    ??? //從后往前遍歷

    ??? for (auto it = _arr.rbegin(); it != _arr.rend();++it)

    ??? {

    ?

    ??? }

    ?

    ??? //刪除

    ??? _arr.eraseObject(sprite);

    ?

    ??? return true;

    }

    2 Map的用法(注意字符編解碼的第三方庫有:iconvcocos中集成的有這方面的功能)

    頭文件:T03Map.h

    #ifndef __T03Map_H__

    #define __T03Map_H__

    ?

    #include "T32.h"

    class T03Map : public Layer{

    public:

    ??? CREATE_FUNC(T03Map);

    ??? bool init();

    };

    ?

    #endif

    編寫:T03Map.cpp

    #include "T03Map.h"

    ?

    /*

    ???? ValueMap是用來代替cocos2d.xCCDictionary

    */

    bool T03Map::init()

    {

    ??? Layer::init();

    ?

    ??? //內容的加載

    ??? ValueMap& vm = FileUtils::getInstance()->getValueMapFromFile("about.xml");

    ???

    ??? //CCDictionary* dict = CCDictionary::createWithContentsOfFile("about.xml");

    ??? //const CCString* x = dict->valueForKey("x");

    ??? //x->intValue();

    ?

    ??? //查找

    ??? auto it = vm.find("aaa");

    ??? if (it == vm.end())

    ??? {

    ??????? CCLog("can not find aaa");

    ??? }

    ?

    ??? it = vm.find("people3");

    ??? it->first;?? //key:的類型是std::string

    ??? it->second;? //value:的類型是Value,相對cocos3.2.3CCString

    ??? CCLog("key is %s, value is %s", it->first.c_str(), it->second.asString().c_str());

    ?

    ??? CCLog("............................end");

    ?

    ??? vm["中文"] = "bbb";

    ?

    ??? CCLog("........start walk over");

    ??? //遍歷

    ??? for (auto it = vm.begin(); it != vm.end();++it)

    ??? {

    ??????? CCLog("key is %s,value is %s",it->first.c_str(),it->second.asString().c_str());

    ??? }

    ??? CCLog("..........................end");

    ?

    ??? FileUtils::getInstance()->writeToFile(vm, "new.xml");

    ?

    #if 0

    ??? // C++11

    ??? for (auto it : vm)

    ??? {

    ??????? it.first;

    ??????? it.second;

    ??? }

    ?

    ??? // 插入

    ??? vm["aa"] = 10;

    ?

    ??? // 訪問,這種訪問有副作用,如果bb節點不存在,它會創建一個bb節點

    ??? Value& v = vm["bb"];

    ??? v = 100;

    ?

    ??? vm["bb"] = false;

    #endif

    ??? return true;

    }

    用到的about.xml如下:

    <?xml version="1.0" encoding="UTF-8" ?>

    <plist>

    <dict>

    <key>people1</key>

    <string>許佳音工作室出品</string>

    <key>people2</key>

    <string>總監:許佳音</string>

    <key>people3</key>

    <string>程序:姜博</string>

    <key>people4</key>

    <string>美術:馬俊</string>

    <key>people5</key>

    <string>改編:班級</string>

    </dict>

    </plist>

    3 ?T04Label的用法

    頭文件:T04Label.h

    #ifndef __T04Label_H__

    #define __T04Label_H__

    ?

    #include "T32.h"

    class T04Label :public Layer{

    public:

    ??? CREATE_FUNC(T04Label);

    ?

    ??? bool init();

    };

    ?

    #endif

    編寫:T04Label.cpp

    #include "T04Label.h"

    ?

    bool T04Label::init()

    {

    ??? Layer::init();

    ?

    ??? {

    ??????? Label* label = Label::createWithCharMap("fonts/Labelatlas.png", 24, 32, '0');

    ??????? label->setString("12345");

    ??????? addChild(label);

    ??????? label->setPosition(winSize.width / 2, winSize.height / 2);

    ??? }

    ?

    #if 0

    ??? Label* label = Label::createWithBMFont();

    ??? Label* label = Label::createWithSystemFont("aaa", "Arial", 24);

    ??? Label* label = Label::createWithTTF("");

    #endif

    ??? //Label* label = Label::createWithTexture()

    ??? return true;

    }

    運行結果:

    3 ?T05Touch觸摸事件的用法

    頭文件:T05Touch.h

    #ifndef __T05Touch_H__

    #define __T05Touch_H__

    ?

    #include "T32.h"

    class T05Touch :public Layer

    {

    public:

    ??? CREATE_FUNC(T05Touch);

    ?

    ??? bool init();

    ??? void TouchEnded(Touch*,Event *);

    };

    ?

    #endif

    編寫:T05Touch.cpp

    #include "T05Touch.h"

    ?

    bool T05Touch::init()

    {

    ??? Layer::init();

    ?

    ??? {

    ??????? // 一般使用這種方式,和一個Node相關聯

    ??????? EventListenerTouchOneByOne* ev = EventListenerTouchOneByOne::create();

    ??????? ev->onTouchBegan = [](Touch*, Event*){return true; };

    ??????? //? ev->onTouchEnded = [](Touch*, Event*){};

    ??????? ev->onTouchEnded = CC_CALLBACK_2(T05Touch::TouchEnded, this);

    ?

    ??????? _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

    ??? }

    ?

    #if 0

    ??? {

    ??????? // 固有優先級的方式使用比較少

    ??????? EventListenerTouchOneByOne* ev = EventListenerTouchOneByOne::create();

    ??????? ev->setSwallowTouches(true);

    ?

    ??????? ev->onTouchBegan = [](Touch*, Event*){CCLog("Touch Begin"); return true; };

    ?

    ??????? _eventDispatcher->addEventListenerWithFixedPriority(ev, -128);

    ??? }

    #endif

    ?

    ??? {

    ??????? Sprite* node = Sprite::create();

    ??????? addChild(node);

    ?

    ??????? EventListenerTouchOneByOne* ev = EventListenerTouchOneByOne::create();

    ??????? ev->onTouchBegan = [](Touch* touch, Event*){

    ??????????? //通過touch->getLocation()的方式獲得被選中的點的位置

    ??????????? Vec2 pt = touch->getLocation();

    ??????????? CCLog("Sprite is touched, pt.x=%f, pt.y=%f", pt.x, pt.y);

    ??????????? return true;

    ?

    ??????? };

    ??????? //? ev->onTouchEnded = [](Touch*, Event*){};

    ??????? // ev->onTouchEnded = CC_CALLBACK_2(T05Touch::TouchEnded, this);

    ?

    ??????? _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, node);

    ?

    ??? }

    ?

    ??? {

    ??????? EventListenerTouchAllAtOnce* ev = EventListenerTouchAllAtOnce::create();

    ??????? ev->onTouchesBegan = [](const std::vector<Touch*>&, Event*){};

    ??????? _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

    ??? }

    ?

    ??? return true;

    }

    ?

    void T05Touch::TouchEnded(Touch*, Event*){

    }

    ?

    與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的1.Cocos2dx 3.2中vector,ValueMap,Touch触摸时间的使用.iconv字符编解码的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。