
[英]How to make a specific object defy gravity? (SpriteKit / Objective-C)
[英]Objective-C SpriteKit – How to make circle of buttons
我已经在项目中有一些代码,但是我想对其进行一些更改。 当前,我的代码创建了一个名为_buttonLayer
的空节点,然后创建了一些要添加到其中的按钮。 我的按钮从_buttonLayer
左上角_buttonLayer
,然后一直添加到右下角。
// 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
旋转。 当然,按照我现在的方式,它会在正方形的左上角旋转。 我希望圆的中心旋转。
我将如何去做呢?
将您的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所建议的那样,如果您想围绕某个CGPoint centerOfRotation
旋转sprite
,请设置sprite.anchorPoint = centerOfRotation;
。
请记住,我是在没有测试的情况下编写的(并且现在我比Objective-C更习惯于Swift)。 但是我认为它应该起作用...对吗? :)
要使节点SKSpriteNode
圆形而不是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
设置为节点中心
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.