XmlParser * XmlParser::createWithFile(const char *fileName)
{
XmlParser *pRet = new XmlParser();
// ①
return pRet;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto goItem = MenuItemImage::create(
"go-down.png",
"go-up.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
goItem->setPosition(Vec2(origin.x + visibleSize.width - goItem->getContentSize().width/2 ,
origin.y + goItem->getContentSize().height/2));
auto menu = Menu::create(goItem, NULL); ①
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1); ②
this->list = __Array::createWithCapacity(MAX_COUNT);
this->list->retain(); ③
for(int i = 0;i < MAX_COUNT; ++i){
Sprite* sprite = Sprite::create("Ball.png");
this->list->addObject(sprite); ④
}
return true;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Ref* obj = NULL;
log("list->count() = %d",this->list->count()); ⑤
Size visibleSize = Director::getInstance()->getVisibleSize();
CCARRAY_FOREACH(this->list, obj) {
Sprite* sprite = (Sprite*)obj; ⑥
int x = CCRANDOM_0_1() * visibleSize.width;
int y = CCRANDOM_0_1() * visibleSize.height;
sprite->setPosition( Vec2(x, y) );
this->removeChild(sprite);
this->addChild(sprite);
}
}
HelloWorld::~HelloWorld()
{
this->list->removeAllObjects();
CC_SAFE_RELEASE_NULL(this->list); ⑦
}
void Ref::release()
{
CCASSERT(_referenceCount > 0, "reference count should greater than 0");
--_referenceCount;
if (_referenceCount == 0)
{
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
auto poolManager = PoolManager::getInstance();
if (!poolManager->getCurrentPool()->isClearing() && poolManager->isObjectInPools(this))
{
// Trigger an assert if the reference count is 0 but the Ref is still in autorelease pool.
// This happens when 'autorelease/release' were not used in pairs with 'new/retain'.
//
// Wrong usage (1):
//
// auto obj = Node::create(); // Ref = 1, but it's an autorelease Ref which means it was in the autorelease pool.
// obj->autorelease(); // Wrong: If you wish to invoke autorelease several times, you should retain `obj` first.
//
// Wrong usage (2):
//
// auto obj = Node::create();
// obj->release(); // Wrong: obj is an autorelease Ref, it will be released when clearing current pool.
//
// Correct usage (1):
//
// auto obj = Node::create();
// |- new Node(); // `new` is the pair of the `autorelease` of next line
// |- autorelease(); // The pair of `new Node`.
//
// obj->retain();
// obj->autorelease(); // This `autorelease` is the pair of `retain` of previous line.
//
// Correct usage (2):
//
// auto obj = Node::create();
// obj->retain();
// obj->release(); // This `release` is the pair of `retain` of previous line.
CCASSERT(false, "The reference shouldn't be 0 because it is still in autorelease pool.");
}
#endif
delete this;
}
}
void AutoreleasePool::clear()
{
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
_isClearing = true;
#endif
for (const auto &obj : _managedObjectArray)
{
obj->release();
}
_managedObjectArray.clear();
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
_isClearing = false;
#endif
}
《Cocos2d-x实战 C++卷》现已上线,各大商店均已开售:
京东:http://item.jd.com/11584534.html
亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU
当当:http://product.dangdang.com/23606265.html
互动出版网:http://product.china-pub.com/3770734
《Cocos2d-x实战 C++卷》源码及样章下载地址:
源码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1
样章下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1