Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking the SKS file to a Custom Class in SpriteKit

Tags:

I'm new to Swift and I am trying to code a simple game using SpriteKit.

I'm trying to create a main menu page for the 'start' button.

I made a .sks file and .swift (cocoa touch class) files with the same name.

I designed the .sks as I want it to be, but the cocoa touch class is not working.

.swift code:

import SpriteKit

class MenuScene: SKScene {

    var mStartBtn: SKSpriteNode = SKSpriteNode()
    var mLabel: SKLabelNode = SKLabelNode()

    override func didMove(to view: SKView) {
        mStartBtn = self.childNode(withName: "startBtn") as! SKSpriteNode
        mLabel = self.childNode(withName: "label") as! SKLabelNode
        mLabel.text = "Hello.."
    }
}

I tried to change the label on the screen to see if its working, but it won't change, as if the .swift file is not working/connected to the .sks file.

like image 998
swiftnewbie Avatar asked Dec 24 '16 13:12

swiftnewbie


1 Answers

As you suggested, the SKS file is probably not linked to the swift file.

In Xcode

  • open the sks file
  • select your scene
  • open the right side panel
  • open the Custom Class Inspector
  • type MenuScene into the Custom Class field

enter image description here

Then try again.

like image 172
Luca Angeletti Avatar answered Sep 26 '22 17:09

Luca Angeletti