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