It bothered me most of the day. I planned to create a timer similar to the excellent TCProgressTimer from Tony Chamblee . However, since my application uses several progress timers, I did not want to create dozens of different sprites of different sizes for use with different resolutions.
My solution was to convert SKShapeNode objects to SKSpriteNode objects. I had to go back to the basics and use Core Graphics for hard work. This is a pretty dirty way to do something, I'm sure, but I wanted fast results to dynamically create objects that resembled the results obtained using SKShapeNode .
I'm only interested in creating circle objects, 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; }
The resulting sprite can now be masked using SKCropNode , as expected. Since these sprites are generated before the start of the scene, I do not notice a performance hit. However, I would suggest that this method is very inefficient if you create multiple nodes on the fly.
I would really like to hear decisions from other users. Hope this helps.
-DC
Doctorclod
source share