2017-05-04 100 views
3

我正在做一些游戏的死亡动画,并想寻求一些帮助。我希望我的怪物能够在一阵烟雾中消失,但不会在它激发穿过他身体的斜线效果之前消失。SKAction排序和分组动画

我有3个动画,我想用:

weaponSlash - 跨怪物绘制一条线。看起来你用剑砍了他。

- 一缕青烟慢慢扩大了

monsterFalling - 怪物回落,吓了一跳

我想要做的就是在这个顺序播放:

  1. 同时,斜线出现&怪物开始回落
  2. Abou t 0.25s进入上面的动画,我想云开始出现
  3. 当云即将结束(所以也许在1s后)我想怪物消失
  4. 删除烟雾,怪物,剑等,并丢弃一些硬币在地上

我开始喜欢这一点,因为的作品有些测试:(忽略上述时间现在)

//Cancel any current actions, like a monster attacking 
monster.removeAllActions() 

//since you can't play 3 animations on one node at the same time, you have to create 3 separate nodes for everything       
let slash = SKSpriteNode() 
let cloud = SKSpriteNode() 
cloud.size = monster.size 
slash.size = monster.size 
monster.addChild(cloud) 
monster.addChild(slash) 


//Start the slash animation 
slash.run(self.player.currentlyEquippedWeapon.attackAnimation()) 

//Start the cloud animation (how I get it is elsewhere and not relevant) 
cloud.run(cloudAnimation) 

//Run the monster death animation, followed by the cleanup/coin dropping     
monster.run(SKAction.sequence([monster.deathAnimation(), SKAction.wait(forDuration: 1), postDeathActions])) 

上面的变量PostDeathActions只是移除怪物节点并为一些硬币下落提供动画。


,我需要一些帮助

所以上面的代码不会彼此独立的所有运行工作如此之大的动画。在此基础上,你可以看到无论是完成斜线还是云,怪物都会执行两个动作:他回退,然后进行清理,这会清除怪物并产生硬币。正如你所看到的,我试图通过增加1秒的延迟来延迟这一点,但这有点破解,因为我可能有不同的怪物或攻击等,这些更快/更慢。我宁愿保证,所有事情在我将这个怪物消灭之前完成。

我想这组到SKAction.Run像这样:

let preDeath = SKAction.run { 
[unowned self] in 
    monster.run(monster.deathAnimation() 
    slash.run(self.player.currentlyEquippedWeapon.attackAnimation()) 
    cloud.run(cloudAnimation) 
} 

但这再次运行在同一时间一切。

我想要做的是序列像这样(伪代码):

让preDeathAnimations = SKAction.Group([斜线,云,monsterDeathAnimation]) ])

SKAction。序列([preDeathAnimations,postDeathActions])

所以这种方式在运行清理前运行全部3个。

有没有办法做这样的事情?我知道Sequnce/Group需要对SKNode运行,但我没有3个独立的。

感谢您阅读本文以及您可以提供的任何建议!

回答

1

这是我的一个想法,但是你可以使用线程+ state + onCompletion块来计算它的数学。我没有测试出来完全但这一般概念应该工作:

let slash = SKAction.fadeIn(withDuration: 0.5) 

let fall = SKAction.fadeOut(withDuration: 0.25) 

let puff = SKAction.fadeIn(withDuration: 0.1) 

// Put in ALL of the actions from ALL parties that you want to happen prior to puff: 
func findLongestTime(from actions: [SKAction]) -> TimeInterval { 

    var longestTime = TimeInterval(0) 

    for action in actions { 
    if action.duration > longestTime { longestTime = action.duration } 
    } 

    // Note, if you put a sequence into this function I don't know if it will work right.. 
    // Might need another func like `findDurationOfSequence(_ sequence: SKAction) -> TimeInterval 
    return longestTime 
} 

// Note, if you have the monster doing more than falling prior to puff, then you will 
// need to subtract those as well: 
let monsterActionsPriorToPuff = [fall] 

// Add the duration of all monster animations prior to puff: 
var MAPTP_duration = TimeInterval(0) 
for action in monsterActionsPriorToPuff { 
    MAPTP_duration += action.duration 
} 

// Calculate our final wait time, with no negative numbers: 
    var waitTime = findLongestTime(from: [slash, fall]) - MAPTP_duration 
if waitTime < 0 { waitTime = 0 } 
let wait = SKAction.wait(forDuration: waitTime) 

// Our monster sequence (I forgot to add the disappear, just add after puff) 
let monsterSequence = SKAction.sequence([fall, wait, puff]) 

// Player slashes: 
SKSpriteNode().run(slash) 

// Monster will wait 0.25 seconds after falling, 
// for slash to finish before puffing: 
SKSpriteNode().run(monsterSequence) 

等我知道这个想法是行不通的,我可以尝试更新它。

+0

哇,这很有趣..让我稍后详细阅读。感谢您的帮助 – NullHypothesis

+0

@NullHypothesis np。希望它有用。我认为粉扑是消失的动画。所以如果你想在粉扑之后再拍一张动画,那么你需要调整其他的代码。此外,对不起,这不是过度复制/粘贴,应该只是一个想法,如何得到你想要的 – Fluidity