相互交换精灵位置
Switching sprite positions with each other
我有 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]
}
您现在的设置方式将使每个管道位置都相同,因为它将引用您使用 randomNumberBetween0And6
生成的相同随机 index
数字。把它放在一个循环中,每次都生成一个新的随机数。您还可以使用 Tuple 来交换值。
(greenPipe.position, redPipe.position) = (redPipe.position, greenPipe.position)
我有 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]
}
您现在的设置方式将使每个管道位置都相同,因为它将引用您使用 randomNumberBetween0And6
生成的相同随机 index
数字。把它放在一个循环中,每次都生成一个新的随机数。您还可以使用 Tuple 来交换值。
(greenPipe.position, redPipe.position) = (redPipe.position, greenPipe.position)