精灵未通过SKAction删除

时间:2019-03-10 09:35:02

标签: ios swift sprite-kit skaction

我正在尝试云层向上移动并生成,但是我一直在不断获取

“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'尝试添加已经具有父级的SKNode:名称:'(null)'纹理:['CLOUD'(746 x 497)]位置:{ 198.66667175292969,1227.987060546875}比例:{0.30,0.30}尺寸:{223.80000305175781,149.10000610351562}锚点:{0.5,0.5}旋转:0.00'。 '

我不确定发生了什么问题,将不胜感激

 let cloud1 = SKSpriteNode(imageNamed: "CLOUD")
 let cloud2 = SKSpriteNode(imageNamed: "CLOUD")

class GameScene: SKScene {

override func didMove(to view: SKView) {
          self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
 setUp()
}

func setUp() {

    let block = SKAction.run ({
        self.spawnClouds()

        if (cloud1.position.y  > self.size.height) {

        cloud1.removeFromParent()
            cloud2.removeFromParent()
        }
    })

    let wait = SKAction.wait(forDuration: 9)

    let action = SKAction.repeat(block, count: 10)
            let removes = SKAction.removeFromParent()

    let sequence = SKAction.sequence([block, wait, action, removes])

    self.run(sequence)
}

@objc func spawnClouds() {
    let gapHeight = self.frame.width / 9
    let movementAmount = arc4random() % UInt32(self.frame.width / 2)
    let cloudOffset = CGFloat(movementAmount) - self.frame.width / 4

   // let cloud1 = SKSpriteNode(imageNamed: "CLOUD")

    cloud1.xScale = 0.3
    cloud1.yScale = 0.3
    cloud1.zPosition = 1
    self.addChild(cloud2)
    cloud2.xScale = 0.3
    cloud2.yScale = 0.3
    cloud2.zPosition = 1

    cloud1.position = CGPoint(x:self.frame.minX / 2 + gapHeight / 5 + cloudOffset , y: self.frame.minY  )
    cloud2.position = CGPoint(x:self.frame.maxX / 2 + gapHeight / 5 + cloudOffset , y: self.frame.minY  )

    self.addChild(cloud1)

    var distanceToMove = CGFloat(self.frame.size.width + 2.0 * cloud1.frame.width)
    let moveCloud = SKAction.move(by: CGVector(dx:0, dy:25 * self.frame.width), duration: TimeInterval(self.frame.height / 15 ))

    let newaction = SKAction.removeFromParent()
    let new = SKAction.sequence([moveCloud, newaction])

    cloud1.run(new)
    cloud2.run(new)
}

override func update(_ currentTime: TimeInterval) {

    if cloud2.position.y > size .height {

        cloud1.removeFromParent()
        cloud2.removeFromParent()
    }
    }

}

2 个答案:

答案 0 :(得分:1)

cloud1和cloud2是属性,即随处可见。因此,当您执行addChild(cloud1)addChild(cloud2)时,这些不是新创建的sprite,而是您之前添加的sprite,因此出现消息“尝试添加已经具有父级的SKNode”

在旨在产生节点的函数中,通常会创建一个新的SKNode来每次产生,就像注释掉的代码行一样:

// let cloud1 = SKSpriteNode(imageNamed: "CLOUD")

这将确保cloud1每次都是新节点。

您还应该针对cloud2进行此操作。

答案 1 :(得分:0)

两件事,首先是动作应该是一个序列。

   // let action = SKAction.repeat( block, count: 10)
     let action = SKAction.repeat( SKAction.sequence( [block,wait]), count: 10)

second,计时应该是9s左右的完美时间,即self.frame.height / 15

   //let moveCloud = SKAction.move(by: CGVector(dx:0, dy:25 * self.frame.width), duration: TimeInterval(self.frame.height / 15 ))

所以可能是:

  let moveCloud = SKAction.move(by: CGVector(dx:0, dy:2.5 * self.frame.width), duration: TimeInterval(self.frame.height / 150 ))