2017-09-30 18 views
1

나는 장면에 추가 한 노드가 있습니다. 또한 장면에서 나는 튀어 오르는 구성 요소를 제공합니다. 구성 요소는 다음과 같습니다.코드에서 엔티티에 액세스하는 방법 - 게임 플레이

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() 구성 요소를 호출합니다. 이것은 잘 작동합니다. 내가 클릭하면

if (play?.contains(pos))! { 
     print("test") 
     if let _ = play?.entity { 
      print("we have an entity") 
     } 
     play?.entity?.component(ofType: BounceComponent.self)?.performBounce() 
    } 

, 그것은 인쇄 유일한 것은 "테스트"하고 기업이 전무는 ... 내가 버튼을 클릭하면 함수 performBounce()를 호출하기 위해 노력하고있어. 편집기에 노드를 추가 할 때 그 노드에 대한 엔티티도 설정한다는 가정하에, 왜 이것이 nil인지 확실하지 않습니다. 누군가가 이것에 대해 밝힐 수 있다면 궁금한가요?

도움 주셔서 감사합니다.

+0

놀이는 무엇인가에서 엔티티를 유지해야, 약한 참조입니다? 재생할 구성 요소를 추가 했습니까? – Knight0fDragon

+0

Play는 장면 편집기에서 추가 한 단추이며 편집기에 구성 요소를 추가했습니다. 이 if 문은 내 터치 다운 기능 내에 있습니다. 버튼에 대한 하위 클래스가 없습니다. 재생은 단지 변수입니다. 장면의 버튼에 대한 참조를 저장합니다. – Discoveringmypath

+0

코드에서 이전에 보낸 내용입니까? 좀 더 교묘해질 수 있기 때문에 더 많이 볼 필요가있다. – Knight0fDragon

답변

1

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 
+0

그게 중요했다 ... 나는 그것을 필요 없다고 생각한 것을 삭제했다. – Discoveringmypath