这个工厂函数负责解析字符串形式的属性,然后根据解析结果创建并返回对应的 Node* 节点。

数据协议可以选用 JSON,也可以选用高性能的 Google FlatBuffers。


Node * FactoryCreater(string type,  string style )
{
	Node * ret = nullptr;

	if (type == "sprite")
	{
		auto ret = Sprite::create("shouzhi_dianji0.png");

		//解析器
		std::unordered_map<string,string> _map_arrt;

		static string NON = "";

		int cursor = 0;
		int las1 = 0, las2 = 0;
		style.append(",");
		for (int i = 0; i < style.size(); i++)
		{
			if (style[i] == ':')
			{
				las1 = i;
			}
			if (style[i] == ',')
			{
				las2 = i;
				string key =  style.substr(cursor, las1 - cursor);
				string value= style.substr(las1 + 1, las2 - las1 - 1);
				_map_arrt.insert(std::make_pair(key, value));
				cursor = las2 + 1;
			}

		}

		//解析完毕,初始化属性
		float x = atof(_map_arrt["x"].c_str());
		float y = atof(_map_arrt["y"].c_str());
		ret->setPosition(x,y);
		float scale = 1.0f;
		if (_map_arrt["scale"] != NON)
		{
			scale = atof(_map_arrt["scale"].c_str());
		}


		ret->setScale(scale);

		return ret;
	}


	if (type == "label")
	{

	}

	return nullptr;
}




// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

	this->addChild(FactoryCreater("sprite" , "x:100,y:200,scale:0.1"));
	this->addChild(FactoryCreater("sprite", "x:200,y:10"));



    return true;
}