Ios 生成UIInt8而不是UIInt32的随机方法?
我想随机生成一个精灵的位置。我使用arc4random()方法获取1到8的范围Ios 生成UIInt8而不是UIInt32的随机方法?,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我想随机生成一个精灵的位置。我使用arc4random()方法获取1到8的范围 var randomFactor = arc4random_uniform(8) + 1 sprite.position = CGPointMake(self.frame.width/8 * randomFactor, self.frame.height) 弹出一个错误,告诉我“UIInt32不能转换为UIInt8”。 有没有一种方法可以绕过这个问题,或者用不同的方式设计代码使其工作?就像一个解决方案,其中随机
var randomFactor = arc4random_uniform(8) + 1
sprite.position = CGPointMake(self.frame.width/8 * randomFactor, self.frame.height)
弹出一个错误,告诉我“UIInt32不能转换为UIInt8”。
有没有一种方法可以绕过这个问题,或者用不同的方式设计代码使其工作?就像一个解决方案,其中随机方法不是UIInt32而是UIInt8。
提前感谢。您应该将“随机因子”转换为CGFloat:
var randomFactor = arc4random_uniform(8) + 1
var x = self.frame.width / 8 * CGFloat(randomFactor)
var y = self.frame.height
var position = CGPointMake(x, y)
这个错误在哪里弹出?我看不到您在任何地方使用的是
UInt8
或UInt32
。我认为arc4random是一个UIInt32,CGPointMake要求我使用UIInt8,如果我想在其中“混合”的话。CGPointMake
接受CGFloat
参数。