问题描述:

I have this game I'm making and in the game the player dodges block by moving under or over this beam-like ground. So far I have everything down except for the fact that the hero doesn't flip back up when he flips down.

This is the code i used:

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)

}

网友答案:

My suggestion (sorry fo my poor swift, i'm don't really know it):

1) You must add isFlipping property to count for subsequent calls and rename isUpsideDown to isAtBottom (matter of taste, but better describes its purpose):

... blah-blah-blah initialization

self.isFlipping = false
self.isAtBottom = true
self.flipDuration = 0.1

2) next, separate flip and animation logic

... 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
        )   
}

Now you can call hero.makeFlip(), and it will be flipped, or ignored, if hero is mid-air

相关阅读:
Top