iosswiftpaintcode

How to implement PaintCode project into SKScene


In my app I'd like to implement a circle made with PaintCode (Stylekit) in SKScene.

This is my code:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

  override init(size: CGSize) {
    super.init(size: size)     
    StyleKit.drawCanvas1()
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  override func didMove(to view: SKView) {
  }
}

How can I resolve this problem?


Solution

  • you might build the SKSpriteNode in this way:

    func makeCircle(width:CGFloat = 200, height:CGFloat = 200) -> SKSpriteNode? {
      let size = CGSize(width: width, height: height)
      UIGraphicsBeginImageContextWithOptions(size, false, 0)
      StyleKit.drawCircle(frame: CGRect(origin: .zero, size: size), resizing: .AspectFit)
      let image = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
      if let safeImage = image {
        return SKSpriteNode(texture: SKTexture(image: safeImage))
      } else {
        return nil
      }
    }