本文介绍了在 spritekit 场景编辑器中使用自定义 SKNodes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用我自定义的 SKNode 子类创建一个关卡.我尝试在场景编辑器中添加一个 SKNode 并使用自定义类"选项卡给出我想要的类,但这绝对没有任何作用.该节点仍然是空的,当我运行模拟器时不会显示任何内容.此外,为了确保该类确实有效,我以编程方式将它的一个实例添加到场景中,以查看它是否显示以及是否显示.

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.

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

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"))
    }
}

推荐答案

您需要做两件事:

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

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.

2) 你的 SKNode 子类需要实现 required init?(coder aDecoder: NSCoder).

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

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

For example, in my project called 'MyGame':

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")
    }
}

这篇关于在 spritekit 场景编辑器中使用自定义 SKNodes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:47