LoginSignup
30
30

More than 5 years have passed since last update.

RPG風に文字を1文字ずつ表示 cocos2dx ver3.2

Posted at

http://anz-note.tumblr.com/post/78738162338/cocos2d-x-v3
3.0 rc0からできなくなったとあるのでなんとかしてできないかと
いろいろ試したところ、LabelにはgetLetterという便利なメソッドが追加されていた。
ttfでもRPG風文字ができたのでそのメモ。
cocos2dxのテストコードから拝借して検証開始。

    auto label1 = Label::createWithTTF("あいうえおかきくけこ", "fonts/ipag.ttf", 24); // createWithBMFont("fonts/bitmapFontTest2.fnt", "Test");
    label1->setTextColor(Color4B::BLACK);                            
    label1->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
    popup->addChild(label1);

    {
        auto AChar = label1->getLetter(0);
        auto rotate = RotateBy::create(2, 360);
        auto rot_4ever = RepeatForever::create(rotate);
        AChar->runAction(rot_4ever);
    }

    {
        auto AChar = label1->getLetter(2);
        auto fade_out = FadeOut::create(1);
        auto fade_in = FadeIn::create(1);
        auto seq = Sequence::create(fade_out, fade_in, nullptr);
        auto fade_4ever = RepeatForever::create(seq);
        AChar->runAction(fade_4ever);
    }

    {
        auto AChar = label1->getLetter(1);
        AChar->setVisible(false);
    }

ここで、大事だったこととしてはsetColorだとgetLetterで取得した文字の色がデフォルトの白固定になった点。setTextColorがただしいのかな。でもsetColorでもgetLetterで動かさなければ色の変更はできたので何が正しいのかよくわからない。。
うごけばいいということで。
あと、{}しているのは変数名を同じにするためにスコープ与えているだけです。手抜きです。
一文字一文字にアニメーションがつけられて便利そう。

    auto label1 = Label::createWithTTF(FileUtils::getInstance()->getStringFromFile(FileUtils::getInstance()->fullPathForFilename("data/label.txt")), "fonts/ipag.ttf", 24);
    label1->setDimensions(520, 300);
    label1->setTextColor(Color4B::BLACK);                                                                                             
    label1->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));                                                             
    popup->addChild(label1);                                                                                                          


    for(int i = 0; i < label1->getStringLength(); i++) {                                                                              
        auto AChar = label1->getLetter(i);                                                                                            
        if(nullptr != AChar) {                                                                                                        
            AChar->setVisible(false);                                                                                                 
            AChar->runAction(                                                                                                         
                    Sequence::createWithTwoActions(
                    DelayTime::create(0.2f*i),
                    Show::create()
                    ));
        }
    }

ACharでnullptrちぇっくしているのは、改行コードの文字位置だとnullptrかえしてくるから。
やりたかったことはできた気がする。

結論、cocos2dxで困ったらtestsの中身をgrepするとだいたいやりたいことのサンプルコードはある。

30
30
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
30