gpt4 book ai didi

swift - X、Y 坐标的随机数生成器

转载 作者:行者123 更新时间:2023-11-28 11:16:07 25 4
gpt4 key购买 nike

我今天在编写代码时偶然发现了随机数生成器的问题。我正在尝试制作一个与 Fruit Ninja 中的生成器非常相似的生成器,我也在快速编写。这是我的代码片段:

var rand_x = arc4random() 
var rand_y = arc4random()
node!.position = CGPoint(x: rand_x, y: rand_y)

我的问题是出现错误:

Cannot find an initializer for type 'CGPoint' that accepts an argument list of type '(x: UInt32, y: UInt32)'

有什么提示可以解决这个问题吗?谢谢!

最佳答案

两件事:

  • 您可能需要将头寸的最大值限制在某个值,例如屏幕的宽度/高度
  • arc4random 返回一个 UInt32,而 CGPoint 的初始化器采用 CGFloats。您需要将 UInt32 转换为 CGFloats

这看起来像:

var rand_x = CGFloat(arc4random_uniform(maxXValue)) // maxXValue is a variable with your maximum possible x value
var rand_y = CGFloat(arc4random_uniform(maxYValue)) // maxYValue is a variable with your maximum possible y value
node!.position = CGPoint(x: rand_x, y: rand_y)

关于swift - X、Y 坐标的随机数生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31687709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com