user2121776
user2121776

Reputation: 121

NSMutableArray not adding object

I keep trying to get my array count but it keeps returning 0, i think its not adding the object correctly. I Put my property in header file and synthesized it in the layer.m file. i allocated space in my super init.

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]);
            }

        }
    }
}

Upvotes: 0

Views: 1584

Answers (3)

Ikmal Ezzani
Ikmal Ezzani

Reputation: 1283

when you init you called self.asteroids = [[NSMutableArray alloc] init]; but when you adding you call [asteroids addObject:tileonPos[amounts]];.

try: [self.asteroids addObject:tileonPos[amounts]]; or [_asteroids addObject:tileonPos[amounts]];

also are you sure the self.asteroids = [[NSMutableArray alloc] init]; is executed in init?

Upvotes: 1

Anupdas
Anupdas

Reputation: 10201

If you are only going to add objects to "asteroids" in findTiles method, try initializing the array in that method before for loop .

self.asteroids = [@[] mutableCopy];

Upvotes: 0

Chuck
Chuck

Reputation: 237010

Whatever method you're initializing asteroids in either isn't run when findTiles is or isn't run at all. Without more information, it's impossible to say more, but that's almost certainly what's going on.

Upvotes: 2

Related Questions