我想讓我的節點在屏幕上移動並在定時器上無限生成。我希望一次在屏幕上有多個節點。這是我迄今爲止的穩定代碼。我嘗試了多種凍結或崩潰的方法。swift繁殖節點infinietly
let bunnyTexture = SKTexture(imageNamed: "oval 1.png")
bunny = SKSpriteNode(texture: bunnyTexture)
bunny.position = CGPoint(x: (size.width/3), y: 750 + bunny.frame.height)
self.addChild(bunny)
bunny.physicsBody = SKPhysicsBody(circleOfRadius: bunnyTexture.size().height/2)
bunny.physicsBody!.dynamic = true
let spawnBunny = SKAction.moveByX(0, y: -self.frame.size.height, duration: 4)
let despawnBunny = SKAction.removeFromParent()
let spawnNdespawn2 = SKAction.sequence([spawnBunny, despawnBunny])
bunny.runAction(spawnNdespawn2)
let gobblinTexture = SKTexture(imageNamed: "oval 2.png")
gobblin = SKSpriteNode(texture: gobblinTexture)
gobblin.position = CGPoint(x: 150 + gobblin.frame.width, y: (size.height/3))
self.addChild(gobblin)
let randomGob = arc4random() % UInt32(self.frame.size.height/2)
let spawnGob = SKAction.moveByX(+self.frame.size.width, y: 0, duration: 4)
let despawnGob = SKAction.removeFromParent()
let spawnNdespawn = SKAction.sequence([spawnGob, despawnGob])
gobblin.runAction(spawnNdespawn)
let ground = SKNode()
ground.position = CGPointMake(0, 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, -50))
ground.physicsBody!.dynamic = false
self.addChild(ground)
這是爲什麼如此複雜?將2個動作(等待和移動)放在一個序列中,然後使用重複動作不會更容易嗎?同樣,如果繼續循環爲假,爲什麼還要等待 – Knight0fDragon
@ Knight0fDragon'continueLoop()'可以/應該儘可能多次評估,在wait之前和之後都是有意義的。我之所以選擇「等待」是因爲它會在最後一個動作運行後進行評估。如果時機不同,這將不再是事實。看着'repeatAction' ... –
@ Knight0fDragon你說得對,'repeatAction'確實提供了一個更清潔的解決方案。我將它添加到我的答案中。好決定! –