我想让我的节点在屏幕上移动并在定时器上无限生成。我希望一次在屏幕上有多个节点。这是我迄今为止的稳定代码。我尝试了多种冻结或崩溃的方法。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'确实提供了一个更清洁的解决方案。我将它添加到我的答案中。好决定! –