本文介绍了根据特定元素对SKSpriteNode数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样声明的SKSpriteNode数组:

I have a SKSpriteNode array declared like this :

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值对数组的每个元素进行排序,例如:

And I would like to sort every element of this array according to their position.x value, for example, if :

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

我希望对数组进行如下排序:

I want the array to be sorted like this :

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

但是我如何使用sort命令来做到这一点呢?

But how can I manage to do it with the sort command ?

推荐答案

要对colorNode进行排序,可以执行以下操作:

To sort colorNode, you could do this:

colorNode.sort() {
    $0.position.x < $1.position.x
}

这篇关于根据特定元素对SKSpriteNode数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:32