tags:

views:

32

answers:

2

I am new to Obj C, but familiar with OOP. I am working on a game that uses collision detection to score points and change UIImageView properties like background color.

I have a finite number of UIImageViews that are subviews of a larger view. I can manually check for collision and set the background colors of the UIImageViews accordingly. The problem is that this creates a rather large if / else block that I can see growing out of control. In JavaScript I can use a for loop and eval() to evaluate a string + i (the loop var) and then simply call the new var.backgroundColor = someColor;

It seems that eval is not used in OBJ C, so I am looking for an equivalent that will allow me to create a loop that executes the same block of code on a given object that only differs from other objects by name only:

Example:

if (someStatement == true){ object_01.someProperty = newProperty; } else if (someOtherStatement == true){ object_02.someProperty = newProperty; } else if (someOtherOtherStatement == true){ object_03.someProperty = newProperty; }

I want to write the above as a loop. I realize I can use an Array, but if so, how? Also, I want to loop through CGRects which are not storing properly in an Array.

Any help greatly appreciated!

Damon

A: 

Creating your array:

NSMutableArray *items = [NSMutableArray array];

Populating your array with CGRects:

[items addObject:[NSValue valueWithCGRect:frame]];

Pulling a CGRect from the array:

CGRect frame = [[items objectAtIndex:0] CGRectValue];

Replacing an index in the array:

[items replaceObjectAtIndex:0 withObject:[NSValue valueWithCGRect:frame]];
rpetrich
Thank you for clarifying the improper use of eval(). I was told never to use it!!!It looks like the answer is to create an array of CGRects and an array for the UIImageViews and then set the properties based on the value of objectAtIndex:i. I will give it a shot and see if I can simplify my code block.Do you know if Apple frowns on large if/else blocks for game development?Thanks again!
Damonmath
I don't think `eval` should be banned outright, but there are very few circumstances where it's the best or even a reasonable choice. Large if/else blocks are fine as long as they don't make your game sluggish :)
rpetrich
"Large if/else blocks are fine as long as they don't make your game sluggish :)"Right ;) I think I can handle everything I want to do in one large block, but if I can get it down to 12 lines from 600+ and still accomplish the same goal... then walla. My large block is just for collision detection anyways. I'd rather take the proper approach and loop through the array.Thanks Again!
Damonmath
NSMutableArray of CGRects worked. Thanks!
Damonmath
A: 

The best way is to put object_01, object_02, ... into an array and then use `-[NSArray objectAtIndex:] to access the objects.

You could also use Key-Value Coding:

NSString *keyPath = [NSString stringWithFormat:@"object_0%d.someProperty", objectIndex];
[self setValue:newProperty forKeyPath:keyPath];

But as rpetrich mentioned, it's not good programming style.

Ole Begemann
Sneaky. Didn't even think of this.
rpetrich