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

时间:2019-07-16 01:00:48

标签: ios swift sprite-kit gameplay-kit

因此,我目前正在使用spriteKitgameplayKit进行游戏。我创建了一个自定义GKComponent类,该类通过Xcode(图像)中的场景检查器提供给某些SKSpriteNode。现在,当我想使用相同的SKSpritenodes重新创建随机GKComponent时,它似乎不起作用,因为自定义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
            }
        }
    }
}

期望的行为:如果nodeComponent中的CustomComponent变量不是nil,我想使其运行如下动画:

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

但使用代码,该操作将无法运行

1 个答案:

答案 0 :(得分:1)

这是您要执行的操作。

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

此外,当您将节点添加到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) {

    }
}