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)")
}
}