我一直试图让我的数组数,但它一直返回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]);
}
}
}
}
请先检查此代码是否通过了if语句。 'tileGid1 == 1'可能永远不会计算为TRUE(是)。我假设你已经将定义和启动分开了! – ipinak 2013-03-05 01:06:24
[无法添加对象到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