在SpriteKit中,SKCropNode对SKShapeNode没有影响

pwa*_*ang 9 objective-c sprite-kit

我一直在尝试使用SKCropNode将掩码应用于SKShapeNode,到目前为止还没有成功.认为这是一个SpriteKit错误 - 这是代码片段:

SKNode* contentNode = [SKNode node];

// picture - use an image bigger than 50x50
SKSpriteNode *pictureNode = [SKSpriteNode spriteNodeWithImageNamed:@"tree"];

// triangle
SKShapeNode* triangleNode = [SKShapeNode node];
UIBezierPath* triangleNodeBezierPath = [[UIBezierPath alloc] init];
[triangleNodeBezierPath moveToPoint:CGPointMake(0.0, 0.0)];
[triangleNodeBezierPath addLineToPoint:CGPointMake(0.0, 100.0)];
[triangleNodeBezierPath addLineToPoint:CGPointMake(50.0, 100.0)];
[triangleNodeBezierPath closePath];

triangleNode.path = triangleNodeBezierPath.CGPath;
triangleNode.fillColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1];

// create a mask
SKSpriteNode *mask = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size: CGSizeMake(50, 50)]; //50 by 50 is the size of the mask

// create a SKCropNode
SKCropNode *cropNode = [SKCropNode node];

[cropNode addChild: contentNode];
[cropNode setMaskNode: mask];

[self addChild: cropNode];
[contentNode addChild:pictureNode]; // pictureNode is being cropped
[contentNode addChild:triangleNode]; // triangleNode is not

cropNode.position = CGPointMake( CGRectGetMidX (self.frame), CGRectGetMidY (self.frame));
Run Code Online (Sandbox Code Playgroud)

有没有人有关于这个问题的解决方法?非常感谢!

Doc*_*lod 9

这一天大部分时间一直困扰着我.我曾计划创建一个类似于Tony Chamblee的优秀TCProgressTimer的计时器.但是,由于我的应用程序使用多个进度计时器,我不想设计几十个不同大小的精灵用于不同的分辨率.

我的解决方案是将SKShapeNode对象转换为SKSpriteNode对象.我最终不得不回到基础并使用Core Graphics来完成繁重的任务.这是一种相当混乱的做事方式,我敢肯定,但我希望快速结果动态创建与使用时获得的结果类似的对象SKShapeNode.

我目前只对制作圆形物体感兴趣,所以我这样做:

-(SKSpriteNode *)createSpriteMatchingSKShapeNodeWithRadius:(float)radius color:(SKColor *)color {
    CALayer *drawingLayer = [CALayer layer];
    CALayer *circleLayer = [CALayer layer];
    circleLayer.frame = CGRectMake(0,0,radius*2.0f,radius*2.0f);
    circleLayer.backgroundColor = color.CGColor;
    circleLayer.cornerRadius = circleLayer.frame.size.width/2.0;
    circleLayer.masksToBounds = YES;


    [drawingLayer addSublayer:circleLayer];

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(circleLayer.frame.size.width, circleLayer.frame.size.height), NO, [UIScreen mainScreen].scale);
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), TRUE);
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor clearColor].CGColor);
    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0,0,circleLayer.frame.size.width,circleLayer.frame.size.height));
    [drawingLayer renderInContext: UIGraphicsGetCurrentContext()];

    UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImage:layerImage]];


    return sprite;
}
Run Code Online (Sandbox Code Playgroud)

现在可以SKCropNode按预期屏蔽生成的精灵.由于这些精灵都是在场景开始之前生成的,所以我没有注意到性能受到影响.但是,我想如果你动态生成多个节点,这种方法效率很低.

我很想听听其他用户的解决方案.希望有所帮助.

-DC

  • 叹了口气,我也遇到了这个问题.我就此提交了雷达.非常不幸的是,SKShapeNode不能与SKCropNode一起使用.幸运的是在我的情况下我需要一个矩形,所以我可以使用SKSpriteNode. (3认同)

Dmi*_*kov 5

我的应用程序中有类似的任务.我需要根据用户输入绘制多个不规则形状,然后将它们用作裁剪节点的蒙版.

适合我的解决方案是:

  1. 创建具有所需路径的SKShapeNode

  2. 使用SKView方法从中检索SKTexture textureFromNode:crop:

  3. 从该纹理创建一个SKSpriteNode.

  4. 使用SKSprite节点作为掩码.