随机位置函数

Random position func

我有 2 个对象(dvere1、dvere2)有它们的位置。如何设置这两个对象通过按下改变它们的位置的随机函数。我是初学者

这是我的代码:

    var dvere1 = SKSpriteNode(imageNamed: "D")
var dvere2 = SKSpriteNode(imageNamed: "D")



override func didMoveToView(view: SKView) {
backgroundColor = SKColor.whiteColor()


    dvere1.position = CGPoint(x:self.view!.bounds.width / 1.5, y:self.view!.bounds.height/1.9)
    addChild(dvere1)
    dvere2.position = CGPoint(x:self.view!.bounds.width / 3.1, y:self.view!.bounds.height/1.9)
    addChild(dvere2)

}

您应该使用 arc4random_uniform 方法如下:

var randomPostion = CGPoint(x:CGFloat(arc4random_uniform(UInt32(self.view!.bounds.width))), y:CGFloat(arc4random_uniform(UInt32(self.view!.bounds.height))))

dvere1.position = randomPosition