在 SpriteKit 中,SKCropNode 对 SKShapeNode 没有影响 [英] In SpriteKit, SKCropNode has no effect over a SKShapeNode
问题描述
我一直在尝试使用 SKCropNode 将蒙版应用于 SKShapeNode,但到目前为止没有成功.认为这是 SpriteKit 错误 - 这是代码片段:
I've been trying to apply a mask to a SKShapeNode using SKCropNode, and so far without success. Thinking that it's a SpriteKit bug - Here is the code snippet:
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));
有人对此问题有解决方法吗?非常感谢!
Does anyone have a workaround about this issue? Thanks a lot!
推荐答案
这一直困扰着我一天的大部分时间.我曾计划创建一个类似于 Tony 出色的 TCProgressTimer 的计时器钱布利.但是,由于我的应用程序使用多个进度计时器,我不想设计几十个不同大小的精灵以用于不同的分辨率.
This had been bugging me for most of the day. I had planned to create a timer similar to the excellent TCProgressTimer by Tony Chamblee. However, as my application uses multiple progress timers I didn't want to have to design dozens of different sized sprites for use at different resolutions.
我的解决方案是将 SKShapeNode
对象转换为 SKSpriteNode
对象.我最终不得不回到基础并使用 Core Graphics 来完成繁重的工作.我敢肯定,这是一种相当混乱的做事方式,但我想要快速的结果来动态创建类似于使用 SKShapeNode
时获得的结果的对象.
My solution was to convert SKShapeNode
objects to SKSpriteNode
objects. I ended up having to go back to basics and use Core Graphics to do the heavy lifting. This is a rather messy way of doing things, I'm sure, but I wanted quick results to dynamically create objects that would resemble the results obtained when using SKShapeNode
.
我目前只对制作圆形物体感兴趣,所以我是这样做的:
I am only interested in making circle objects at present, so I did it like this:
-(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;
}
生成的精灵现在可以按预期被 SKCropNode
屏蔽.由于这些精灵都是在场景开始之前生成的,因此我没有注意到性能下降.但是,如果您动态生成多个节点,我想这种方法效率非常低.
The resulting sprite can now be masked by an SKCropNode
as expected. As these sprites are all generated before the scene begins, I do not notice a performance hit. However, I would imagine this method is highly inefficient if you are generating multiple nodes on the fly.
我很想听听其他用户的解决方案.希望对您有所帮助.
I would be eager to hear solutions from other users. Hope that helps.
-DC
这篇关于在 SpriteKit 中,SKCropNode 对 SKShapeNode 没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!