2016-01-14 115 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函數中的所有代碼

相關問題