简体   繁体   English

如何使用gameplaykit在编辑器中向精灵添加组件

[英]How to add components to sprites within editor with gameplaykit

I have a component:我有一个组件:

class Test: GKComponent {
    override init() {
        super.init()
        print("init ran")
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        print("coder init ran")
    }
}

I also have a subclass:我也有一个子类:

class Testing: SKSpriteNode {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

    }
}

So in my scene editor, I have a sprite.所以在我的场景编辑器中,我有一个精灵。 In the editor I subclass it to Testing.在编辑器中,我将其子类化为测试。

Also in the scene editor I add the component Test.同样在场景编辑器中,我添加了组件 Test。

When I run the project, it runs fine, and it prints both print statements in the Component subclass.当我运行该项目时,它运行良好,并且在 Component 子类中打印了两个打印语句。

  1. Why does it use both init functions and how do I get it to use only 1?为什么它同时使用两个 init 函数,我如何让它只使用 1 个?

  2. How would I add something in this component like an action or something... Say I want the sprite to rotate for example?我如何在这个组件中添加一些东西,比如一个动作或其他东西……比如说我想让精灵旋转? How would I do this?我该怎么做?

Thank you for any help...感谢您的任何帮助...

1) I guess you do not understand what a component is, A component is not the same thing as an SKSpriteNode , in fact they are 2 completely different things with almost no relationship to each other. 1)我猜你不明白组件是什么,组件与SKSpriteNode不是一回事,实际上它们是两个完全不同的东西,彼此几乎没有关系。 When you place a node on the scene with the scene builder, behind the scenes it makes an instance of GKEntity and GKSKComponent automatically.当您使用场景构建器在场景上放置节点时,它会在幕后自动GKEntityGKSKComponent的实例。 GKSKComponent is the component that holds the SKSpriteNode . GKSKComponent是保存SKSpriteNode的组件。 Your Test component is a component that does something else.您的Test组件是一个执行其他操作的组件。 Everything is compartmentalized.一切都是分门别类的。 Best example, think of a bicycle.最好的例子,想想一辆自行车。 The wheels are a component, the chain is a component, the brakes are a component, each that serve a very unique purpose.车轮是一个组件,链条是一个组件,制动器是一个组件,每个都有一个非常独特的目的。 You need to create (init) all of these objects to get a bicycle.您需要创建(初始化)所有这些对象才能获得自行车。

2) To get other components to work with your SKSpriteNode , you need to get to the entity. 2) 要让其他组件与您的SKSpriteNode一起工作,您需要访问实体。 The component should have access to it, so you just need to do self.entity .组件应该可以访问它,所以你只需要做self.entity Then you need to find the GKSKComponent , which will have a link to the node you are looking for so that you may apply actions to it.然后您需要找到GKSKComponent ,它将包含指向您要查找的节点的链接,以便您可以对其应用操作。

Here is my class I have made to make it easier for Components to access the node这是我为了让组件更容易访问节点而制作的课程

import GameplayKit

class NodeComponent : GKComponent
{
    public private(set) lazy var node : SKNode = self.entity!.component(ofType: GKSKNodeComponent.self)!.node

    func resetNode(to node:SKNode)
    {
        self.node = node;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM