Ios 相互切换精灵位置

Ios 相互切换精灵位置,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我有6个精灵,我想在每次调用函数时彼此切换位置。我也不希望任何两个(或更多)精灵处于相同的位置。我试着使用这个函数,但没有任何效果 func newPositionPipe() { let pipePosition1 = CGPoint(x: 63, y: 1275) let pipePosition2 = CGPoint(x: 188, y: 1275) let pipePosition3 = CGPoint(x: 315, y: 1275) let pipeP

我有6个精灵,我想在每次调用函数时彼此切换位置。我也不希望任何两个(或更多)精灵处于相同的位置。我试着使用这个函数,但没有任何效果

func newPositionPipe() {
    let pipePosition1 = CGPoint(x: 63, y: 1275)
    let pipePosition2 = CGPoint(x: 188, y: 1275)
    let pipePosition3 = CGPoint(x: 315, y: 1275)
    let pipePosition4 = CGPoint(x: 443, y: 1275)
    let pipePosition5 = CGPoint(x: 565, y: 1275)
    let pipePosition6 = CGPoint(x: 687, y: 1275)

    var randomNumberBetween0And6 = Int(arc4random_uniform(6))

    let pipePositions = [pipePosition1, pipePosition2, pipePosition3, pipePosition4, pipePosition5, pipePosition6]

    greenPipe.position = pipePositions[randomNumberBetween0And6]
    redPipe.position = pipePositions[randomNumberBetween0And6]
    greenPipe.position = pipePositions[randomNumberBetween0And6]
    yellowPipe.position = pipePositions[randomNumberBetween0And6]
    greyPipe.position = pipePositions[randomNumberBetween0And6]
    purplePipe.position = pipePositions[randomNumberBetween0And6]
}

您现在设置的方式将使每个管道位置相同,因为它将引用您使用0和6之间的
randomnumberb生成的相同的随机
索引
编号。将其放入循环中,每次生成一个新的随机数。您还可以使用交换来交换值

(greenPipe.position, redPipe.position) = (redPipe.position, greenPipe.position)