0
我有这个功能,我使用生成花随机的位置的物体每秒:“Attemped添加已经具有父SKNode”斯威夫特
func spawnFlower() {
//Create flower with random position
let tempFlower = Flower()
let height = UInt32(self.size.height/2)
let width = UInt32(self.size.width/2)
let randomPosition = CGPoint(x: Int(arc4random_uniform(width)), y: Int(arc4random_uniform(height)))
tempFlower.position = randomPosition
var tooClose = false
flowerArray.append(tempFlower)
// enumerate flowerArray
for flower in flowerArray {
// get the difference in position between the current node
// and each node in the array
let xPos = abs(flower.position.x - tempFlower.position.x)
let yPos = abs(flower.position.y - tempFlower.position.y)
// check if the spawn position is less than 10 for the x or y in relation
// to the current node in the array
if (xPos < 10) || (yPos < 10) {
tooClose = true
}
if tooClose == false {
//Spawn node
addChild(tempFlower)
}
}
}
我创造了花一个新的实例每一次函数被调用,但由于某种原因,当我调用该函数类似下面,它给我的错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent:
的spawnFlower()函数被调用每一秒。它第一次被调用,第二次崩溃。我究竟做错了什么?
谢谢,这工作。最后,我还不得不在if语句中放入flowerArray.append(tempFlower)行,因为每次都将CloseClose设置为true。 – 5AMWE5T