在spritekit场景编辑器中使用自定义SKNode

时间:2023-01-22 21:30:52

I want to create a level using my custom subclasses of SKNode. I tried adding an SKNode to the scene editor and using the "Custom Class" tab give the class that I want it to be but that did absolutely nothing. The node would still be empty and nothing would show when I run the simulator. Also, to make sure that the class actually works, I added an instance of it to the scene programmatically to see if it shows and it does.

我想使用我的自定义SKNode子类创建一个级别。我尝试将SKNode添加到场景编辑器中,并使用“自定义类”选项卡给出我想要的类,但绝对没有。该节点仍然是空的,当我运行模拟器时没有任何显示。此外,为了确保该类实际工作,我以编程方式将其实例添加到场景中以查看它是否显示并且确实如此。

How do I add my custom nodes to the scene through the scene editor?

如何通过场景编辑器将自定义节点添加到场景中?

Here's my custom class code:

这是我的自定义类代码:

class Player: SKSpriteNode {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        print("Test")
        self.addChild(SKSpriteNode(imageNamed: "Player.png"))
    }
}

2 个解决方案

#1


6  

There are two things you need to do:

您需要做两件事:

1) When setting the Custom Class you have to prefix the class name with your app's name; My_App_Name.MyClass for example, where _ represents a space.

1)设置自定义类时,您必须在类名前加上应用程序的名称;例如,My_App_Name.MyClass,其中_表示空格。

2) Your SKNode subclass needs to implement required init?(coder aDecoder: NSCoder).

2)您的SKNode子类需要实现所需的init?(编码器aDecoder:NSCoder)。


For example, in my project called 'MyGame':

例如,在我的名为“MyGame”的项目中:

在spritekit场景编辑器中使用自定义SKNode

class MyNode: SKSpriteNode {
    // Set this after the node has been initaliser by init(coder:)
    var someStat: Int = 0

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // To check it worked:
        print("Yup, all working")
    }
}

#2


0  

I had the same problem in Xcode 8. It sounds stupid, but to apply a custom class I had to save the .sks file before running.

我在Xcode 8中遇到了同样的问题。这听起来很愚蠢,但是要应用自定义类,我必须在运行之前保存.sks文件。

.sks files are not saved automatically, unlike text files or storyboards.

与文本文件或故事板不同,.sks文件不会自动保存。

#1


6  

There are two things you need to do:

您需要做两件事:

1) When setting the Custom Class you have to prefix the class name with your app's name; My_App_Name.MyClass for example, where _ represents a space.

1)设置自定义类时,您必须在类名前加上应用程序的名称;例如,My_App_Name.MyClass,其中_表示空格。

2) Your SKNode subclass needs to implement required init?(coder aDecoder: NSCoder).

2)您的SKNode子类需要实现所需的init?(编码器aDecoder:NSCoder)。


For example, in my project called 'MyGame':

例如,在我的名为“MyGame”的项目中:

在spritekit场景编辑器中使用自定义SKNode

class MyNode: SKSpriteNode {
    // Set this after the node has been initaliser by init(coder:)
    var someStat: Int = 0

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // To check it worked:
        print("Yup, all working")
    }
}

#2


0  

I had the same problem in Xcode 8. It sounds stupid, but to apply a custom class I had to save the .sks file before running.

我在Xcode 8中遇到了同样的问题。这听起来很愚蠢,但是要应用自定义类,我必须在运行之前保存.sks文件。

.sks files are not saved automatically, unlike text files or storyboards.

与文本文件或故事板不同,.sks文件不会自动保存。