0

我有几个类似的 SKSpriteNode,我想在屏幕上以特定的模式排列它们。具体来说,我希望它们围绕另一个中心 SKSpriteNode 排列成一个圆圈。

我将每个 x,y 放在一个数组中并setPostion:CGPointMake(x,y)用于放置在屏幕上:

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

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

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

虽然这可行,但效率不高,而且随着游戏水平的提高,我想安排越来越多的精灵。

有什么好主意如何完成这项工作?

谢谢,有钱

4

1 回答 1

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;
}
于 2013-12-17T23:53:18.317 回答