到目前为止,我有代码
- (void)CreatenewBlock:(int)blockCountToSpawn
{
for (int i=0; i != blockCountToSpawn; i++)
{
theBlockxCord = arc4random() % 4;
theBlockyCord = arc4random() % 4;
NSLog(@"New Block with Cords (%i, %i)",theBlockxCord, theBlockyCord);
}
}
循环直到达到blockCountToSpawn的数量 这正是我想要的方式,但我想每次都将theBlockxCord设置为一个新变量。所以最终结果将是:
theBlockxCordOfBlock1=2
theBlockxCordOfBlock2=4
theBlockxCordOfBlock3=1
theBlockxCordOfBlock4=3
而不是每次都覆盖BlockXCord。
对于奖励积分,有没有一种方法可以在阵列中调用它们,所以我不必继续这样做:
if (theBlockxCordOfBlock1 == 2 || theBlockxCordOfBlock3 == 2 ..etc)
{
do stuff..
}
答案 0 :(得分:2)
您可以使用C数组或NSMutableArray。在将int
添加到数组之前,您必须将NSNumber
转换为- (void)CreatenewBlock:(int)blockCountToSpawn
{
NSMutableArray *blockXCoord = [NSMutableArray array]; // Retain it as needed.
NSMutableArray *blockYCoord = [NSMutableArray array];
for (int i=0; i != blockCountToSpawn; i++)
{
[blockXCoord addObject:[NSNumber numberWithInt:(arc4random() % 4)];
[blockYCoord addObject:[NSNumber numberWithInt:(arc4random() % 4)];
}
...
}
。
2
如果您要搜索if ( [blockXCoord indexOfObject:[NSNumber numberWithInt:2]] != NSNotFound ) {
... do stuff
}
,请执行此操作
if ( [blockXCoord containsObject:[NSNumber numberWithInt:2]] ) {
... do stuff
}
或
for ( int i = 0; i < [blockXCoord count]; i++ ) {
NSPoint point = NSMakePoint([[blockXCoord objectAtIndex:i] intValue],[[blockYCoord objectAtIndex:i] intValue]);
... do something with the point.
}
修改强>
{{1}}
答案 1 :(得分:2)
对于您的第一个问题:将结果放入常规数组中,就像任何C程序或NSMutableArray一样。 http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html
对于第二个问题:如果使用NSMutableArray存储对象(例如,NSNumber),则可以调用containsObject:
来确定对象是否在数组中。 http://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/containsObject: