Entity* EntityFactory::gen( EntityIdentifier identifier, EntityType entityType, EntityParameterLevel parameterLevel, std::map<std::string, EntityAttackParams> attackMap, Color4B initialColor ) { CSLoader* instance = CSLoader::getInstance(); switch (entityType) { case EntityType::CIRCLE: { instance->registReaderObject("CircleReader", (ObjectFactory::Instance)CircleReader::getInstance); Circle* circle = dynamic_cast<Circle*>(CSLoader::createNode("Circle.csb")); circle->setEntityType(entityType); circle->setEntityParameterLevel(parameterLevel); circle->setAttackMap(attackMap); circle->setInitialColor(initialColor); circle->setIdentifier(identifier); return circle; } case EntityType::NONE: { return nullptr; } } CCASSERT(false, "Undefined entity type is passed."); }
bool MainScene::init() { if ( !Layer::init() ) { return false; } CSLoader* instance = CSLoader::getInstance(); instance->registReaderObject("CharacterReader", (ObjectFactory::Instance) CharacterReader::getInstance); instance->registReaderObject("ObstacleReader" , (ObjectFactory::Instance) ObstacleReader::getInstance); auto rootNode = CSLoader::createNode("MainScene.csb"); Size size = Director::getInstance()->getVisibleSize(); rootNode->setContentSize(size); ui::Helper::doLayout(rootNode); this->background = rootNode->getChildByName("back"); this->character = this->background->getChildByName<Character*>("character"); this->ground[0] = rootNode->getChildByName("ground0"); this->ground[1] = rootNode->getChildByName("ground1"); this->scoreLabel = this->background->getChildByName<ui::TextBMFont*>("scoreLabel"); score = 0; this->setScore(score); this->character->setLocalZOrder(1); this->scoreLabel->setLocalZOrder(1); addChild(rootNode); return true; }
void MainScene::initNodeReader() { CSLoader* instance = CSLoader::getInstance(); // -> Cocos Studio instance loader // Register the readers for our custom classes, type casting to ObjectFactory, WITHOUT IT WILL BREAK ROOT NODE instance->registReaderObject("CharacterReader", (ObjectFactory::Instance) CharacterReader::getInstance); instance->registReaderObject("PieceReader", (ObjectFactory::Instance) PieceReader::getInstance); }
// on "init" you need to initialize your instance bool MainScene::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } // Register the readers for our custom classes // Be very careful to do CharacterReader::getInstance, not CharacterReader::getInstance() which will crash CSLoader* instance = CSLoader::getInstance(); instance->registReaderObject("CharacterReader", (ObjectFactory::Instance) CharacterReader::getInstance); instance->registReaderObject("PieceReader", (ObjectFactory::Instance) PieceReader::getInstance); this->pieceIndex = 0; this->lastObstacleSide = Side::Left; this->gameState = GameState::Title; // set up references to instance variables auto rootNode = CSLoader::createNode("MainScene.csb"); // fix resizing bug Size size = Director::getInstance()->getVisibleSize(); rootNode->setContentSize(size); ui::Helper::doLayout(rootNode); auto lifeBG = rootNode->getChildByName("lifeBG"); this->timeBar = lifeBG->getChildByName<Sprite*>("lifeBar"); this->character = rootNode->getChildByName<Character*>("character"); this->pieceNode = rootNode->getChildByName("pieceNode"); this->scoreLabel = rootNode->getChildByName<cocos2d::ui::Text*>("scoreLabel"); // initialize ten sushi pieces // stack their heights // put them in the pieces vector to track references to them for (int i = 0; i < 10; ++i) { Piece* piece = dynamic_cast<Piece*>(CSLoader::createNode("Piece.csb")); // set chopstick side this->lastObstacleSide = this->getSideForObstacle(this->lastObstacleSide); piece->setObstacleSide(this->lastObstacleSide); float rollHeight = piece->getSpriteHeight(); piece->setPosition(0.0f, rollHeight / 2.0f * i); this->pieceNode->addChild(piece); this->pieces.pushBack(piece); } this->resetGameState(); this->addChild(rootNode); return true; }
CoinContainer::CoinContainer() { CSLoader* instance = CSLoader::getInstance(); instance->registReaderObject("CoinReader", (ObjectFactory::Instance)CoinReader::getInstance); this->currentCoinCacheIndex = 0; this->coinCache = Vector<Coin*>(); for (int i = 0; i < 100; ++i) { Coin* coin = dynamic_cast<Coin*>(CSLoader::createNode("Coin.csb")); this->coinCache.pushBack(coin); } }
Node* CSLoader::createNode(const std::string &filename, const ccNodeLoadCallback &callback) { std::string path = filename; size_t pos = path.find_last_of('.'); std::string suffix = path.substr(pos + 1, path.length()); CSLoader* load = CSLoader::getInstance(); if (suffix == "csb") { return load->createNodeWithFlatBuffersFile(filename, callback); } return nullptr; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } //wchar_t buf[1000]; //GetCurrentDirectory(1000, buf); //得到当前工作路径 CSLoader* instance = CSLoader::getInstance(); instance->registReaderObject("CCLoginReader", (ObjectFactory::Instance)CCLoginReader::getInstance); auto rootNode = CSLoader::createNode("LoginScene.csb"); addChild(rootNode); return true; }
Node* CSLoader::createNode(const std::string& filename) { std::string path = filename; size_t pos = path.find_last_of('.'); std::string suffix = path.substr(pos + 1, path.length()); CSLoader* load = CSLoader::getInstance(); if (suffix == "csb") { return load->createNodeWithFlatBuffersFile(filename); } else if (suffix == "json" || suffix == "ExportJson") { return load->createNodeFromJson(filename); } return nullptr; }
Node * CSLoader::createNode(const Data& data, const ccNodeLoadCallback &callback) { CSLoader * loader = CSLoader::getInstance(); Node * node = nullptr; do { CC_BREAK_IF(data.isNull() || data.getSize() <= 0); auto csparsebinary = GetCSParseBinary(data.getBytes()); CC_BREAK_IF(nullptr == csparsebinary); auto csBuildId = csparsebinary->version(); if (csBuildId) { CCASSERT(strcmp(loader->_csBuildID.c_str(), csBuildId->c_str()) == 0, StringUtils::format("%s%s%s%s%s%s%s%s%s%s", "The reader build id of your Cocos exported file(", csBuildId->c_str(), ") and the reader build id in your Cocos2d-x(", loader->_csBuildID.c_str(), ") are not match.\n", "Please get the correct reader(build id ", csBuildId->c_str(), ")from ", "http://www.cocos2d-x.org/filedown/cocos-reader", " and replace it in your Cocos2d-x").c_str()); } // decode plist auto textures = csparsebinary->textures(); int textureSize = csparsebinary->textures()->size(); CCLOG("textureSize = %d", textureSize); for (int i = 0; i < textureSize; ++i) { SpriteFrameCache::getInstance()->addSpriteFramesWithFile(textures->Get(i)->c_str()); } node = loader->nodeWithFlatBuffers(csparsebinary->nodeTree(), callback); } while (0); loader->reconstructNestNode(node); return node; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } //CSLoader is the class that reads the binary files exported by Cocos Studio to create instances of the objects in code CSLoader* instance = CSLoader::getInstance(); // Be very careful to do GridReader::getInstance, not GridReader::getInstance() which will crash instance->registReaderObject("GridReader", (ObjectFactory::Instance) GridReader::getInstance); auto rootNode = CSLoader::createNode("MainScene.csb"); // Fixing a IDE / Code interpolation Size size = Director::getInstance()->getVisibleSize(); rootNode->setContentSize(size); ui::Helper::doLayout(rootNode); // Getting References auto leftPanel = rootNode->getChildByName("leftPanel"); auto rightPanel = rootNode->getChildByName("rightPanel"); grid = rightPanel->getChildByName<Grid*>("gridNode"); auto balloon = leftPanel->getChildByName("balloon"); generationCount = balloon->getChildByName<cocos2d::ui::Text*>("generationCount"); populationCount = balloon->getChildByName<cocos2d::ui::Text*>("populationCount"); cocos2d::ui::Button* playButton = leftPanel->getChildByName<cocos2d::ui::Button*>("btnPlay"); cocos2d::ui::Button* pauseButton = leftPanel->getChildByName<cocos2d::ui::Button*>("btnPause"); playButton->addTouchEventListener(CC_CALLBACK_2(HelloWorld::play, this)); pauseButton->addTouchEventListener(CC_CALLBACK_2(HelloWorld::pause, this)); addChild(rootNode); return true; }
// on "init" you need to initialize your instance bool MainScene::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } CSLoader* instance = CSLoader::getInstance(); instance->registReaderObject("CharacterReader", (ObjectFactory::Instance) CharacterReader::getInstance); instance->registReaderObject("ObstacleReader", (ObjectFactory::Instance) ObstacleReader::getInstance); auto rootNode = CSLoader::createNode("MainScene.csb"); //Cocosのバグ修正のために以下の3行を追加 Size size = Director::getInstance()->getVisibleSize(); rootNode->setContentSize(size); ui::Helper::doLayout(rootNode); addChild(rootNode); this->back = rootNode->getChildByName("back"); this->character = back->getChildByName<Character*>("character"); this->character->setLocalZOrder(1); auto ground = this->back->getChildByName<Sprite*>("ground"); ground->setLocalZOrder(1); auto ground2 = this->back->getChildByName<Sprite*>("ground2"); ground2->setLocalZOrder(1); this->grounds.pushBack( ground ); this->grounds.pushBack( ground2 ); this->state = GameState::Ready; return true; }
// on "init" you need to initialize your instance bool HelloWorld::init() { /** you can create scene with following comment code instead of using csb file. // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); // position the label on the center of the screen label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label, 1); // add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0); **/ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } CSLoader* instance = CSLoader::getInstance(); // Be very careful to do GridReader::getInstance, not GridReader::getInstance() which will crash instance->registReaderObject("GridReader", (ObjectFactory::Instance) GridReader::getInstance); auto rootNode = CSLoader::createNode("MainScene.csb"); Size size = Director::getInstance()->getVisibleSize(); rootNode->setContentSize(size); ui::Helper::doLayout(rootNode); addChild(rootNode); return true; }