如何在没有重叠的情况下生成多个节点?

时间:2016-01-14 17:38:46

标签: swift sprite-kit

我的屏幕上每0.2-5.0秒产生一个节点,如下所示:

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

    runAction(SKAction.repeatActionForever(
        SKAction.sequence([
            SKAction.runBlock(blackDots),
            SKAction.waitForDuration(1.0)])))
}

func random() -> CGFloat {
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}

func random(min min: CGFloat, max: CGFloat) -> CGFloat {
    return random() * (max - min) + min
}

func blackDots() {
    let dot = SKSpriteNode(imageNamed: "first@2x")
    dot.size = CGSizeMake(75, 75)
    dot.name = "dotted"
    dot.position = CGPointMake(500 * random(min: 0, max: 1), 500 * random(min: 0, max: 1))
    addChild(dot)
}

然而,当它们产生时,有些相交并且相互叠加?有办法防止这种情况吗?提前致谢。

2 个答案:

答案 0 :(得分:2)

以下是检查节点是否存在于特定位置的方法。

您可能希望将检查放入循环中,以便在获取位置时,它将使用新生成的点重试。否则你会得到一些刚刚赢得的点。只取决于你对他们的所作所为。

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

    runAction(SKAction.repeatActionForever(
        SKAction.sequence([
            SKAction.runBlock(blackDots),
            SKAction.waitForDuration(1.0)])))
}

func random() -> CGFloat {
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}

func random(min min: CGFloat, max: CGFloat) -> CGFloat {
    return random() * (max - min) + min
}

func blackDots() {
    let dot = SKSpriteNode(imageNamed: "first@2x")
    dot.size = CGSizeMake(75, 75)
    dot.name = "dotted"

    let position = CGPointMake(500 * random(min: 0, max: 1), 500 * random(min: 0, max: 1))

    if positionIsEmpty(position) {
        dot.position = position
        addChild(dot)
    }
}

func positionIsEmpty(point: CGPoint) -> Bool {
    self.enumerateChildNodesWithName("dotted", usingBlock: {
        node, stop in

        let dot = node as SKSpriteNode
        if (CGRectContainsPoint(dot.frame, point)) {
            return false
        }
    })
    return true
}

答案 1 :(得分:0)

这是在swift 3.1中请注意,这确实需要几个小时来成功重新创建一个for循环或TimerInterval将无法正常运行它必须是一系列操作

像这样

    let wait = SKAction.wait(forDuration: 1)
    let spawn = SKAction.run {
        //be sure to call your spawn function here
        example()
    }
    let sequence = SKAction.sequence([wait, spawn])
    self.run(SKAction.repeatForever(sequence))

如果做得好,它应该像我一样工作

    func example() {
    //the person node is equal to an SKSpriteNode subclass 
    person = people()
    func random() -> CGFloat {
    //this is the random spawn generator increasing or decreasing these two numbers will change how far or how close they spawn together
        return CGFloat(Float(arc4random_uniform(UInt32(500 - 343))))
    }

    func random(min: CGFloat, max: CGFloat) -> CGFloat {
        return random() * (max - min) + min
    }

    func spawnPeople() {
        //note that this is set to spawn them on the x-axis but can easily be changed for the y-axis
        let position = CGPoint(x: 2 * random(min: 0, max: 1),y: -290)

        if positionIsEmpty(point: position) {
        //set the position of your node and add it to the scene here any actions you want to add to the node will go here
            person.position = position
            self.addChild(person)
            //example 
            person.run(parallax1)
        }
    }

    func positionIsEmpty(point: CGPoint) -> Bool {
        if (person.frame.contains(point)) {
            print("failed")
            return false
        }
        print("success")
        return true
    }
    //make sure to call the spawn(name)() function down here or the code will not run
    spawnPeople()
}

我在didMoveToView函数中也有所有这些代码