2014-12-06 24 views
1

我有一個SKSpriteNode數組聲明如下:不大不小的SKSpriteNode陣列根據特定的元素

class rgbNodes: SKSpriteNode 
{ 
} 

var colorNode = [rgbNodes]() 

colorNode.append(rgbNodes(imageNamed: "Rectangle")) // every time we want to add a new element to this array 

而且我想根據自己的position.x值這個數組的每個元素進行排序,例如,如果:

colorNode[0].position.x = 25 
colorNode[1].position.x = 5 
colorNode[2].position.x = 15 

我想數組排序是這樣的:

colorNode[0].position.x = 5 
colorNode[1].position.x = 15 
colorNode[2].position.x = 25 

但我怎樣才能設法使用排序命令呢?

回答

1

排序colorNode,你可以這樣做:

colorNode.sort() { 
    $0.position.x < $1.position.x 
} 
相關問題