2016-07-12 22 views
0

我有一個在一個快速遊戲中擁有相同動作列表的幾個精靈(技術上[SKAction])。他們在動作序列中都處於不同的位置,我需要按照最完整的順序排列它們(採取行動),使其最不完整。我如何獲得精靈的進度(或者至少是它所在的動作的步驟)?優選地,我希望獲得它所在步驟的完成百分比。Swift:檢查SKActions的進度

對於示例代碼的緣故:

let sprite1 = SKShapeNode.init(rectOf: CGSize.init(width: 20, height: 20), cornerRadius: 5) 
    let sprite2 = SKShapeNode.init(rectOf: CGSize.init(width: 20, height: 20), cornerRadius: 5) 
    let sprite3 = SKShapeNode.init(rectOf: CGSize.init(width: 20, height: 20), cornerRadius: 5) 

    sprite1.position = CGPoint(x: -100, y: 0) 
    sprite2.position = CGPoint(x: 0, y: 0) 
    sprite3.position = CGPoint(x: 100, y: 0) 

    let sequence : [SKAction] = [SKAction.move(by: CGVector(dx: 50, dy: 0), duration: 1.5), 
           SKAction.rotate(byAngle: CGFloat(M_PI/2), duration: 1.0), 
           SKAction.move(by: CGVector(dx: 0, dy: 50), duration: 1.5), 
           SKAction.rotate(byAngle: CGFloat(M_PI/2), duration: 1.0), 
           SKAction.move(by: CGVector(dx: -50, dy: 0), duration: 1.5)] 

    sprite1.run(SKAction.sequence(sequence)) 
    //wait 1 second 
    sprite2.run(SKAction.sequence(sequence)) 
    //wait another second 
    sprite3.run(SKAction.sequence(sequence)) 

    var spriteAccord : [SKSpriteNode] = //insert ranking code 
+0

百分比請發表您的真正的代碼,這並不編譯 – Eric

+1

你需要根據時間來排序,或者他們是在 – Knight0fDragon

+0

什麼行動這將是他們的行動(因爲我忘了提及他們可以以不同的速度跑)。因此,我要拍攝開始時間字典,並使用不同的速度來計算沿着序列的「距離」,並使用距離來獲得排名 –

回答

1

只要精靈的速度是恆定的(又名沒有子彈時間效果)我只想用字典來存儲起始時間:

var startingTimes = [SKNode:NSTimeInterval]() 

然後存儲在一個精靈開始他的序列

sprite1.run(SKAction.sequence(sequence)) 
startingTimes[sprite1 as! SKNode] = currentTime 

最後,排序由startingTime

let sortedStartingTimes = startingTimes.sort(){ 
          (time1,time2)->Bool in 
          return time1 <= time2 
         } 

然後,只需迭代日不管你需要在這裏做什麼,粗略的字典,或抓住第一個項目。

這可以突然出現在操場,以測試上述代碼:

var startingTimes = [String:NSTimeInterval]() 
startingTimes["A"] = 1 
startingTimes["C"] = 3 
startingTimes["B"] = 2 

let sortedStartingTimes = startingTimes.sort(){ 
    (time1,time2)->Bool in 
    return time1 <= time2 
} 
print("start") 
for time in startingTimes 
{ 
    print(time) 
} 
print("sort") 
for time in sortedStartingTimes 
{ 
    print(time) 
} 

要獲得完成的百分比是一個有點複雜。你需要把你的currentTime - startingTime,然後根據你在你的精靈的位置找出它。

所以我總共有3個步驟,每個1秒。

我的currentTime - startingTime是1.5秒。

var deltaTime = currentTime - startingTime 

予取1.5 - 步驟1的時間,所以結果是0.5秒

deltaTime -= step1Time 

的DeltaTime> 0,我在一個新的步驟

予取0.5 - 步驟2時間,所以結果是0.5秒

deltaTime -= step2Time 

的DeltaTime < = 0我在這個步驟

這樣的DeltaTime/step2time,那是我在第二步時間表

let completionPercentage = deltaTime/step2Time 
1

您可以將您的現有操作之間runBlock行動。在塊中,您可以通過增加計數器來跟蹤每個節點通過操作的進度。