Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何从代码-游戏工具包访问实体_Swift_Xcode_Sprite Kit_Skaction_Gameplay Kit - Fatal编程技术网

Swift 如何从代码-游戏工具包访问实体

Swift 如何从代码-游戏工具包访问实体,swift,xcode,sprite-kit,skaction,gameplay-kit,Swift,Xcode,Sprite Kit,Skaction,Gameplay Kit,我在场景中添加了一个节点。同样在场景中,我给它一个要反弹的组件。该组件如下所示: class BounceComponent: GKComponent, ComponentSetupProt { var bounce: SKAction? var node: SKSpriteNode? @GKInspectable var diff: CGFloat = 0.2 @GKInspectable var startScale: CGFloat = 1 @GKInspectable var dur

我在场景中添加了一个节点。同样在场景中,我给它一个要反弹的组件。该组件如下所示:

class BounceComponent: GKComponent, ComponentSetupProt {

var bounce: SKAction?
var node: SKSpriteNode?

@GKInspectable var diff: CGFloat = 0.2
@GKInspectable var startScale: CGFloat = 1
@GKInspectable var duration: TimeInterval = 0.5

override init() {
    super.init()
    comps.append(self)
}

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

func setup() {
    print("setup")
    node = entity?.component(ofType: GKSKNodeComponent.self)!.node as? SKSpriteNode
    setupBounce()
}

func setupBounce() {
    ...
}

func performBounce() {
    print("performing")
    node?.run(bounce!)
}
}//
在我的场景文件的didMove函数中,它调用components setup()。这个很好用。我试图在单击按钮时调用函数performBounce()

    if (play?.contains(pos))! {
        print("test")
        if let _ = play?.entity {
            print("we have an entity")
        }
        play?.entity?.component(ofType: BounceComponent.self)?.performBounce()
    }
单击时,它打印的唯一内容是“test”,实体为nil。我的假设是,当您将一个节点添加到编辑器中时,它也会为该节点设置实体,所以我不确定为什么它是nil。好奇有没有人能解释一下


谢谢你的帮助

SKSpriteNode上的实体是弱引用,您需要确保从gameplaykit对象保留实体

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Load 'GameScene.sks' as a GKScene. This provides gameplay related content
        // including entities and graphs.
        if let scene = GKScene(fileNamed: "Main") {

            // Get the SKScene from the loaded GKScene
            if let sceneNode = scene.rootNode as? Main {

                sceneNode.entities = scene.entities // add this

                // Set the scale mode to scale to fit the window
                sceneNode.scaleMode = .aspectFill

                // Present the scene
                if let view = self.view as! SKView? {
                    view.presentScene(sceneNode)
                    view.showsDrawCount = true
                    view.ignoresSiblingOrder = true
                    view.showsFPS = true
                    view.showsNodeCount = true
                }
            }
        }
    }



class Main: Base {
    var entities = [GKEntity]() // add this

什么是游戏?是否添加了要播放的组件?播放是我在场景编辑器中添加的按钮,我在编辑器中添加了组件。此if语句在我的触地功能中。button没有子类,play只是一个变量我在场景中存储了对button的引用这是您之前发送给我的代码吗?我需要看到更多,因为它可能会变得有点棘手它不是。。。我可以寄给你这个该死的,所以这很重要。。。我删除了那个,以为我不需要它。