我的建议(对不起,我可怜的斯威夫特,我真的不知道):
1)您必须添加isFlipping
属性以计数后续调用并重命名isUpsideDown
为isAtBottom
(口味问题,但更好地描述其目的):
... blah-blah-blah initialization
self.isFlipping = false
self.isAtBottom = true
self.flipDuration = 0.1
2)接下来,分离翻转和动画逻辑
... blah-blah-blah implementations
let flipScale() -> {
return self.isAtBottom ? 1.0 : -1
}
func makeFlip() {
if self.isFlipping then
return
// first press, can actualy animate
self.isFlipping = true
self.runFlipAction(self.flipScale(), () -> Void = {
// completion block, runs on flip finished
self.isFlipping = false
self.isAtBottom = !self.isAtBottom
})
}
let runFlipAction(scale: CGFloat, after: () -> Void) {
let translate = SKAction.moveByX(
0,
y: scale * (size.height + kMLGroundHeight),
duration: self.flipDuration
)
let flip = SKAction.scaleYTo(scale, duration: self.flipDuration)
runAction(
// must run in a group, in order to properly handle completion anchor time
SKAction.group([translate, flip]),
completion: after
)
}
现在你可以调用hero.makeFlip()
,如果 hero 在半空中,它会被翻转或忽略