cocosエンジンを使用してplistピクチャを分解する

5519 ワード

最近、プロジェクトの練習手を探したいのですが、素材がないのに苦労して、他のところから1部探していますが、画像はパッケージ化されていて、多くの画像は練習手にとって余計です.パッケージを減らし、より柔軟に画像を使うために、cocos 2 dxエンジンライブラリを使ってすべての散図を切り取りました.cococos 3のため.xの後にレンダリング方式が変更されたので、nodeで直接visitするのは難しいので、plistで読み取った画像に従ってレンダリングしてRenderTextureで保存することにしました.
勝手にシーンを作成して、私が直接使っているhellowordは、init関数の中でplist画像をSpriteFrameCacheに追加します.
  SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plistname);

次に、必要な変数をいくつか初期化し、フレームループを開きます.
    timer = 0;    //   timer
    number = 0;  //       
    callindex = 0;//              
  timeadd = 0; scheduleUpdate();// update

update関数:
timeaddは、現在切り取る画像がレンダリングされるのに十分な時間があることを保証するために遅延するために使用されます.
    timer += dt;
    if (timer >= (callindex + 2 + timeradd)) {
        savefile();
        timeadd += 0.2f;
    }

savefile関数:
  auto winsize = Director::getInstance()->getWinSize();
    ValueMap vm = FileUtils::getInstance()->getValueMapFromFile(plistname);
    ValueMap& framesDict = vm["frames"].asValueMap();
    Scene *scene = Director::getInstance()->getRunningScene();
    int index = 0;

    for (auto iter = framesDict.begin(); iter != framesDict.end(); ++iter)
    {
        if(index == numberindex)
        {
        if (callindex == numberindex * 2) {
            if(!prename.empty())
            {
            if (this->getChildByName(prename)) {
                this->removeChildByName(prename);
            }
            }
            std::string spriteFrameName = iter->first;
            prename = spriteFrameName;
            auto sp = Sprite::createWithSpriteFrameName(spriteFrameName);
            sp->setAnchorPoint(Vec2(0, 0));
            sp->setPosition(Vec2(0, 0));
            sp->setName(prename);
            sp->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
            spsize = sp->getContentSize();
            addChild(sp);
            numberindex++;
            break;
        }
        else{
            RenderTexture* rt = RenderTexture::create(spsize.width, spsize.height);
            rt->begin();
            scene->visit();
            rt->end();
            rt->saveToFile(prename, Image::Format::PNG);
            break;
        }
        }
        index++;
    }
   
    callindex++;
    if (callindex == framesDict.size()*2) {
        RenderTexture* rt = RenderTexture::create(spsize.width, spsize.height);
        rt->begin();
        scene->visit();
        rt->end();
        rt->saveToFile(prename, Image::Format::PNG);
    }

1回目からsavefileを呼び出して画像のレンダリングを行い、2回目は画像の切り取りを行い、3回目は前回の画像を削除し、次の章の画像を追加し、4回目は画像の切り取りを行い、このようにして、1枚1枚plistの画像を切り取ります.
転載先:https://www.cnblogs.com/leisc/p/6642482.html