-1

我有我正在制作的这个游戏,在游戏中玩家通过在这个梁状地面下方或上方移动来躲避阻挡。到目前为止,我已经把所有东西都放下了,除了当他向下翻转时英雄不会向后翻转。

这是我使用的代码:

func flipDown() { 
    isUpsideDown = !isUpsideDown

    var scale: CGFloat
    if isUpsideDown { 
    scale = -1.0
    } else { 
        scale = 1.0
    } 

    let translate = SKAction.moveByX(0, y: scale * (size.height + kMLGroundHeight), duration: 0.1)
    let flip = SKAction.scaleYTo(scale, duration: 0.1)
    runAction(translate)
    runAction(flip)
} 
4

1 回答 1

0

我的建议(对不起,我可怜的斯威夫特,我真的不知道):

1)您必须添加isFlipping属性以计数后续调用并重命名isUpsideDownisAtBottom(口味问题,但更好地描述其目的):

... 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 在半空中,它会被翻转或忽略

于 2016-01-04T04:43:52.797 回答