2013-05-19 62 views
-1

如果我例如在我的遊戲中有水果,並且想要將所有水果CCSprites添加到CCArray,我該怎麼做?如何在CCArray中添加多個CCSprites

說我有下面的代碼:

CCSprite *Apple = [CCSprite spriteWithFile:@"Apple2.png"]; 
CCSprite *Banana = [CCSprite spriteWithFile:@"Banana2.png"]; 

,這是我CCArray:

CCArray *FruitsArray = [CCSprite spriteWithFile:@"Banana.png", @"Apple.png", nil];// Why is this not working? 
self.FruitsArray = [CCArray arrayWithCapacity:10]; 

我想FruitsArray具有10的容量,所以應該保持10個蘋果和香蕉。

任何幫助,將不勝感激!

回答

0

不工作,因爲它不是有效的語法調用spriteWithFile功能:

[CCSprite spriteWithFile:@"Banana.png", @"Apple.png", nil]; 

語法:

+(id) spriteWithFile:(NSString*)filename; 
// Returns Object of CCSprite class 

«你爲什麼不能得到CCSprite類和您的需要的基礎上覆蓋spriteWithFile?

OR 

«爲什麼你不能像這樣添加每個對象?

CCArray *fruitsArray = [CCArray array]; 
    CCSprite *Apple = [CCSprite spriteWithFile:@"Apple2.png"]; 
    CCSprite *Banana = [CCSprite spriteWithFile:@"Banana2.png"]; 

    [fruitsArray addObject: Apple];  
    [fruitsArray addObject: Banana]; 
+0

我真的不明白你的意思了。你能多解釋一下嗎?我是編程新手。 –

+0

更新了我的答案。 – Guru

相關問題