Procedurally generate sprites

Hello,


I would like to generate sprites and graphics on the fly as the application runs. What is the best method to do this?

It looks like this can be done in SpriteKit with SKMutableTexture. So I imagine drawing to an array of bytes then copy into SKMutableTexture?

Are there any other API to help with drawing into SKMutableTexture (splines, lines, circle, polygon shading)?

I'm new to SpriteKit and iOS dev in general so much appreciate if you could point to any documentation regarding above subject. Thank you.

Perhaps SKShapeNode is what you are looking for.

No, SKMutableTexture isn't going to help with this. The easiest way is to draw (using UIBezierPath and other graphics APIs) into a CGContext, then convert the context into an image, then convert the image into a SKTexture.


The code is something like this:


UIGraphicsBeginImageContextWithOptions (size, false, 0)
… draw into the current context …
… use UIGraphicsGetCurrentContext () if you need the current context explcitly …
let contextImage = UIGraphicsGetImageFromCurrentImageContext ()
UIGraphicsEndImageContext ()
let texture = SKTexture (image: contextImage)
return SKSpriteNode (texture: texture, size: size)


Note that specifying 0 as the last parameter in the first line causes a retina-quality image to be drawn (for the current device), and creating the sprite node with an explicit size is necessary to get the logical image size to be respected.

Procedurally generate sprites
 
 
Q