Swift中的随机值 [英] Random value in Swift
本文介绍了Swift中的随机值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试设置一个介于0和屏幕尺寸之间的值。所以我这样做是这样的:
I'm trying to have a value who is between 0 and the size of my screen. So i did it like this :
let sizeX = Int(CGRectGetMaxX(self.frame))
let sizeY = Int(CGRectGetMaxY(self.frame))
var randomX = CGFloat(arc4random()) % sizeX
var randomY = CGFloat(arc4random()) % sizeY
self.touchButton.position = CGPointMake(randomX, randomY)
我有此错误:
找不到' %'接受提供的参数
I have this error : could not find an overload for '%' that accepts the supplied arguments
我需要使用此参数来随机化SkSpriteNode的位置,也许有更好的解决方案吗?
I need this to randomize the position of an SkSpriteNode, maybe is there a better solution ?
谢谢
推荐答案
转换 arc4random()$ c $的值c>返回一个Int,然后将整个表达式转换为CGFloat:
Convert the value that arc4random()
returns to an Int and then convert the whole expression to a CGFloat:
var randomX = CGFloat(Int(arc4random()) % sizeX)
var randomY = CGFloat(Int(arc4random()) % sizeY)
self.touchButton.position = CGPointMake(randomX, randomY)
这篇关于Swift中的随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文