如何从代码访问实体 - 游戏工具包
how to access entity from code - 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 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 函数中,它调用组件 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。我假设当你向编辑器添加一个节点时,它也会为该节点设置实体,所以我不确定为什么它是零。想知道是否有人可以对此有所了解?
感谢您的帮助!
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
我在场景中添加了一个节点。同样在场景中,我给了它一个反弹的组件。该组件如下所示:
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 函数中,它调用组件 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。我假设当你向编辑器添加一个节点时,它也会为该节点设置实体,所以我不确定为什么它是零。想知道是否有人可以对此有所了解?
感谢您的帮助!
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