-1

例如,如果我的游戏中有水果,并且想将所有水果 CCSprites 添加到 CCArray 中,我该怎么做?

说我有以下代码:

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 个苹果和香蕉。

任何帮助,将不胜感激!

4

1 回答 1

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];
于 2013-05-19T18:16:04.990 回答