2016-01-14 78 views
2

我喜欢,所以我的屏幕上的每一个节点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: "[email protected]") 
    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) 
} 

然而,当他们被催生,有的交叉,躺在彼此顶部?有没有办法来防止这种情况?提前致谢。

+0

您是否尝试过使用碰撞检测?因此为每个节点打开物理,并关闭重力。 –

+0

我其实从来没有想到这一点。谢谢!但是,这将如何阻止它们重叠? – IHeartAppsLLC

+0

他们可能仍然重叠,但他们会(希望)彼此离开。只要给它一下,看看它是否有预期的效果。它足够快实施。我能想到的唯一方法就是在节点添加到场景之前检查节点是否存在于某个位置。我会以第二种方式做出答案。 –

回答

1

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

您可能希望将支票放入循环中,以便如果取得该位置,它将使用新生成的点重试。否则,你会得到一些不会显示的点。取决于你在做什么。

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: "[email protected]") 
    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 
} 
+0

哇,谢谢。这个功能非常好,我没有碰撞检测的运气。 – IHeartAppsLLC

0

这里是这迅速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函数中的所有代码

相关问题