没有重叠的随机节点位置

时间:2016-07-30 18:52:48

标签: swift xcode sprite-kit

我遇到了问题,我不知道从哪里开始。

我使用以下代码将sprite1分配到随机位置:

let index2 = arc4random() % 3;
        if index2 == 0 {
        sqrite1.position = CGPoint(x: CGRectGetMidX(self.frame) - 100  , y: CGRectGetMidY(self.frame))
    } else if index2 == 1 {
        sprite1.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    } else if index2 == 2{
        sprite1.position = CGPoint(x: CGRectGetMidX(self.frame) + 100, y: CGRectGetMidY(self.frame))
    }

然后我再次重复上面的代码,但对于sprite2如下:

我使用相同的代码将sprite2分配给一个随机位置,方法与sprite 1相同:

let index2 = arc4random() % 3;
        if index2 == 0 {
        sqrite2.position = CGPoint(x: CGRectGetMidX(self.frame) - 100  , y: CGRectGetMidY(self.frame))
    } else if index2 == 1 {
        sprite2.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    } else if index2 == 2{
        sprite2.position = CGPoint(x: CGRectGetMidX(self.frame) + 100, y: CGRectGetMidY(self.frame))
    }

然后我再次重复上面的代码第3次,但对于sprite3如下:

然后我使用相同的代码将sprite3分配给一个随机位置,方法与sprite1和sprite2相同:

let index2 = arc4random() % 3;
        if index2 == 0 {
        sqrite3.position = CGPoint(x: CGRectGetMidX(self.frame) - 100  , y: CGRectGetMidY(self.frame))
    } else if index2 == 1 {
        sprite3.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    } else if index2 == 2{
        sprite3.position = CGPoint(x: CGRectGetMidX(self.frame) + 100, y: CGRectGetMidY(self.frame))
    }

现在,这是我的问题:我想确保sprite1,sprite2和sprite3不会在同一个位置结束。我怎样才能做到这一点?我试图想办法做到这一点,但我不知道从哪里开始。

谢谢:)

2 个答案:

答案 0 :(得分:0)

我建议创建一个将节点分配给随机点的函数。

func randomSpot(node: SKSpriteNode) {
    let index2 = arc4random() % 3;
    if index2 == 0 {
         node.position = CGPoint(x: CGRectGetMidX(self.frame) - 100  , y: CGRectGetMidY(self.frame))
    } else if index2 == 1 {
        node.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    } else if index2 == 2{
        node.position = CGPoint(x: CGRectGetMidX(self.frame) + 100, y: CGRectGetMidY(self.frame))
    }
}

然后实际随机化:

randomSpot(sprite1)

确保它不在同一地点:

randomSpot(sprite2)
while sprite2.position == sprite1.position {
    randomSpot(sprite2)
}

对于sprite3:

randomSpot(sprit3)
while sprite3.position == sprite1.position || sprite3.position == sprite2.position {
    randomSpot(sprite3)
}

注意:您可以使用扩展程序执行此操作:

extension SKSpriteNode {
    func randomSpot() {
        let index2 = arc4random() % 3;
        if index2 == 0 {
             self.position = CGPoint(x: CGRectGetMidX(self.parent!.frame) - 100  , y: CGRectGetMidY(self.parent!.frame))
        } else if index2 == 1 {
            self.position = CGPoint(x: CGRectGetMidX(self.parent!.frame), y: CGRectGetMidY(self.parent!.frame))
        } else if index2 == 2{
            self.position = CGPoint(x: CGRectGetMidX(self.parent!.frame) + 100, y: CGRectGetMidY(self.parent!.frame))
        }
    }

}

然后你会称之为:

sprite1.randomSpot()

答案 1 :(得分:0)

如果选择了之前的数字,您需要一种方法来检查每次检查,这对n个随机位置有效,没有限制:

   class GameScene: SKScene {
        var positions = [UInt32]()
        override func didMoveToView(view: SKView) {
            let index2 = getRandomNum()
            // do whatever you want with index2 for node1
            ...
            let index2 = getRandomNum()
            // do whatever you want with index2 for node2
        }
        func getRandomNum() -> UInt32 {
            var exist : Bool = false
            var index2 : UInt32!
            while !exist {
                index2 = arc4random() % 3
                if !positions.contains(index2) {
                    exist = true
                }
            }
            positions.append(index2)
            return index2
        }
    }

最后关于你的立场我考虑了一种更优雅的方法:

func setPos(index2:UInt32, sprite:SKSpriteNode) {
        switch index2 {
        case 0:
            sprite.position = CGPoint(x: CGRectGetMidX(self.frame) - 100  , y: CGRectGetMidY(self.frame))
        case 1:
            sprite.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
        case 2:
            sprite.position = CGPoint(x: CGRectGetMidX(self.frame) + 100, y: CGRectGetMidY(self.frame))
        default:
            break
        }
}

您可以直接执行每个节点只有一行:

setPos(getRandomNum(),sprite:sprite1)