2015-11-04 55 views
0

我在项目中已经有了一些代码,但是我想稍微改变它。目前,我的代码创建了一个名为_buttonLayer的空节点,然后创建一些按钮添加到它。我的按钮从_buttonLayer的左上角开始,然后一直添加到右下角。Objective-C SpriteKit - 如何制作按钮圈

// Set up button layer. 
_buttonLayer = [SKNode node]; 
_buttonLayer.position = CGPointMake(_centerOfScreen.x - (_buttonSize.width * 1.5) - 5.0, 
            self.size.height - ((self.size.height * 0.375) + (_buttonSize.height) - 50.0) + 5.0); 
[self addChild:_buttonLayer]; 


// Set up button array. 
_buttonArray = [NSMutableArray array]; 


// Add some buttons. 
for (char row = 0; row < 4; row++) { 
    for (char col = 0; col < 4; col++) { 

     // Buttons. 
     SKSpriteNode *button = [SKSpriteNode spriteNodeWithImageNamed:@"LightGreyButton"]; 
     button.name = @"LightGreyButton"; // Initially set it to grey, because they all start off that way. 
     button.size = _buttonSize; 
     button.position = CGPointMake(2.0 + ((button.size.width + 3.0) * col), 
             -(2.0 + ((button.size.height + 3.0) * row))); 
     [_buttonLayer addChild:button]; 
     [_buttonArray addObject:button]; 

    } 
} 

所以我现在需要做的是创建一个圆形的按钮 - 而不是在一个正方形。我想以某种方式将按钮添加到一个圆的轮廓,然后采取整个_buttonLayer并旋转它。当然,我现在的方式是,它在广场的左上角旋转。我想要圆的中间旋转。

我该怎么做呢?

回答

2

更改for循环到这一点:

int numberOfButtons = 4; // The number of buttons you want. 
int radiusOfCircle = button.size.width*4; // The radius of the circle that will contain those buttons. 

for (int i = 0; i < numberOfButtons; i++) { 

    // Buttons. 
    SKSpriteNode *button = [SKSpriteNode spriteNodeWithImageNamed:@"LightGreyButton"]; 
    button.name = @"LightGreyButton"; // Initially set it to grey, because they all start off that way. 
    button.size = _buttonSize; 
    CGFloat angle = 2*M_PI/numberOfButtons * i; 
    button.position = CGPointMake(radiusOfCircle*cos(angle),radiusOfCircle*sin(angle)); 
    [_buttonLayer addChild:button]; 
    [_buttonArray addObject:button]; 
} 

而且,Darvydas建议,如果要绕一个一个sprite某些CGPoint centerOfRotation,设置sprite.anchorPoint = centerOfRotation;

请记住,我已经写了这个没有测试(我现在更多地使用Swift比Objective-C)。但我认为它应该工作......是吗? :)

+0

谢谢@RoberRM!这应该肯定会帮助我。我会尝试一下,让它知道它是否有效。 –

+0

它似乎确实对你有帮助。我很高兴。 :)祝你好运! – RoberRM

0

,使各节点一轮SKSpriteNode而不是你应该使用SKShapeNode

SKShapeNode* button = [SKShapeNode node]; 
[button setPath:CGPathCreateWithRoundedRect(CGRectMake(-50, -50, 100, 100), 50, 50, nil)]; 

制作按钮,在这里填写无形

button.fillColor = [UIColor clearColor]; 

更改轮廓宽度

button.lineWidth = 100 

更多信息: https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKShapeNode_Ref/index.html

要转动你的节点周围中心设置anchorPoint到您的节点中心

+0

感谢您的回复,但您误解了我的问题。我不需要制作一个圆圈的轮廓。我需要在圆形路径上添加一组精灵,而不是我问题中给定代码创建的方形路径。 –

+0

@ChristianKRider ups,你有没有检查过RobertRM的答案?我认为他给了你很好的回答:) – Darvydas

+0

是的,我有! :) –