-1

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)
} 
Dieblitzen
  • 544
  • 4
  • 22
  • 2
    Where is the code that's supposed to cause "the hero [to] flip back up"? – KSFT Jan 03 '16 at 02:18
  • 1
    `except for the fact that the hero doesn't flip back up `, for that, `flipDown()` must be called twice: first to flip down, second to flip back up. And where in your code is second call? – ankhzet Jan 03 '16 at 02:20
  • @KSFT the function was called flip() only. it was supposed to do both flip down and flip up. but when it that didn't work, i tried to make 2 different functions. one to flip down and one to flip back up – FreshMinded Jan 03 '16 at 22:36
  • @ankhzet this function was supposed to do both but it didn't – FreshMinded Jan 03 '16 at 22:37

1 Answers1

0

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

ankhzet
  • 2,517
  • 1
  • 24
  • 31