Search code examples
swiftxcodesprite-kitswift-playgroundskscene

ChildNode of custom type is returning Nil on Swift Playground


I have a class with name (Player.swift). then in my (Scene.swift) class, I declared my object like this.

var player: Player?

then in my initializeGame function, I am doing like this

private func initializeGame() {

    player = childNode(withName: "Player") as? Player!;
    //print NIL
    print(player?.name);
    // initializePlayer()is declared in Player.swift
    player?.initializePlayer();
    print(player?.name);
    //Print NIL
    scoreLabel = childNode(withName: "ScoreLabel") as? SKLabelNode!;
    scoreLabel?.text = "0";
}

I made my ios project in Xcode, and its working fine, then i create a Playground project and import all the xcode files in this project, My playground is running properly except the player is not functioning properly. The problem is in associating the Player Node with custom class in .sks file. The name of my Xcode project is "Game play" so I write Game Play in module and Player in custom class, and using that same sks file in Playground causing problem, the name of my playground project is "MyPlayground", so the module name should be same as project name, so i edit the module field to Myplayground, still not working. So guide me how to change that properly.


Solution

  • 1-Open your (.sks) file

    2-Select the Player Node.

    3-Select Show custom class inspector.

    4-Write "MyPlayground_Sources" instead of just MyPlayground.