Objective c 精灵套件中的形状
如何在精灵套件中制作圆形或其他形状我见过一些使用CGPath的,但我从未真正使用过CGPath。我可以用SKSpritenode创建一个正方形,但我似乎无法创建三角形或圆。这将创建一个SKShapeNode,并将其路径属性设置为半径为16的圆路径Objective c 精灵套件中的形状,objective-c,sprite-kit,Objective C,Sprite Kit,如何在精灵套件中制作圆形或其他形状我见过一些使用CGPath的,但我从未真正使用过CGPath。我可以用SKSpritenode创建一个正方形,但我似乎无法创建三角形或圆。这将创建一个SKShapeNode,并将其路径属性设置为半径为16的圆路径 SKShapeNode *shape = [SKShapeNode node]; CGRect rect = CGRectMake(0, 0, 32, 32); shape.path = [self circleInRect:r
SKShapeNode *shape = [SKShapeNode node];
CGRect rect = CGRectMake(0, 0, 32, 32);
shape.path = [self circleInRect:rect];
shape.strokeColor = [SKColor greenColor];
shape.fillColor = [SKColor redColor];
shape.position = CGPointMake(100,100);
[self addChild:shape];
此方法返回用椭圆路径初始化的CGPath对象
- (CGPathRef) circleInRect:(CGRect)rect
{
// Adjust position so path is centered in shape
CGRect adjustedRect = CGRectMake(rect.origin.x-rect.size.width/2, rect.origin.y-rect.size.height/2, rect.size.width, rect.size.height);
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:adjustedRect];
return bezierPath.CGPath;
}
这是一条三角形的路径
- (CGPathRef) triangleInRect:(CGRect)rect
{
CGFloat offsetX = CGRectGetMidX(rect);
CGFloat offsetY = CGRectGetMidY(rect);
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(offsetX, 0)];
[bezierPath addLineToPoint: CGPointMake(-offsetX, offsetY)];
[bezierPath addLineToPoint: CGPointMake(-offsetX, -offsetY)];
[bezierPath closePath];
return bezierPath.CGPath;
}
使用Sprite工具包创建三角形的代码段
SKShapeNode* leftContainer = [SKShapeNode node];
leftContainer.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
CGMutablePathRef pathLeftContainer = CGPathCreateMutable();
// Draw circle with radius 20
CGPathAddArc(pathLeftContainer, NULL, 0, 20, 20, 0, 2*M_PI, true);
[leftContainer setPath:pathLeftContainer];
leftContainer.lineWidth = 10.0;
leftContainer.strokeColor = [UIColor redColor];
leftContainer.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:leftContainer.path];
leftContainer.physicsBody.dynamic = FALSE;
[leftContainer.physicsBody setAllowsRotation:FALSE];
[self addChild:leftContainer];
CGPathRelease(pathLeftContainer);
使用Sprite工具包创建圆的代码段
SKShapeNode* leftContainer = [SKShapeNode node];
leftContainer.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
CGMutablePathRef pathLeftContainer = CGPathCreateMutable();
// Draw circle with radius 20
CGPathAddArc(pathLeftContainer, NULL, 0, 20, 20, 0, 2*M_PI, true);
[leftContainer setPath:pathLeftContainer];
leftContainer.lineWidth = 10.0;
leftContainer.strokeColor = [UIColor redColor];
leftContainer.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:leftContainer.path];
leftContainer.physicsBody.dynamic = FALSE;
[leftContainer.physicsBody setAllowsRotation:FALSE];
[self addChild:leftContainer];
CGPathRelease(pathLeftContainer);
三角形呢?您可以通过moveToPoint和addLineToPoint调用的组合来形成三角形。我真的不知道如何使用moveToPoint和addLineToPoint。当我这样做时,三角形是倾斜的。