随机位置函数

时间:2015-06-02 12:53:02

标签: ios swift sprite-kit

我有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)

}

1 个答案:

答案 0 :(得分:1)

您应该使用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