2017-08-29 81 views
0

因此,它非常像這樣的:我有這樣的點觸手勢識別這樣的作品,繼承人在GameViewController類代碼:對象不產卵,從另一個類製作對象時

@IBAction func handleTap(_ sender: UITapGestureRecognizer) 
{ 
    GameScene().makeCirc(); 
} 

和函數我我打電話是在GameScene類,它看起來像這樣:

public func makeCirc() { 

    circle = SKShapeNode (circleOfRadius: 15) 
    circle.name = "white circ" 
    circle.position = CGPoint (x: 0, y: 10) 
    circle.fillColor = .white 
    circle.physicsBody = SKPhysicsBody(circleOfRadius: 15) 

    circle.physicsBody?.isDynamic = true 
    circle.physicsBody?.affectedByGravity = false 
    circle.physicsBody?.linearDamping = 0 
    circle.physicsBody?.restitution = 1.0 
    circle.physicsBody?.allowsRotation = true 
    circle.physicsBody?.mass = 300 
    circle.physicsBody?.density = 2*3.14*15/(circle.physicsBody?.mass)! 
    circle.physicsBody?.angularDamping = 0.0 

    self.addChild(circle) 
    circle.physicsBody?.applyImpulse(CGVector(dx: 0.0, dy:1.0)) 
    print(circCount) 
    circCount += 1 
} 

我也有更新功能的定時器,每過一會兒,球被催生。當我點擊屏幕時,球也必須產生。這裏是代碼:

override func update(_ currentTime: TimeInterval) 
{ 
    timer += 1 
    if timer > spawntime { 
     makeCirc() 
     timer = 0 
    } 
    if circCount > 0 { 
     if (self.childNode(withName: "white circ")?.position.y)! > self.frame.maxY - 100 { 
      self.childNode(withName: "white circ")?.removeFromParent() 
      circCount -= 1 
     } 
    } 
} 

然而,輸出有點奇怪。沒有竊聽,輸出是circCount,它通常是一個常數12,這應該是。但是,當我點擊時,circCount顯示爲0,而不是產生球並使circCount 13+,但是,所有對象仍然在屏幕上。

無論如何從另一個班級產生的球沒有circCount爲0?謝謝!


編輯:這裏是viewDidMove功能,如果它可以幫助:

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Load 'GameScene.sks' as a GKScene. This provides gameplay related content 
    // including entities and graphs. 
    self.view.isUserInteractionEnabled = true 
    let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(handleTap(_:))) 
    self.view.addGestureRecognizer(tapGesture) 
    if let scene = GKScene(fileNamed: "GameScene") { 

     // Get the SKScene from the loaded GKScene 
     if let sceneNode = scene.rootNode as! GameScene? { 
      // 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.ignoresSiblingOrder = true 

       view.showsFPS = true 
       view.showsNodeCount = true 
      } 
     } 

    } 
} 
+1

使用'GameScene()',您正在**創建'GameScene'的另一個實例**。您應該參考當前的屏幕控制器。 – the4kman

+0

我該怎麼做dat? –

+0

將'sceneNode'保存在控制器的實例變量中,然後使用它調用'makeCirc()'。 (或者使用self.view.scene,或許。) –

回答

0

好了,所以我把它都在做什麼,我做了以下工作後。感謝您的幫助!

let scene = GKScene (fileNamed: "GameScene") 
var instance = GameScene() 
override func viewDidLoad() { 
    super.viewDidLoad() 
    let sceneNode = scene?.rootNode as! GameScene? 
    // Load 'GameScene.sks' as a GKScene. This provides gameplay related content 
    // including entities and graphs. 

    let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(handleTap(_:))) 
    self.view.addGestureRecognizer(tapGesture) 
    self.view.isUserInteractionEnabled = true 
     // Get the SKScene from the loaded GKScene 
     // Set the scale mode to scale to fit the window 
      sceneNode?.scaleMode = .aspectFill 
      instance = (sceneNode)! 
      // Present the scene 
      if let view = self.view as! SKView? { 
       view.presentScene(sceneNode) 
       view.ignoresSiblingOrder = true 
       view.showsFPS = true 
       view.showsNodeCount = true 


    } 
}