0

I have several similar SKSpriteNodes that I'd like to arrange in specific patterns on the screen. Specifically I'd like them to arrange as a circle around another central SKSpriteNode.

I'm putting the x,y for each in an array and using setPostion:CGPointMake(x,y) to place on screen:

NSInteger DotX_Pos[] = {50,200,350,500};

NSInteger DotY_Pos[] = {50,200,350,500};

[sprite setPosition:CGPointMake(DotX_Pos[i],DotY_Pos[i])];

While this works, it's not efficient plus I'd like to arrange more and more sprites as the game level advances.

Any good ideas how to get this done?

thanks, rich

erkanyildiz
  • 13,044
  • 6
  • 50
  • 73
user2887097
  • 309
  • 4
  • 12

1 Answers1

0

Here is a method that will return a collection of NSValues each containing a CGPoint with an appropriate position. You can pass in the radius, the origin x and y and the number of sprites you want, and it will space them out evenly around a circle. To extract the CGPoint from the NSValue* value, use [value CGPointValue]. Good luck.

-(NSArray*)getPositionsForCircleWithRadius:(uint)radius originXPos:(int)originXPos originYPos:(int)originYPos andNumberOfItems:(uint)numberOfItems
{
    NSMutableArray* positions = [[NSMutableArray alloc]initWithCapacity:numberOfItems];
    CGFloat angle = 0;
    CGFloat angleIncrement = 2*M_PI/numberOfItems;

    for (int i = 0; i < numberOfItems; i++)
    {
        int pointX = originXPos + radius*cos(angle);
        int pointY = originYPos + radius*sin(angle);

        CGPoint position = CGPointMake(pointX, pointY);

        [positions addObject:[NSValue valueWithCGPoint:position]];

        angle += angleIncrement;
    }

    return positions;
}
Bokoskokos
  • 512
  • 4
  • 13