swift - 在 SpriteKit 中创建随机数

标签 swift random sprite-kit int

我正在尝试创建一个以随机角度发射射弹的游戏。 为此,我需要能够生成两个随机 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/

相关文章:

ios - 如何从标签数组中快速更改 Collection View 中特定单元格标签的颜色

ios - 为什么每当我加载 UIImagePickerController View 时都会加载我的键盘?

ios - 将粒子发射器添加到 FlappySwift 项目

ios - SpriteKit如何从另一个场景请求数据

ios - 遵循相反的路径

swift - 当我尝试在 Xcode 中导入 VisualRecognitionV3 时,它给出了用 swift 3.0 编译的错误模块无法在 swift 3.0.2 中导入?

python random.randint 以 5 为增量生成 0-100

java - 以帕斯卡为单位的高斯

java - 多个随机数结果

swift - 如何在第一层SWIFT上添加SKNode