1

Stuck here trying to call an endGame() function in a genericnode class from my GameScene.swift class. When I try to acess it by calling GameScene().endGame() I get the error:

Cannot invoke initializer for type 'GameScene' with no arguments

GameScene:

class GameScene: SKScene, SKPhysicsContactDelegate {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

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

    func endGame() {
        gameOver = true
        scoreboardSetup()
    }
}

carNode.swift

enum CarType:Int {
    case normalCar = 0
}

class carNode: GenericNode {
    var carType:carType!

    override func collisionWithPlayer(player: SKNode) -> Bool {
        GameScene().endGame() //Gives me an error

        return false
    }
}
NowYouKnow
  • 67
  • 9
  • Either you need to pass in a `CGSize` during initializing the `GameScene` like `GameScene(size: CGSize(width: 0, height: 0).endGame()` or alternatively you can make the `endGame()` method static. `static endGame()` and than call it like `GameScene.endGame()` Hope this helps. – riik Apr 29 '16 at 21:55
  • None of these solutions work. For the `GameScene(size: CGSize(width: 0, height: 0).endGame()` i get _Fatal error: unexpectedly found nil while unwrapping an Optional values_. and for the `static endGame()` i get error _Consecutive declarations on a line must be separated by ';'_ – NowYouKnow Apr 29 '16 at 22:13
  • No need for all this. You should handle a collision directly in a scene and run endGame() from there. This method does nothing except than ending a game, so it doesn't make sense to keep it as a part of a CarNode class. So, what I said, would be an easiest way for you. Otherwise, you can accomplish this using delegation (where scene would act as a delegate of CarNode class). – Whirlwind Apr 29 '16 at 22:14
  • Never heard of that way, will read about it. Could you show an example on how to proceed with delegation – NowYouKnow Apr 29 '16 at 22:20
  • I can write an example for you, but that is not what you need. Why do you want to call `endGame()` defined in GameScene, from within `CarNode` class. Can you explain that? – Whirlwind Apr 29 '16 at 22:24
  • Here is an example of how you can implement a delegation pattern : http://stackoverflow.com/a/36524132/3402095. Still, that is not what you need. You can run endGame() directly in a current scene inside of its `didBeginContact` or where ever you handle contacts. – Whirlwind Apr 29 '16 at 22:29
  • Every character(player, car and tree) is created in a seperate class that is called GameElements, each one in a function that returns types that is later used in the carNode, treeNode etc classes. Their functions are called in GameScene and gets created to the scene. The thing is, each one of these elements could only be called in GameScene by their names(enumratechildnodeswithname). As a beginner when it comes to programming i figured out that the easiest way to end the game when the car collides with the player is to call the function in the carNode class – NowYouKnow Apr 29 '16 at 22:33
  • I am just saying that you are complicating things. When physics world send a notification to a scene, and invoke didBeginContact, the `endGame()` should be executed. And that's it. Your current setup will not work because you are creating a new instance of a GameScene inside of a CarNode method. So it will not work like that even if you get rid of error. – Whirlwind Apr 29 '16 at 22:50
  • 1
    @riik Type methods (static) will not work in this case. You call type methods on the type, not on an instance. endGame() obviously ends game...Means, it resets the scene, remove nodes, shows score, makes transition to another scene, stuff like that... So for example, something like self.view.presentScene() will not be possible, etc, etc. – Whirlwind Apr 29 '16 at 22:57
  • Safe to say, you are right. Just got it working. Here is the code for the did begin contact `var otherNode:SKNode! if contact.bodyA.node != player { otherNode = contact.bodyA.node endGame() } else { otherNode = contact.bodyB.node } (otherNode as! GenericNode).collisionWithPlayer(player)` Thank you for the help! – NowYouKnow Apr 29 '16 at 22:59

0 Answers0