0

I have an NSMutableArray of NSNumbers. I enumerate over array, and for each "1" in there I need to check its position in array and do some stuff. This is how I do it:

for (NSNumber *number in fieldArray) {

    int temp = [number intValue];
    if (temp != 0)
    {
        if (temp == 1)
        {
            switch ([fieldArray indexOfObject:number]) {
                case 0:
                {
                    CCSprite *x = [CCSprite spriteWithFile:@"x.png"];
                    x.position = ccp(53,265);
                    [self addChild:x];
                }   
                    break;
                case 1:
                { 
                 //this code never gets called
                }

As I have a "1" at position zero of the array my switch statement never going to go anywhere but number 1. How do I fix that? How to check the position of the stuff in array cell?

Why I need this and why I do it this way. I'm making a tic-tac-toe game. I store field in NSMutableArray with capacity of 9. So each number in array is a cell on play field. If the cell is empty, the array holds zero for that position, if there is x - there is 1 value in array, and if there is zero in cell - the array holds 2. When drawing time comes I iterate over array and check what value I see. If the value is zero I do nothing, but if it is one, I draw at certain position. Maybe there is easier way to do this, and you can point me in that direction if there is no way to fix my problem.

EDIT: Working solution was to change the enumeration to usual loop.

 for (int i = 0; i < 9; i++) {

        int temp = [[fieldArray objectAtIndex:i] intValue];
        if (temp == 1)
        {
            switch (i) {
                case 0:
                {
                    CCSprite *x = [CCSprite spriteWithFile:@"x.png"];
                    x.position = ccp(53, 265);
                    [self addChild:x z:1];
                    break;
                }

                case 1:
                {
                    CCSprite *x = [CCSprite spriteWithFile:@"x.png"];
                    x.position = ccp(159, 265);
                    [self addChild:x z:1];
                    break;
                }

2 Answers 2

2

I think I'd avoid the enumeration for this one and look at array positions first, then content. (This isn't checked in the compiler but should give you the idea. I assume it would be possible to write the positionOfSpriteForIndex: method to return the correct ccp() value.)

for (int idx = 0; idx < [fieldArray count]; idx++) {
    NSNumber *number = [fieldArray objectAtIndex:idx];
    CCSprite *x = nil;
    switch ([number intValue]) {
        case 1:
            x = [CCSprite spriteWithFile:@"x.png"];
            x.position = [self positionOfSpriteForIndex:idx];
            [self addChild:x];
            break;

        case 2:
            x = [CCSprite spriteWithFile:@"o.png"];
            x.position = [self positionOfSpriteForIndex:idx];
            [self addChild:x];
            break;

        default:
            break;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Changing to usual for loop helped indeed, but I kept my code mostly.
0

Looks like you need to put your break inside your brackets.

switch ([fieldArray indexOfObject:number]) {
                case 0:
                {
                    CCSprite *x = [CCSprite spriteWithFile:@"x.png"];
                    x.position = ccp(53,265);
                    [self addChild:x];
                break;
                }   

1 Comment

Still same, break position was wrong, but positions still evaluated same.

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.