gpt4 book ai didi

swift - 在 SpriteKit 中创建随机数

转载 作者:搜寻专家 更新时间:2023-11-01 06:19:06 25 4
gpt4 key购买 nike

我正在尝试创建一个以随机角度发射射弹的游戏。为此,我需要能够生成两个随机 Int。我查阅了一些教程并想到了这个:

var random = CGFloat(Int(arc4random()) % 1500)
var random2 = CGFloat(Int(arc4random()) % -300)
self.addChild(bullet)
bullet.physicsBody!.velocity = CGVectorMake((random2), (random))

它工作了一段时间,但现在崩溃了。

如有任何帮助,我们将不胜感激。

最佳答案

我发现我使用最多的是 arc4random_uniform(upperBound),它返回一个从零到 upperBound -1 的随机 Int。

let random = arc4random_uniform(1500)
let random2 = arc4random_uniform(300) * -1

//pick a number between 1 and 10
let pick = arc4random_uniform(10)+1

arc4 函数的内幕:arc4random man page

GameplayKit 有一个很好的包装 arc4 函数的类:GameplayKit Randomness

和一个方便的引用:Random Hipster

关于swift - 在 SpriteKit 中创建随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37689030/

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