2013-04-03 44 views
0

此代碼運行良好,但有一些我不明白。發佈在cocos2d

難道

//Release 
[_nextProjectile release]; 
_nextProjectile =nil; 

這裏釋放和_player的動作後置_nextProjectile爲零? 如果是這樣,下一個塊如何釋放_nextProjectile?

_nextProjectile = [[CCSprite spriteWithFile:@"projectile2.png"] retain]; 
_nextProjectile.position = ccp(20, winSize.height/2); 
... 
[_player runAction: 
[CCSequence actions: 
    [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle], 
    [CCCallBlockN actionWithBlock:^(CCNode *node) { 
    [self addChild:_nextProjectile]; 
    [_projectiles addObject:_nextProjectile]; 

    // Release 
    [_nextProjectile release]; 
    _nextProjectile =nil; 
}], 
    nil]]; 

// Move projectile to actual endpoint 
[_nextProjectile runAction: 
[CCSequence actions: 
    [CCMoveTo actionWithDuration:realMoveDuration position:realDest], 
    [CCCallBlockN actionWithBlock:^(CCNode *node) { 
    [_projectiles removeObject:node]; 
    [node removeFromParentAndCleanup:YES]; 
}], 
    nil]]; 
+0

CCSequence只執行一次。還張貼您的代碼_nextProjectile初始化 – Guru

+0

謝謝,我添加了初始化。 – coolhongly

+0

不用擔心,學會愛ARC炸彈;) – LearnCocos2D

回答

0

當CCCallBlockN被執行時(旋轉動作之後),它將被釋放並設置爲零。

然而,儘管這段代碼被執行,它還沒有被釋放,所以[_nextProjectile runAction ...]的作品,因爲你仍然有一個引用_第一個序列還沒有開始。

你必須釋放它的原因是因爲它被保留在第一位,所以保留計數在某個點下降到0。當發生這種情況時,由於CCSprite構造函數返回一個自動釋放對象,因此當主循環的下一次迭代時,最終的清除和回收內存將在obj-c運行時清理autorelease池時發生。