【发布时间】:2016-02-01 10:10:45
【问题描述】:
我有一个包含 CGFloat 值的数组,我试图弄清楚如何通过这个数组进行随机化以获得随机 x 坐标:
let xAxisSpawnLocations: [CGFloat] = [screenWidth + 150, screenWidth + 100, screenWidth + 50 , screenWidth - 50, screenWidth - 100, screenWidth - 150]
因为我必须将它们放入将在地图上定位对象的代码行中。
obstacle.position = CGPoint(x: ARRAY VALUE HERE, y: yAxisSpawnLocations[0])
我尝试查找如何通过数组随机化,但它仅适用于 Int32 值。
请提供一些关于如何执行此操作的示例。
以下是完整代码供您参考:
let xAxisSpawnLocations: [CGFloat] = [screenWidth + 150, screenWidth + 100, screenWidth + 50 , screenWidth - 50, screenWidth - 100, screenWidth - 150]
let yAxisSpawnLocations: [CGFloat] = [0]
for xLocation in xAxisSpawnLocations {
for (var i = 0; i < Int(numberOfObjectsInLevel); i++) {
let obstacle:Object = Object()
obstacle.theType = LevelType.road
obstacle.createObject()
addChild(obstacle)
obstacle.position = CGPoint(x: xLocation, y: yAxisSpawnLocations[0])
obstacle.zPosition = 9000
// addChild(greenFrog)
}
}
}
xLocation 需要被随机化器替换
【问题讨论】:
标签: arrays swift random cgfloat