精灵套件中的形状

时间:2014-08-01 22:39:57

标签: objective-c sprite-kit

你如何在精灵套件中制作一个圆形或其他形状我见过一些使用CGPath但我还没有真正使用过CGPath。我能够用SKSpritenode制作一个正方形,但我似乎无法制作三角形或圆形。

2 个答案:

答案 0 :(得分:4)

这将创建一个SKShapeNode,并将其path属性设置为半径为16的圆路径。

    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;
}

答案 1 :(得分:2)

使用Sprite Kit创建Triangle的代码片段


SKShapeNode* leftContainer = [SKShapeNode node];
leftContainer.position = CGPointMake(CGRectGetMidX(self.frame),
                                     CGRectGetMidY(self.frame));
CGMutablePathRef pathLeftContainer = CGPathCreateMutable();


// Move to position and Draws line to form a triangle
CGPathMoveToPoint(pathLeftContainer, NULL, 60, 100);
CGPathAddLineToPoint(pathLeftContainer, 0, 120, 160);
CGPathAddLineToPoint(pathLeftContainer, 0,180,100);
CGPathAddLineToPoint(pathLeftContainer, 0,60,100);


[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 Kit创建Circle的代码片段

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);