UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SwiftUI with SpriteKit in an AUv3 - Using the SwiftUI view to control the SKScene

Forums > SwiftUI

I am building an AUv3 plug in. I have got it to work with a basic SwiftUI view so that can in turn display a SKScene. Now I am trying to combine the GUI. I have a basic button in the SwiftUI view and the SKScene displaying happily.

I want that button to add a SKShapeNode to my scene. The fuction I have built here builds a simple circle node. If I run the function from the didMovetoView in the simplest way then it works fine.

When I run the same function from the Button code within the SwiftUI... no node appears...the function obviously runs as my "print" functions appear in the debug window.

Not sure why this won't work?

My code for the SwiftUI:

struct Content: View {

    var scene: SKScene {
        let scene = GameScene()
        scene.size = CGSize(width: 400, height: 400)
        scene.scaleMode = .fill
        scene.backgroundColor = .black

        return scene
    }

    var body: some View {

        Button("Makeball") {
            print("make ball")
            let makeBall = GameScene()
            makeBall.createBallBloq()

        }

        VStack {
            SpriteView(scene:scene)
                .frame(width:400, height: 400)

        }
    }
}

and my code in the SKScene or GameScene...

var ballBloqArray :[SKShapeNode] = [SKShapeNode]()
var ballNumber = -1

class GameScene: SKScene, UIGestureRecognizerDelegate {

    var ballSize = 50
    //var ballBloqArray :[SKShapeNode] = [SKShapeNode]()
    //var ballNumber = -1

    override func didMove(to view: SKView) {

        physicsBody = SKPhysicsBody(edgeLoopFrom: frame)

    }

    func createBallBloq() {

            let ball = SKShapeNode(circleOfRadius: 20)
            ball.zPosition = 10
            ball.fillColor = UIColor.lightGray.withAlphaComponent(0.7)
            ball.strokeColor = .orange
            ball.glowWidth = 1
            ball.physicsBody = SKPhysicsBody(circleOfRadius: 20)
            ball.physicsBody?.friction = 0
            ball.physicsBody?.affectedByGravity = false
            ball.physicsBody?.allowsRotation = false
            ball.physicsBody?.isDynamic = true
            ball.physicsBody?.pinned = false
            ball.physicsBody?.linearDamping = 0
            ball.physicsBody?.angularDamping = 0
            ball.physicsBody?.angularVelocity = 0
            ball.physicsBody?.restitution = 1
            ball.physicsBody?.mass = 1
            ballBloqArray.append(ball)
            ballNumber = ballNumber + 1
            ballBloqArray[ballNumber].position = CGPoint(x: 50, y: 100)
            self.addChild(ballBloqArray[ballNumber])

            //ballBloqArray[i].physicsBody!.categoryBitMask = PhysicsCategories.DeadBallCategoryMask
            //ballBloqArray[i].physicsBody!.collisionBitMask = PhysicsCategories.DeadBallCategoryMask

            print("building ball")
            print("number of balls \(ballBloqArray.count)")
    }

}

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.