如何使用自定义 GKComponent 创建 SKSpriteNode?

How to create a SKSpriteNode with Custom GKComponent?

所以我目前正在与 spriteKitgameplayKit 一起开发一款游戏。我创建了一个自定义 GKComponent class,我通过 Xcode 中的场景检查器将其提供给某些 SKSpriteNode(图片)。现在,当我想用​​相同的 GKComponent 重新创建随机 SKSpritenodes 时,它似乎不起作用,因为我的自定义 GKComponent 的覆盖更新方法不起作用。有什么线索吗?

我试图给节点一个新的实体对象(因为它返回 nil)并且我给了那个实体我的自定义 GKComponent 但没有任何效果

// In scene                
let node = SKSpriteNode(texture: "coin", color: UIColor.clear, size: CGSize(width: 60, height: 60))
let entities: GKEntity! = GKEntity()
let component: CollectableComponent = CollectableComponent()
component.itemType = 1

node.anchorPoint = CGPoint(x: 0.5, y: 0.5)
node.zPosition = 6
node.setScale(0.5)
node.size = CGSize(width: 60, height: 60)
node.position = CGPoint(x: 90, y: 90)
entities.addComponent(component)
node.entity = entities
self.scene?.addChild(node)


// GKComponent

class CustomComponent: GKComponent {
    var node: SKSpriteNode?

    @GKInspectable var itemType: Int = 0
    @GKInspectable var isStatic: Bool = false

    override init() {
        super.init()
    }

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

    override func update(deltaTime seconds: TimeInterval) {
        if node == nil {
            if let nodeComponent = self.entity?.component(ofType: GKSKNodeComponent.self){
                node = nodeComponent.node as? SKSpriteNode
            }
        }
    }
}

期望的行为:如果 CustomComponent 中的 nodeComponent 变量不为零,我想让它 运行 像这样的动画:

node = nodeComponent.node as? SKSpriteNode; node.run(SKAction.moveBy(x: 0, y: 15, duration: 0.3));

但是有了代码,动作就不会 运行ning

这就是您想要的方式。

请注意,您需要保留您的实体,因此需要一个全局变量。

此外,当您将节点添加到 GKSKNodeComponent 时,会自动设置 node.entity 变量。

class GameScene : SKScene{
    var entities: [GKEntity] = [GKEntity]()

    func doSomething(){


        let node = SKSpriteNode(texture: "coin", color: UIColor.clear, size: CGSize(width: 60, height: 60))
        node.anchorPoint = CGPoint(x: 0.5, y: 0.5)
node.zPosition = 6
        node.setScale(0.5) //<--- YUCK!!!!!
        node.size = CGSize(width: 60, height: 60)
        node.position = CGPoint(x: 90, y: 90)
        self.scene?.addChild(node)

        let entity = GKEntity()
        let nodeComponent : GKSKNodeComponent = GKSKNodeComponent(node:node)
        let component: CollectableComponent = CollectableComponent()
        component.itemType = 1
        entity.addComponent(nodeComponent)
        entity.addComponent(component)
        entities.append(entity)

    }
}

// GKComponent

class CustomComponent: GKComponent {
    //We want this line to crash if node is empty, so do not use ?
    lazy var node: SKSpriteNode! = {return self.entity!.component(ofType: GKSKNodeComponent.self).node as? SKSpriteNode! }

    @GKInspectable var itemType: Int = 0
    @GKInspectable var isStatic: Bool = false

    override init() {
        super.init()
    }

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

    override func update(deltaTime seconds: TimeInterval) {

    }
}