0 Replies
      Latest reply on Mar 24, 2020 5:10 PM by s_zapo
      s_zapo Level 1 Level 1 (0 points)

        I am developing an app that contains a SpriteKit scene embedded inside a SwiftUI context.

         

        I am trying to render a simple SKShapeNode like this:

         

        import Foundation
        import SpriteKit
        
        
        class UIConnection: SKShapeNode {
        
            override init() {
                super.init()
                self.fillColor = UIColor.clear
                self.strokeColor = UIColor.blue
                self.lineWidth = 0.01
            }
            
            func updatePositions(node1 : CGPoint, node2 : CGPoint) {
                let path = CGMutablePath()
                path.move(to: node1)
                path.addLine(to: CGPoint(x: node1.x, y: node2.y))
                path.addLine(to: CGPoint(x: node2.x, y: node2.y))
                path.addLine(to: CGPoint(x: node2.x, y: node1.y))
                path.addLine(to: node1)
                self.path = path
            }
            
            required init?(coder aDecoder: NSCoder) {
                super.init(coder: aDecoder)
            }
        }
        

        When I am adding that Node inside the standard SpriteKit game project template in XCode (without any SwiftUI things), everything is working fine, I get a non-filled rectangle with a blue outline. The frame.width  and frame.height variables are valid pixel dimensions like 750 x 1334.

         

        let node = UIConnection()
        self.addChild(node)
             
        node.updatePositions(node1: CGPoint(x: frame.midX-frame.width/4, y: frame.midY-frame.height/4), node2: CGPoint(x: frame.midX+frame.width/4, y: frame.midY+frame.height/4))
        

         

        Now I want to embed the SKScene inside a SwiftUI context using this approach:

        https://dev.to/sgchipman/using-spritekit-with-swiftui-ibc

         

        First thing I encounter is the fact that frame.width and frame.height both result in a value of 1.

        Adding the above node to the embedded SKScene results in an extremely blurred rectangle, so no solid line but some weird cloudy object.