我想要' n'节点数量不断向各个方向移动。 以下是我编码的方式
func spriteCollection(count : Int) -> [SKSpriteNode]{
var spriteArray = [SKSpriteNode]()
for _ in 0..<count {
let sprite = SKSpriteNode(imageNamed: "node.png")
//giving random position to sprites,
let x = CGFloat(arc4random_uniform(UInt32(((self.scene?.frame.width)! - sprite.size.width))))
let y = CGFloat(arc4random_uniform(UInt32(((self.scene?.frame.height)! - sprite.size.height))))
sprite.position = CGPoint(x: x, y: y)
spriteArray.append(sprite)
}
return spriteArray
}
override func didMoveToView(view: SKView) {
//here if I pass more than 2, the nodes won't even get displayed sometimes
let spriteCollection = spriteCollection(2)
for sprite in spriteCollection{
let blockOFaction = SKAction.runBlock({
let x = CGFloat(arc4random_uniform(UInt32(((self.scene?.frame.width)! - sprite.size.width))))
let y = CGFloat(arc4random_uniform(UInt32(((self.scene?.frame.height)! - sprite.size.height))))
let action = SKAction.moveTo(CGPoint(x: x , y: y), duration: 2)
print("moving to \(x,y)")
action.speed = 3.0
sprite.runAction(action)
})
let waitAction = SKAction.waitForDuration(1)
sprite.runAction(SKAction.repeatActionForever(SKAction.sequence([blockOFaction, waitAction])))
addChild(sprite)
}
}
节点在所有方向上随机移动,但如果我添加超过2个精灵,有时没有可见的精灵(只是一个空白的屏幕),而且我想最终增加精灵的数量。我在做什么,只是传递我想要的精灵数量(最终不能增加)所以还有其他方法可以做到这一点。任何人都可以建议我如何实现这一目标..