对象不产卵,从另一个类制作对象时

问题描述:

因此,它非常像这样的:我有这样的点触手势识别这样的作品,继承人在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,或许。) –

好了,所以我把它都在做什么,我做了以下工作后。感谢您的帮助!

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 


    } 
}