Search code examples
swiftfunctionclassskphysicsbody

Object does not spawn, when making object from another class


So, its pretty much like this: I have this tap gesture recognizer that works, heres the code in the GameViewController Class:

@IBAction func handleTap(_ sender: UITapGestureRecognizer) 
{
    GameScene().makeCirc();
}

And the function I am calling is in the GameScene Class, it looks like this:

public func makeCirc() {

    circle = SKShapeNode (circleOfRadius: 15)
    circle.name = "white circ"
    circle.position = CGPoint (x: 0, y: 10)
    circle.fillColor = .white
    circle.physicsBody = SKPhysicsBody(circleOfRadius: 15)

    circle.physicsBody?.isDynamic = true
    circle.physicsBody?.affectedByGravity = false
    circle.physicsBody?.linearDamping = 0
    circle.physicsBody?.restitution = 1.0
    circle.physicsBody?.allowsRotation = true
    circle.physicsBody?.mass = 300
    circle.physicsBody?.density = 2*3.14*15/(circle.physicsBody?.mass)!
    circle.physicsBody?.angularDamping = 0.0

    self.addChild(circle)
    circle.physicsBody?.applyImpulse(CGVector(dx: 0.0, dy:1.0))
    print(circCount)
    circCount += 1
}

I also have a timer on the updating function so that every little while, a ball is spawned. A ball must also spawn when I tap the screen. Here is the code:

override func update(_ currentTime: TimeInterval)
{
    timer += 1
    if timer > spawntime {
        makeCirc()
        timer = 0
    }
    if circCount > 0 {
        if (self.childNode(withName: "white circ")?.position.y)! > self.frame.maxY - 100 {
            self.childNode(withName: "white circ")?.removeFromParent()
            circCount -= 1
        }
    }
}

The output however, is a bit odd. Without tapping, the output is the circCount, which is usually a constant 12, which is what it should be. However, when I tap, the circCount displayed is 0, instead of spawning the ball and making circCount 13+, however, all objects are still on the screen.

Is there anyway to spawn the ball from another class without a circCount of 0? Thank You!


EDIT: Here is viewDidMove function, if it helps:

override func viewDidLoad() {
    super.viewDidLoad()
    // Load 'GameScene.sks' as a GKScene. This provides gameplay related content
    // including entities and graphs.
    self.view.isUserInteractionEnabled = true
    let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(handleTap(_:)))
    self.view.addGestureRecognizer(tapGesture)
    if let scene = GKScene(fileNamed: "GameScene") {

        // Get the SKScene from the loaded GKScene
        if let sceneNode = scene.rootNode as! GameScene? {
            // Set the scale mode to scale to fit the window
            sceneNode.scaleMode = .aspectFill

            // Present the scene
            if let view = self.view as! SKView? {
                view.presentScene(sceneNode)

                view.ignoresSiblingOrder = true

                view.showsFPS = true
                view.showsNodeCount = true
            }
        }

    }
}

Solution

  • Alright, so I got it all working after doing what I did below. Thank you for your help!

    let scene = GKScene (fileNamed: "GameScene")
    var instance = GameScene()
    override func viewDidLoad() {
        super.viewDidLoad()
        let sceneNode = scene?.rootNode as! GameScene?
        // Load 'GameScene.sks' as a GKScene. This provides gameplay related content
        // including entities and graphs.
    
        let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(handleTap(_:)))
        self.view.addGestureRecognizer(tapGesture)
        self.view.isUserInteractionEnabled = true
            // Get the SKScene from the loaded GKScene
            // Set the scale mode to scale to fit the window
                sceneNode?.scaleMode = .aspectFill
                instance = (sceneNode)!
                // Present the scene
                if let view = self.view as! SKView? {
                    view.presentScene(sceneNode)
                    view.ignoresSiblingOrder = true
                    view.showsFPS = true
                    view.showsNodeCount = true
    
    
        }
    }