【问题标题】:Object does not spawn, when making object from another class从另一个类制作对象时,对象不会产生
【发布时间】:2017-08-29 15:53:32
【问题描述】:

所以,它几乎是这样的:我有这个可以工作的点击手势识别器,这是 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
            }
        }

    }
}

【问题讨论】:

  • 使用GameScene(),您正在创建GameScene 的另一个实例。您应该引用当前屏幕上的控制器。
  • 我该怎么做?
  • sceneNode保存在控制器的实例变量中,然后使用它调用makeCirc()。 (或者使用 self.view.scene,也许。)
  • 好的,所以我在类中创建了一个名为:var instance : SKScene 的变量,然后在函数中创建,instance = sceneNode?​​span>

标签: swift function class skphysicsbody


【解决方案1】:

好的,所以我在完成下面的操作后一切正常。感谢您的帮助!

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


    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2023-03-23
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    相关资源
    最近更新 更多