Skip to content

Instantly share code, notes, and snippets.

@akehoyayoi
Created May 4, 2015 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akehoyayoi/0466d26ea82d7c74069a to your computer and use it in GitHub Desktop.
Save akehoyayoi/0466d26ea82d7c74069a to your computer and use it in GitHub Desktop.
cocos2d-x v2系でRPG風に一文字ずつ遅延して表示するクラス
// header file
#include "cocos-ext.h"
#include "CCPointer.h"
// https://github.com/ivzave/cocos2dx-ext/blob/master/CCPointer.h
USING_NS_CC;
USING_NS_CC_EXT;
class CCLabelTTFWithAnimation
: public CCLabelTTF
, public CCNodeLoaderListener
{
public:
CCLabelTTFWithAnimation();
virtual ~CCLabelTTFWithAnimation();
CREATE_FUNC(CCLabelTTFWithAnimation);
virtual void onNodeLoaded(CCNode * pNode, CCNodeLoader * pNodeLoader);
virtual void onEnter();
virtual void setString(const char *label);
virtual void draw();
CC_SYNTHESIZE(float, m_delayTime, DelayTime);
private:
CCPointer<CCArray> _sprites;
void updateSprites();
void animationSprites();
};
class CCLabelTTFWithAnimationLoader : public CCLabelTTFLoader {
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(CCLabelTTFWithAnimationLoader, loader);
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(CCLabelTTFWithAnimation);
};
// source file
#include "CCLabelTTFWithAnimation.h"
CCLabelTTFWithAnimation::CCLabelTTFWithAnimation()
: m_delayTime(0.2f)
{}
CCLabelTTFWithAnimation::~CCLabelTTFWithAnimation()
{}
void CCLabelTTFWithAnimation::onNodeLoaded(CCNode * pNode, CCNodeLoader * pNodeLoader)
{
updateSprites();
}
void CCLabelTTFWithAnimation::onEnter()
{
CCLabelTTF::onEnter();
animationSprites();
}
void CCLabelTTFWithAnimation::setString(const char *label)
{
CCLabelTTF::setString(label);
updateSprites();
animationSprites();
}
void CCLabelTTFWithAnimation::updateSprites()
{
// 元々あった場合は全て削除する
if(_sprites) {
CCARRAY_FOREACH_CAST(_sprites, CCSprite*, sprite) {
removeChild(sprite,true);
}
_sprites->removeAllObjects();
} else {
_sprites = CCArray::create();
}
// 生成したtextureを分解してCCSpriteにする
const auto texture = getTexture();
const auto width = m_tDimensions.width;
const auto height = m_tDimensions.height;
const auto hFontSize = m_fFontSize + 9;
for(int h = 0 ; h*hFontSize < height ; h++)
for(int w = 0 ; w*m_fFontSize < width ; w++) {
const auto sprite = CCSprite::createWithTexture(texture,
CCRectMake(w*m_fFontSize,
h*hFontSize,
m_fFontSize,
hFontSize));
sprite->setPosition(CCPointMake(10 + w*m_fFontSize, height - h*hFontSize));
sprite->setVisible(false);
_sprites->addObject(sprite);
addChild(sprite);
}
}
void CCLabelTTFWithAnimation::animationSprites()
{
// 一文字ずつ遅延して表示するように設定する
int count = 0;
CCARRAY_FOREACH_CAST(_sprites, CCSprite*, sprite) {
const auto p = sprite->getPosition();
sprite->setVisible(false);
sprite->runAction(CCSequence::create(CCDelayTime::create(m_delayTime*count),CCShow::create(),NULL));
count++;
}
}
void CCLabelTTFWithAnimation::draw()
{
// 元々CCLabelTTFが持っているtextureが描画されると困るのでdraw処理は空振りするようにする
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment