Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.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 如何在Swift中制作随机精灵位置生成器_Ios_Xcode_Swift_Random_Generator - Fatal编程技术网

Ios 如何在Swift中制作随机精灵位置生成器

Ios 如何在Swift中制作随机精灵位置生成器,ios,xcode,swift,random,generator,Ios,Xcode,Swift,Random,Generator,我已经创建了一个游戏,其中鸟类将随机出现在屏幕上。 肖像模式。 希望它们从随机x位置(从帧中间)显示 我还希望他们在屏幕的任意Y位置(最大“自我”屏幕顶部)定位 代码的顶行可以工作(randomGenerator),但只在屏幕的一侧(X位置)。 我不能将负值放在X位置,因为它不允许 正如你所看到的,我当时正在尝试其他事情 我做错了什么 var randomGenerater = CGPoint(x:Int(arc4random()%180) ,y:Int(arc4random()%260)) v

我已经创建了一个游戏,其中鸟类将随机出现在屏幕上。 肖像模式。 希望它们从随机x位置(从帧中间)显示 我还希望他们在屏幕的任意Y位置(最大“自我”屏幕顶部)定位

代码的顶行可以工作(randomGenerator),但只在屏幕的一侧(X位置)。 我不能将负值放在X位置,因为它不允许

正如你所看到的,我当时正在尝试其他事情

我做错了什么

var randomGenerater = CGPoint(x:Int(arc4random()%180) ,y:Int(arc4random()%260))
var minXPosition = (CGRectGetMidX(self.frame) * CGFloat(Float(arc4random()) / Float(300)))
var RandomYPosition = (CGRectGetMidY(self.frame) * CGFloat(Float(arc4random()) / Float(UINT32_MAX)))
var trial = (CGRectGetMidY(self.frame) + CGFloat(Float(arc4random()) - Float(UINT32_MAX))) 
var randomGenerator2 = CGPointMake(minXPosition, trial)
 bird.position = randomGenerator2
一般来说

func randomInRange(lo: Int, hi : Int) -> Int {
    return lo + Int(arc4random_uniform(UInt32(hi - lo + 1)))
}
给出一个范围
lo。。。你好
,所以

// x coordinate between MinX (left) and MaxX (right):
let randomX = randomInRange(Int(CGRectGetMinX(self.frame)), Int(CGRectGetMaxX(self.frame)))
// y coordinate between MinY (top) and MidY (middle):
let randomY = randomInRange(Int(CGRectGetMinY(self.frame)), Int(CGRectGetMidY(self.frame)))
let randomPoint = CGPoint(x: randomX, y: randomY)

应该给出想要的结果。

感谢您在这个随机范围内的提醒。由于hi和lo尚未设置,您给我的内容出现了错误。我不太明白这个算法是否公平,所以我不能通过它。但是谢谢你的帮助:)@RickC:对不起,我不明白。您必须在源文件的某个地方定义
randomInRange()
函数。然后让randomX=randomInRange(…,…)使用给定值调用函数。