使用Swift创建随机CGPoint

时间:2014-06-26 19:19:12

标签: swift sprite-kit cgpoint

所以,我试图开发一个用Swift编写的简单游戏,但是我在做一件非常简单的事情时遇到了麻烦。我无法创建随机CGPoint ...当使用arc4random时,编译器错误显示告诉我我不能在CGPoint中使用Int32。那么,有没有办法做到这一点?任何解决方法?谢谢!

3 个答案:

答案 0 :(得分:3)

也可以利用Swift的基类型扩展来创建一组可重用的CGPoint重载函数。也许是这样的:

extension CGPoint {

    func random()->CGPoint { return CGPoint(x:Int(arc4random()%1000),y:Int(arc4random()%1000))}

    func random(range:Int)->CGPoint { 
         return CGPoint(x:Int(arc4random()%range),y:Int(arc4random()%range))}

    func random(rangeX:Int, rangeY:Int)->CGPoint { 
         return CGPoint(x:Int(arc4random()%rangeX),y:Int(arc4random()%rangeY))} 
}

然后您可以像这样编写随机CGPoints:

var p = CGPoint.random() 
//random x and y with a range of 1000

var p = CGPoint.random(range:100)
//random x and y with a range of 100

var p = CGPoint.random(rangeX:200, rangeY:400)
//random x up to 200 and random y with a range of up to 400

当然,我现在不在Xcode IDE中检查语法/它是否正确编译但希望可以提供帮助: - )

...

<强> //////////////////

Swift 1.2更新

<强> //////////////////

似乎这些类型级函数调用不再允许扩展...至少对于CGPoint;可能是因为CGPoint实际上是一个结构而不是基于当前IOS文档的类。

这是我的扩展程序的更深入版本,允许范围类型。

这已在XCode 6.4 Beta

中得到确认

(Github存储库中包含Playground文件:

https://github.com/princetrunks/Random-CGPoint-Extension

//creates random CGPoints in Swift as of XCode Beta 6.4 (6E7)
extension CGPoint {

/*private functions that help alleviate the ambiguity of the modulo bias 
   and nested typecasting as well as recycle similar functionality 
     for either Int or Range type parameter inputs */

private func randomInt(num:Int) ->Int{
    return Int(arc4random_uniform(UInt32(num)))
}

private func randomIntFromRange(numRange:Range<Int>) ->Int{
    return Int(arc4random_uniform(UInt32((numRange.endIndex - numRange.startIndex) + numRange.startIndex)))
}

//private variable for the default range
private var defaultRange : Int{
    get{return 1000}
}

//(a) public variable that creates a default random CGPoint
static var randomPoint = CGPoint.zeroPoint.random()


//(b) default random point creation
func random()->CGPoint { return CGPoint(x:randomInt(defaultRange),y:randomInt(defaultRange))}

//(c) using an Int parameter for both the random x and y range
func random(range:Int)->CGPoint {
    return CGPoint(x:randomInt(range),y:randomInt(range))
}

//(d) allows for the specification of the x and y random range
func random(#rangeX:Int, rangeY:Int)->CGPoint {
    return CGPoint(x:randomInt(rangeX),y:randomInt(rangeY))
}

//(e) allows the same functionality as (c) but with a Range<Int> type parameter
func random(range:Range<Int>)->CGPoint {
    return CGPoint(x:randomIntFromRange(range), y:randomIntFromRange(range))
}

//(f) allows the same functionality as (d) but with a Range<Int> type parameter
func random(#rangeX:Range<Int>, rangeY:Range<Int> )->CGPoint {
    return CGPoint(x:randomIntFromRange(rangeX), y:randomIntFromRange(rangeY))
 }

}

以下是我们如何测试此扩展程序:

//(a)
let r  = CGPoint.randomPoint

//(b)
var anotherRandomPoint = r.random()

//(c)
anotherRandomPoint = r.random(1000)

//(d)
anotherRandomPoint = r.random(0...1000)

//(e)
anotherRandomPoint = r.random(rangeX:90, rangeY: 2000)

//(f)
anotherRandomPoint = r.random(rangeX:0...90, rangeY: 0...2000)


// generates 100 random CGPoints between -1000 and 999
for _ in 0...100 {
    anotherRandomPoint.random(-1000...1000)
}

答案 1 :(得分:2)

嗨,构建一个Int? Int(arc4random())

e.g。

var p = CGPoint(x:Int(arc4random()%1000),y:Int(arc4random()%1000))

答案 2 :(得分:0)

Swift 4,5
// Add some range
let minX = 0
let maxX = 100
let minY = 0
let maxY = 100
let randomX = CGFloat.random(in: minX..<maxX)
let randomY = CGFloat.random(in: minY..<maxY)
let random = CGPoint(x: randomX, y: randomY)