0

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

        }
    }
}
2

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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?

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.