2014-06-26 126 views
2

因此,我正在嘗試開發一個用Swift編寫的簡單遊戲,但我在做一件非常簡單的事情時遇到了麻煩。我無法設法創建一個隨機CGPoint ...當使用arc4random時,編譯器錯誤顯示告訴我,我不能在CGPoint中使用Int32。那麼,有沒有辦法做到這一點?任何解決方法?謝謝!用Swift創建隨機CGPoint

回答

2

嗨那麼構建一個Int? Int(arc4random())

例如

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

哦,它的工作!謝謝!有沒有更簡單的方法來做到這一點?再次感謝! :D –

2

也可以使用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檢查語法/如果它c ompiles正確,但希望能有所幫助:-)

...

//////////////////

雨燕1.2更新

//////////////////

似乎這些類型級函數調用不與擴展了允許的......至少CGPoi NT;可能是因爲CGPoint實際上是一個結構,而不是基於當前IOS文檔的類。

下面是我的擴展的更深入的版本,它允許範圍類型。

這被確認擔任的XCode 6.4 Beta版的

(Github上庫與遊樂場的文件在這裏找到:

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

這似乎在Swift 1.2中不再起作用。 –

+0

似乎如此。看起來像在Swift 1.2中,我們不再可以執行'CGPoint.random()'或任何這些類型級調用。現在更新擴展代碼,並且應該很快就會有一個github庫,這樣它可以不斷被分叉和更新 –