2012-02-27 78 views
0

我加載項的Cocos2D保持ccmenuitem列表

NSMutableArray *listOfItems; 
    Items *items = [ItemParser loadItemsForALevel:selectedLevel fromASuperLevel:selectedSuperLevel]; 

名單在我的for循環中,我加入一個NSMutableArray,以顯示給我的菜單層

for (Item *item in items.items) { 

//Initialing a CCMenuItem and calling a removeMethod 
CMenuItem *anItem = [[CCMenuItemSprite itemFromNormalSprite:[CCSprite spriteWithFile:item.picture] 
selectedSprite:[CCSprite spriteWithFile:item.picture] 
target:self 
selector:@selector(removeMethod:)] retain]; 

NSString *textToDisplay; 
textToDisplay = item.ItemText; 
[listOfItems addObject:textToDisplay]; 

} 

-(void)removeMethod:(id)sender { 
CCMenuItemSprite *pointedItem = (CCMenuItemSprite *)sender; 

//Now I'm removing only from the layer 
[CCCallFunc actionWithTarget:pointedItem selector:@selector(removeFromParentAndCleanup:)] 

//How do I remove it from array? 

} 

如何從listOfItems數組中刪除所選項並更新它?

回答

0

將'textToDisplay'作爲全局變量。在removeMethod中。您可以使用

[listOfItems removeObjectAtIndex:[listOfItems indexOfObject:textToDisplay]];

希望這個幫助:-)

+0

謝謝!它崩潰並返回:'程序接收信號:EXC_BAD_ACCESS',有疑問? ;) – 2012-02-27 17:28:36

+0

只需檢查textToDisplay是否返回一個字符串。 – Anish 2012-02-28 02:21:27