我一直試圖讓我的數組數,但它一直返回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