2014-09-28 118 views
1

我可以製作一個隨機選擇並從屏幕頂部到底部的SK節點數組。例如,假設我有25個左右不同的平臺,這些平臺將會在肖像iPhone上掉下來。我需要它從數組中隨機選擇一個平臺開始,然後在一定時間/或像素空間之後隨機選擇另一個以繼續相同的動作,直到達到底部等等。Im新到快速但有相當體面的瞭解它。我還沒有找到如何創建一個SKsprite節點數組。有人可以幫忙嗎?快速移動多個精靈節點

到目前爲止,我已經能夠獲得任何類似於我想要的效果的唯一方法是將每個節點放在屏幕上並將它們添加到字典中,並使它們像這樣移動

類ObstacleStatus {

var isMoving = false 
    var timeGapForNextRun = Int(0) 
    var currentInterval = Int(0) 
    init(isMoving: Bool, timeGapForNextRun: Int, currentInterval: Int) { 
     self.isMoving = isMoving 
     self.timeGapForNextRun = timeGapForNextRun 
     self.currentInterval = currentInterval 
} 

func shouldRunBlock() -> Bool { 
    return self.currentInterval > self.timeGapForNextRun 

} 

func moveBlocks(){ 
    for(blocks, ObstacleStatus) in self.blockStatuses { 
     var thisBlock = self.childNodeWithName(blocks) 
     var thisBlock2 = self.childNodeWithName(blocks) 
     if ObstacleStatus.shouldRunBlock() { 
      ObstacleStatus.timeGapForNextRun = randomNum() 
      ObstacleStatus.currentInterval = 0 
      ObstacleStatus.isMoving = true 
     } 

     if ObstacleStatus.isMoving { 
      if thisBlock?.position.y > blockMaxY{ 
        thisBlock?.position.y -= CGFloat(self.fallSpeed) 
      }else{ 
       thisBlock?.position.y = self.origBlockPosistionY 
       ObstacleStatus.isMoving = false 
      } 
     }else{ 
       ObstacleStatus.currentInterval++ 
      } 

    } 
} 

使用該用於該隨機函數

func randomNum() -> Int{ 
return randomInt(50, max: 300)   
} 

func randomInt(min: Int, max:Int) -> Int { 
    return min + Int(arc4random_uniform(UInt32(max - min + 1))) 
} 

這一切對我來說都是以隨機的時間間隔往下移動,但是增加隨機數的最小或最大值並不會影響間隙的實際時間。我需要能夠指定距離或時間差距。

+0

您應該添加您迄今嘗試的內容。 – 2014-09-28 07:51:33

+0

增加了我用過的一些東西。 – Alex 2014-09-28 08:45:42

回答

1

許多可能的解決方案之一是創建一個遞歸調用自己的下降動作序列,直到沒有剩下的平臺節點。您可以控制平均「間隔時間」和其隨機變化的範圍。下面是一個工作示例(假設iOS SpriteKit遊戲模板):

import SpriteKit 

extension Double { 
    var cg: CGFloat { return CGFloat(self) } 
} 

extension Int { 
    var cg: CGFloat { return CGFloat(self) } 
} 

func randomInt(range: Range<Int>) -> Int { 
    return range.startIndex + Int(arc4random_uniform(UInt32(range.endIndex - range.startIndex))) 
} 

extension Array { 
    func randomElement() -> Element? { 
     switch self.count { 
     case 0: return nil 
     default: return self[randomInt(0..<self.count)] 
     } 
    } 

    func apply<Ignore>(f: (T) -> (Ignore)) { 
     for e in self { f(e) } 
    } 
} 

class GameScene: SKScene { 

    var screenWidth: CGFloat { return UIScreen.mainScreen().bounds.size.width } 
    var screenHeight: CGFloat { return UIScreen.mainScreen().bounds.size.height } 

    let PlatformName = "Platform" 
    let FallenPlatformName = "FallenPlatform" 

    func createRectangularNode(#x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) -> SKShapeNode { 
     let rect = CGRect(x: x, y: y, width: width, height: height) 
     let path = UIBezierPath(rect: rect) 
     let node = SKShapeNode(path: path.CGPath) 
     return node 
    } 

    func createPlatformNodes(numNodes: Int, atHeight: CGFloat) -> [SKShapeNode] { 
     var padding = 20.cg 
     let width = (screenWidth - padding)/numNodes.cg - padding 
     padding = (screenWidth - width * numNodes.cg)/(numNodes.cg + 1) 
     let height = width/4 
     var nodes = [SKShapeNode]() 
     for x in stride(from: padding, to: numNodes.cg * (width + padding), by: width + padding) { 
      let node = createRectangularNode(x: x, y: atHeight, width: width, height: height) 
      node.fillColor = SKColor.blackColor() 
      node.name = PlatformName 
      nodes.append(node) 
     } 
     return nodes 
    } 

    func createFallingAction(#by: CGFloat, duration: NSTimeInterval, timeGap: NSTimeInterval, range: NSTimeInterval = 0) -> SKAction { 
     let gap = SKAction.waitForDuration(timeGap, withRange: range) 
//  let fall = SKAction.moveToY(toHeight, duration: duration) // moveToY appears to have a bug: behaves as moveBy 
     let fall = SKAction.moveByX(0, y: -by, duration: duration) 
     let next = SKAction.customActionWithDuration(0) { [unowned self] 
      node, time in 
      node.name = self.FallenPlatformName 
      self.fallNextNode() 
     } 
     return SKAction.sequence([gap, fall, next]) 
    } 

    func fallNextNode() { 
     if let nextNode = self[PlatformName].randomElement() as? SKShapeNode { 
      let falling = createFallingAction(by: screenHeight * 0.7, duration: 1, timeGap: 2.5, range: 2) // mean time gap and random range 
      nextNode.runAction(falling) 
     } else { 
      self.children.apply { ($0 as? SKShapeNode)?.fillColor = SKColor.redColor() } 
     } 
    } 

    override func didMoveToView(view: SKView) { 
     self.backgroundColor = SKColor.whiteColor() 
     for platform in createPlatformNodes(7, atHeight: screenHeight * 0.8) { 
      self.addChild(platform) 
     } 
     fallNextNode() 
    } 
} 
+0

謝謝你,那是我希望的更先進一點,我會試着弄清楚一切是如何運作的。 – Alex 2014-09-29 08:50:09

+0

如果您有關於代碼的具體問題,請告知我。順便說一下,您可以將其複製/粘貼到iOS SpriteKit遊戲模板生成的默認GameScene代碼的位置,然後您就可以看到它在運行。 – milos 2014-09-29 12:01:23