我想知道如何实现从屏幕顶部随机添加节点的功能。所以我需要各种颜色的节点,然后将它们放入一个数组中。然后随机编号。总的来说,数组中有五个节点,我将数字设置为5.
func fallingObjects(){
let randomWidth = arc4random_uniform(60)
let randomHeight = arc4random_uniform(60)
let node1 = SKSpriteNode(color: SKColor.blueColor(), size: CGSizeMake(CGFloat(randomWidth), CGFloat(randomHeight)))
let node2 = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(CGFloat(randomWidth), CGFloat(randomHeight)))
let node3 = SKSpriteNode(color: SKColor.brownColor(), size: CGSizeMake(CGFloat(randomWidth), CGFloat(randomHeight)))
let node4 = SKSpriteNode(color: SKColor.lightGrayColor(), size: CGSizeMake(CGFloat(randomWidth), CGFloat(randomHeight)))
let node5 = SKSpriteNode(color: SKColor.orangeColor(), size: CGSizeMake(CGFloat(randomWidth), CGFloat(randomHeight)))
var array = [SKSpriteNode]()
array.append(node1)
array.append(node2)
array.append(node3)
array.append(node4)
array.append(node5)
let ranNum = arc4random_uniform(5)
for a in array {
addChild(a[ranNum])
}
}
我知道我做错了但是如果你指出问题就很感激。并给我解决方案:)