0

我创建了一个类方法,它返回一个随机位置作为 NSValue 对象。我想将返回的位置存储在一个可变数组中,这样下次调用类方法时,它会检查可变数组中的位置,如果找到一个与存储的位置太接近的位置,则会生成一个新的位置。

我似乎无法弄清楚如何分配和初始化我的可变数组以与我的类方法一起使用。我试过在类方法中分配它。但这不起作用,因为它会在每次调用时被初始化,因为之前存储的值被......删除了?

然后我想使用生成的位置在视图上放置一些圆圈(没有它们重叠)。现在随机生成的位置可以工作,但是由于在我的 randomposition 类中未正确实现可变数组,它们重叠。

我仍然是目标 c 的初学者......和一般的编程......

@implementation randomPosition

NSMutableArray *positionsArrayPlayer1;


    +(NSValue *) givePosition:(int)player forRadius: (CGFloat) radius

    {

        if (player == 1)
        {
            //set range for arc4random
            int fromNumberY = 550;
            int toNumberY = 950;

            //set range for arc4random
            int fromNumberX = 0;
            int toNumberX = 700;


            BOOL far_enough_away = NO;
            CGPoint newpoint;

            while(!far_enough_away)
            {

                newpoint = CGPointMake((arc4random()%(toNumberX-fromNumberX+1))+fromNumberX, 
                                       (arc4random()%(toNumberY-fromNumberY+1))+fromNumberY);
                far_enough_away = YES;


                for(NSValue *existing in positionsArrayPlayer1)
                {
                    NSLog(@"test");

                    CGPoint pointb = [existing CGPointValue];
                    CGFloat deltay = pointb.y-newpoint.y;
                    CGFloat deltax = pointb.x-newpoint.x;
                    CGFloat distance = sqrt(pow(deltax,2) + pow(deltay,2));

                    //fail if closer than desired radius
                    if(distance < radius )
                    {
                        far_enough_away = NO;

                        NSLog(@"test");
                        break;
                    }
                    [positionsArrayPlayer1 addObject:[NSValue valueWithCGPoint:newpoint]];

                }
                NSLog(@"%@",positionsArrayPlayer1);
            }

            return [NSValue valueWithCGPoint:newpoint];

        } else if (player == 2 ){



             //more stuff to come here....



        } else {


            NSLog(@"player invalid");

        }

        return nil;
    }

这是我在视图控制器中使用的方法。我还将放置在视图上的圆形对象存储在单独的数组中以进行其他操作。

- (void) positionCirclesPlayer1 
{

    radius = 70;
    CGRect position;

    for (int i = 0; i < [colors count];i++)
    {

    CGRect positionCircleInCenter = CGRectMake(self.view.frame.size.width/2-35, self.view.frame.size.height/2-35, radius, radius);

    Circle *myCircle = [[Circle alloc] initWithFrame:positionCircleInCenter radius:radius color:[colors objectAtIndex:i]];
    myCircle.label.text = [NSString stringWithFormat:@"%i", i];
    myCircle.alpha = 0;
    myCircle.userInteractionEnabled = NO;


    theNewPointPlayer1 =   [randomPosition givePosition:1 forRadius:radius];

    position = CGRectMake(theNewPointPlayer1.CGPointValue.x, theNewPointPlayer1.CGPointValue.y, 70, 70);


    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
            myCircle.frame = position; myCircle.alpha = 1;} completion:nil];

    [playerOneCircles addObject:myCircle];
    [self.view addSubview:myCircle];

    }


}
4

1 回答 1

1

在学习 Obj-C 之前,您可能应该了解一些关于 C 的知识 :)


+(NSValue*)givePosition:(int)player forRadius:(CGFloat)radius {
  //this is executed when the method is called for the first time
  static NSMutableArray *positionsArrayPlayer1 = nil;

  if (positionsArrayPlayer1 == nil) { //checking if this is the first time execution
     positionsArrayPlayer1 = [[NSMutableArray alloc] init];
  }

}

或者



//global variable accessible only from this file
static NSMutableArray *positionsArrayPlayer1;

@implementation randomPosition

//this method is called when the class is loaded.
+(void)initialize {
   positionsArrayPlayer1 = [[NSMutableArray alloc] init];
}

+(NSValue*)givePosition:(int)player forRadius:(CGFloat)radius {
   //do something with the array
}

@end

请注意,使用类方法实现此功能可能不是一个很好的架构。

于 2012-04-12T18:46:27.623 回答