0

我想做一個非常簡單的事情,但它不工作。我想在一個NSMutableArray中添加一些CCParticleSystemQuad並刪除它們。這是我做的:CCParticleSystemQuand沒有處理

int cpt = 0; 
NSMutableArray *myArray = [[NSMutableArray alloc] init]; 
for (cpt = 0; cpt < 10; cpt++) { 
    part = [CCParticleSystemQuad particleWithFile:@"whiteExplosion.plist"]; 
    [myArray addObject:part]; 
} 

NSLog(@"state of myArray : %@", myArray); 

int cont = 0 
for (cont = 0; cont < 10; cont++) { 
    [myArray removeLastObject:cont]; 
} 

NSLog(@"state of myArray : %@", myArray); 

當我的NSLog我第一次有這樣的:

state of myArray : (
    "<CCParticleSystemQuad = 0x91ee380 | Tag = -1>", 
    "<CCParticleSystemQuad = 0x84aca20 | Tag = -1>", 
    "<CCParticleSystemQuad = 0x125136c0 | Tag = -1>", 
    "<CCParticleSystemQuad = 0x125b0fc0 | Tag = -1>", 
    "<CCParticleSystemQuad = 0x1250d480 | Tag = -1>", 
    "<CCParticleSystemQuad = 0x1250fa50 | Tag = -1>", 
    "<CCParticleSystemQuad = 0x9108840 | Tag = -1>", 
    "<CCParticleSystemQuad = 0x9152b70 | Tag = -1>", 
    "<CCParticleSystemQuad = 0x914fb80 | Tag = -1>", 
    "<CCParticleSystemQuad = 0x9135470 | Tag = -1>" 
) 

第二次我有這樣的:

state of myArray : (
) 

所以,你可以看到我的CCParticleSystemQuad已被刪除。但是,當我檢查儀器(分配)他們仍然生活(我還有20 [CCParticleSystemQuad allocMemory])並仍然使用內存沒有。我錯過了什麼?順便說一句,我用ARC。 我試過(NSString *)對象,它工作正常... Thx。

回答

0
[myArray removeLastObject:cont]; 

您試圖從數組中刪除一個int。你想使用removeLastObject(無PARAMS)或removeObjectAtIndex:

你的問題可能是你不調用removeChild之:顆粒

0

您必須從其父不僅從陣列中刪除。

[part removeFromParentAndCleanup:YES]; 

//從陣列

for (int i = 0; i < [myArray count]; i++) { 
     [myArray removeObjectAtIndex:i]; 
} 

//刪除對象只刪除最後一個對象

 [myArray removeObjectAtIndex:([myArray count]-1)]; 
+0

它不工作,還有我的生活的對象。如果我沒有調用addChild,爲什麼我必須調用removeChild?所以,如果我理解的很好,當我想從內存添加和刪除CCParticleSystemQuad時,我總是必須使用[self addChild:part]和[self removeChild:part]?這與NSString *對象有什麼關係?原因,我沒有addChild我的NSString從內存中刪除它... – Niknolty

+0

爲什麼你創建cocos2d粒子對象,如果沒有添加到任何層/節點?我以爲你添加了..所以建議刪除。好的LearnCocos2d已經提到你將int添加到array..happy編碼中的錯誤。 – Guru

+0

Thx爲您提供建議。但是,爲什麼將它存儲在數組中卻是一個錯誤? – Niknolty