2013-03-05 90 views
0

我一直试图让我的数组数,但它一直返回0,我认为它没有正确添加对象。我将我的属性放在头文件中并将其合成到layer.m文件中。我在我的超级初始化中分配了空间。NSMutableArray不添加对象

NSMutableArray *asteroids; 

@property (strong) NSMutableArray *asteroids; 

@synthesize asteroids; 

self.asteroids = [[NSMutableArray alloc] init]; 

-(void)findTiles 
{ 
    CGPoint tilecoord1; 
    int tileGid1; 
    int aheadcount = 200; 
    CCSprite *tileonPos[aheadcount]; 
    int amounts = 0; 

    for(int x = 0; x<30; x++) 
    { 
     for(int y = 0; y<20; y++) 
     { 
      tilecoord1 = ccp(x+(480*currentlevel),y); 
      tileGid1 = [bglayer tileGIDAt:tilecoord1]; 
      if(tileGid1 == 1) 
      { 
       tileonPos[amounts] = [bglayer tileAt:ccp(x,y)]; 
       amounts++; 
       [asteroids addObject:tileonPos[amounts]]; 
       NSLog(@"%d",[asteroids count]); 
      } 

     } 
    } 
} 
+0

请先检查此代码是否通过了if语句。 'tileGid1 == 1'可能永远不会计算为TRUE(是)。我假设你已经将定义和启动分开了! – ipinak 2013-03-05 01:06:24

+0

[无法添加对象到NSMutableArray]的可能的重复(http://stackoverflow.com/q/851926)[无法添加项目到NSMutableArray ivar](http://stackoverflow.com/q/7125326),[\ [NSMutableArray addObject:\]不影响计数](http://stackoverflow.com/q/3683761),[\ [NSMutableArray addObject:\] not working](http://stackoverflow.com/q/1827058) – 2013-03-05 01:46:21

回答

2

无论采用哪种方式你在初始化asteroids既可以当findTiles是或根本不运行不运行。没有更多的信息,就不可能说更多,但这几乎可以肯定发生了什么。

+0

Bingo ,谢谢,菜鸟的错误。 – user2121776 2013-03-05 01:17:54

1

当你初始化你叫self.asteroids = [[NSMutableArray alloc] init];,但是当你加入你呼叫[asteroids addObject:tileonPos[amounts]];

尝试:[self.asteroids addObject:tileonPos[amounts]];[_asteroids addObject:tileonPos[amounts]];

也是你确定self.asteroids = [[NSMutableArray alloc] init];在初始化执行?

0

如果您只是要将对象添加到findTiles方法中的“小行星”,请尝试在for循环前初始化该方法中的数组。

self.asteroids = [@[] mutableCopy]; 
相关问题