Hey. I’ve got a simple loading sprite on a layer. I want to covert it so that i can reuse it in many cpp files. Since i’m not that good at c++, some help is very appreciated. Here is the code:
void LobbyScene::loadingAnim()
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
this->loading = Layer::create();
this->loading->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
Sprite* loadingSpriteBg = Sprite::create("res/LoadingNode/bg_loading.png");
loadingSpriteBg->setOpacity(128);
this->loading->addChild(loadingSpriteBg);
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);
listener1->onTouchBegan = [](Touch* touch, Event* event){
return true;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this->loading);
Vector<SpriteFrame*> frames(4);
SpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("res/LoadingNode/data.plist");
auto animSpr = Sprite::create("res/LoadingNode/a1.png");
for (int i = 1; i <= 4; i++) {
auto frame = String::createWithFormat("a%d.png", i);
frames.pushBack(
SpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(
frame->getCString()));
}
Animation* anim = Animation::createWithSpriteFrames(frames, 0.2f);
animSpr->setPosition(Point(loadingSpriteBg->getContentSize().width / 2, loadingSpriteBg->getContentSize().height / 2));
loadingSpriteBg->addChild(animSpr, 1);
animSpr->runAction(RepeatForever::create(Animate::create(anim)));
this->addChild(this->loading, 90);
}
void LobbyScene::removeLoading()
{
this->removeChild(this->loading, true);
}
In .h file:
cocos2d::Layer* loading;
Here is how i did:
CustomClass.h
#ifndef __CUSTOM__CLASS
#define __CUSTOM__CLASS
#include "cocos2d.h"
class CustomClass : public cocos2d::Layer
{
public:
CustomClass();
~CustomClass();
static CustomClass* create();
virtual bool init();
private:
cocos2d::Sprite* loadingSpriteBg;
cocos2d::Vector<cocos2d::SpriteFrame*> frames; //4 frame olacak.
cocos2d::Sprite* animSpr;
cocos2d::Animation* anim;
cocos2d::EventListenerTouchOneByOne* listener1;
};
#endif // __CUSTOM__CLASS
CustomClass.cpp
#include "CustomClass.h"
USING_NS_CC;
CustomClass::CustomClass(){}
CustomClass::~CustomClass(){}
CustomClass* CustomClass::create()
{
CustomClass *ret = new (std::nothrow) CustomClass;
if (ret && ret->init())
{
ret->autorelease();
return ret;
}
else
{
CC_SAFE_DELETE(ret);
return nullptr;
}
}
bool CustomClass::init()
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
this->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
loadingSpriteBg = Sprite::create("res/LoadingNode/bg_loading.png");
loadingSpriteBg->setOpacity(128);
this->addChild(loadingSpriteBg);
listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);
listener1->onTouchBegan = [](Touch* touch, Event* event){
return true;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
frames.reserve(4);
SpriteFrameCache* a = SpriteFrameCache::getInstance()->sharedSpriteFrameCache();
a->addSpriteFramesWithFile("res/LoadingNode/data.plist");
animSpr = Sprite::create("res/LoadingNode/a1.png");
for (int i = 1; i <= 4; i++) {
auto frame = String::createWithFormat("a%d.png", i);
frames.pushBack(
SpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(
frame->getCString()));
}
anim = Animation::createWithSpriteFrames(frames, 0.2f);
animSpr->setPosition(Point(loadingSpriteBg->getContentSize().width / 2, loadingSpriteBg->getContentSize().height / 2));
loadingSpriteBg->addChild(animSpr, 1);
animSpr->runAction(RepeatForever::create(Animate::create(anim)));
return true;
}
And in any cpp file:
CustomClass* pLayer = CustomClass::create();
this->addChild(pLayer, 99);
What is the way to add my CustomClass instance as child in CustomClass.cpp?