Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 生成UIInt8而不是UIInt32的随机方法?_Ios_Swift_Sprite Kit - Fatal编程技术网

Ios 生成UIInt8而不是UIInt32的随机方法?

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”。 有没有一种方法可以绕过这个问题,或者用不同的方式设计代码使其工作?就像一个解决方案,其中随机

我想随机生成一个精灵的位置。我使用arc4random()方法获取1到8的范围

 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
参数。