Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7782049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:25:10+00:00 2026-06-01T19:25:10+00:00

I’ve created a class method that returns a random position as an NSValue object.

  • 0

I’ve created a class method that returns a random position as an NSValue object.
I want to store the returned positions in a mutable array so the next time the class method gets called it checks the positions in the mutable array and if it finds one that is too close to the one stored it generates a new one.

What I can’t seem to figure out is how to allocate and initialize my mutable array to use with my class method. I’ve tried allocating it in the class method. but that won’t work because it will get initialized on each call therefor the previously stored values are… erased?

then I want to use the generated position to place a number of circles on the view (without them overlapping). right now the randomly generated positions work, but they overlap because of the mutable array not being implemented correctly in my randomposition class.

I’m still a beginner in objective c…and programming in general…

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

here’s the method I use in my viewcontroller. I also store the circle object that is placed on the view in a separate array for other manipulations.

- (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];

    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:25:12+00:00Added an answer on June 1, 2026 at 7:25 pm

    Before learning Obj-C, you should probably learn something about 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];
      }
    
    }
    

    or

    
    
    //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
    
    

    Note that implementing this functionality using class methods is probably not a very good architecture.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.