画一个没有纹理的圆圈

Jor*_*hez 2 macos cocoa objective-c sprite-kit

我正在创建一个小游戏,我在尝试绘制圆圈时遇到很多问题.我想为osx而不是为iOs做这件事.

使用此代码绘制矩形非常简单.

CGSize posBarraVida;
    posBarraVida.height = 10;
    posBarraVida.width = 100;
    SKSpriteNode* barraVida = [[SKSpriteNode alloc] initWithColor:[SKColor redColor] size:posBarraVida];
    barraVida.position = CGPointMake(self.size.width/8 + 50, self.size.height - 25.0f);
    barraVida.zPosition = 1;
    [self addChild:barraVida];
Run Code Online (Sandbox Code Playgroud)

但我有这个代码画一个圆圈,但我有错误试图将路径转换NSBezierPath为的CGPathRef属性SKShapeNode.

CGRect box = CGRectMake(0, 0, 100, 100);
        NSBezierPath *circlePath = [NSBezierPath bezierPathWithOvalInRect:box];

        SKShapeNode *circle = [SKShapeNode node];
        circle.path = circlePath.bezierPath;    // conversion error
Run Code Online (Sandbox Code Playgroud)

tjw*_*tjw 5

path属性SKShapeNode输入为CGPathRef.这表明您需要使用CGPath对象而不是NSBezierPath对象.在某些情况下,类的Cocoa版本是免费桥接到"核心"版本,但在这种情况下不是.

相反,你想要的东西如下:

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, rect);
circle.path = path;
CGPathRelease(path);
Run Code Online (Sandbox Code Playgroud)