标签:cocos2d cocos2d-x 游戏 c++ 开发人员
1.纹理像素格式bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); 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)); auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); _labelLoading = Label::createWithTTF ("loading...", "fonts/Marker Felt.ttf", 35); _labelPercent = Label::createWithTTF ("0%%", "fonts/Marker Felt.ttf", 35); _labelLoading->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2 - 20)); _labelPercent->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2 + 20)); this->addChild(_labelLoading); this->addChild(_labelPercent); _numberOfLoadedSprites = 0; _imageOffset = 0; auto sharedFileUtils = FileUtils::getInstance(); std::string fullPathForFilename = sharedFileUtils->fullPathForFilename("ImageMetaData.plist"); ① ValueVector vec = FileUtils::getInstance()->getValueVectorFromFile(fullPathForFilename); ② _numberOfSprites = vec.size(); ③ //加载纹理 for( auto& e : vec) ④ { auto row = e.asValueMap(); auto filename = "icons/" + row.at("filename").asString(); Director::getInstance()->getTextureCache()->addImageAsync(filename, CC_CALLBACK_1(HelloWorld::loadingCallBack, this)); ⑤ } return true; } void HelloWorld::loadingCallBack(Texture2D *texture) ⑥ { ++_numberOfLoadedSprites; __String* str = __String::createWithFormat("%d%%", (int)(((float)_numberOfLoadedSprites / _numberOfSprites) * 100)); ⑦ _labelPercent->setString(str->getCString()); ⑧ Size visibleSize = Director::getInstance()->getVisibleSize(); int i = ++_imageOffset * 60; auto sprite = Sprite::createWithTexture(texture); ⑨ sprite->setAnchorPoint(Vec2(0,0)); addChild(sprite, -1); sprite->setPosition(Vec2( i % (int)visibleSize.width, (i / (int)visibleSize.width) * 60)); if (_numberOfLoadedSprites == _numberOfSprites) ⑩ { _numberOfLoadedSprites = 0; } }
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <dict> <key>filename</key> <string>01-refresh.png</string> </dict> <dict> <key>filename</key> <string>02-redo.png</string> </dict> <dict> <key>filename</key> <string>03-loopback.png</string> </dict> <dict> <key>filename</key> <string>04-squiggle.png</string> </dict> … … </array> </plist>
第⑨行代码auto sprite = Sprite::createWithTexture(texture)是通过纹理对象texture创建精灵对象。第⑩行代码if (_numberOfLoadedSprites == _numberOfSprites)是判断是否完成任务,_numberOfLoadedSprites是已经加装的图片数,_numberOfSprites是要加装的全部图片数。
标签:cocos2d cocos2d-x 游戏 c++ 开发人员
原文地址:http://blog.csdn.net/tonny_guan/article/details/41016241