【问题标题】:Draw a circle without a texture画一个没有纹理的圆
【发布时间】:2014-01-13 21:33:40
【问题描述】:

我正在创建一个小游戏,但在尝试画一个圆圈时遇到了很多问题。我想为 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];

但是我有这个代码来绘制一个圆圈,但是我在尝试将 NSBezierPath 的路径转换为 ​​SKShapeNodeCGPathRef 属性时出错。

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

        SKShapeNode *circle = [SKShapeNode node];
        circle.path = circlePath.bezierPath;    // conversion error

【问题讨论】:

    标签: objective-c macos cocoa sprite-kit


    【解决方案1】:

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

    相反,你会想要这样的东西:

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddEllipseInRect(path, NULL, rect);
    circle.path = path;
    CGPathRelease(path);
    

    【讨论】:

      【解决方案2】:

      使用

      circle.path = circlePath.bezierPath.CGPath;
      

      而不是

      circle.path = circlePath.bezierPath;
      

      我更喜欢在 Sprite Kit 中使用 Bezier Path 而不是 CGMutablePathRef。有时我使用CGMutablePathRef 作为SKPhysicsBody 的路径会导致意外的内存泄漏

      【讨论】:

      • 抱歉,您提出的解决方案为 OSX 抛出错误,OSX 中的 NSBezierPath 类不存在 bezierPath。
      • CGPath 属性仅适用于 UIKit,不适用于 AppKit。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多