swift - pause animation whilst jumping

I'm working on a platform style game where a cat jumps over obstacles. Currently the cat is animated to give the illusion of the cat running. I'm looking for the animation to pause whist the cat is jumping then resume once on the ground.

My jumping method allows the cat to jump when it's not traveling on the y axis, this is run under my overide func update

I've tried stopping the cat sequence (animation) in touchesBegan but I don't know how to resume the animation once the cat has landed. Ideally I would like the cat to be on catTexture1 when the animation is paused.

Here is where I've got up to, the cat runs along and once a touch happens the cat animation stops, the cat jumps but doesn't animate once landed.

Would anyone know the answer?

override func didMoveToView(view: SKView) {

    var catTexture1 = SKTexture(imageNamed: "Cat1")
    catTexture1.filteringMode = SKTextureFilteringMode.Nearest
    var catTexture2 = SKTexture(imageNamed: "Cat2")
    catTexture2.filteringMode = SKTextureFilteringMode.Nearest

    var anim = SKAction.animateWithTextures([catTexture1, catTexture2], timePerFrame: 0.2)
    var run = SKAction.repeatActionForever(anim)

    cat = SKSpriteNode(texture: catTexture1)
    cat.position = CGPoint(x: self.frame.size.width / 2.2, y: self.frame.size.height / 7.0 )
    cat.runAction(run)

    cat.physicsBody = SKPhysicsBody(circleOfRadius: cat.size.height / 2.0)
    cat.physicsBody!.dynamic = true
    cat.physicsBody!.allowsRotation = false
    cat.physicsBody?.categoryBitMask = catCategory
    cat.physicsBody?.collisionBitMask = crowCategory | worldCategory
    cat.physicsBody?.contactTestBitMask = crowCategory | contact2Category

    moving.addChild(cat)

}

override func  touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    if ableToJump == true {
    if (moving.speed > 0){

        var stopCat = SKAction.runBlock({() in self.killCatSpeed()})
        var catSequence = SKAction.sequence([stopCat])
        cat.runAction(catSequence)
        cat.physicsBody!.velocity = CGVectorMake(0, 0)
        cat.physicsBody!.applyImpulse(CGVectorMake(0, 30))

                } else if (canRestart) {
                    self.resetScene()

        }
    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

    if (moving.speed > 0){
       cat.zRotation = self.clamp( -1, max: 0.2, value: cat.physicsBody!.velocity.dy * ( cat.physicsBody!.velocity.dy < 0 ? 0.003 : 0.001 ) );cat.zRotation = self.clamp( -1, max: 0.2, value: cat.physicsBody!.velocity.dy * ( cat.physicsBody!.velocity.dy < 0 ? 0.003 : 0.001 ) );

    }  

    if cat.physicsBody?.velocity.dy == 0 {
        ableToJump = true
    }
    else {
        ableToJump = false

    }

}

Answers


You could have a variable, hasTapped = false. When the user taps, set that to true.

You could then setup a physics body for the ground and collision detection so when the cat sprite touches the ground, it runs some code to start off the animation again.

Only start the animation again if hasTapped == true so it is only listening for the collision if the user has tapped (the cat is in the air).


The state design pattern can be used to switch between animation states. First, define the various states:

enum State {
    case Running
    case Jumping
    case Stopped
}

You can then use a combination of computed and stored properties to switch between the various states.

class GameScene: SKScene {
    var cat:SKSpriteNode!
    var run:SKAction!
    var jump:SKAction!
    var stoppedTexture:SKTexture!
    var _state:State = .Stopped
    var state:State {
        get {
            return _state
        }
        set {
            cat.removeActionForKey("animate")
            switch(newValue) {
            case .Running:
                // Start running animation sequence
                cat.runAction(run,withKey:"animate")
            case .Jumping:
                // Start jumping animation sequence
                cat.runAction(jump,withKey:"animate")
            case .Stopped:
                // Change texture to the stopped image
                cat.texture = stoppedTexture
            }
            _state = newValue
        }
    }

When you set the computed property store, the setter method is called and the appropriate animation action executes. For example, the statement

state = .Running

will start the running animation sequence.


Thank you for all your answers, I solved this by simply changing the speed of the cat in my update func.

If the cat isn't traveling on the y axis i set cat.speed = 1 and if it is traveling (or jumping), I set cat.speed = 0.

Here is how the update func looks now...

    if cat.physicsBody?.velocity.dy == 0 {
        ableToJump = true
        cat.speed = 1


    }
    else {
        ableToJump = false
        cat.speed = 0
    }

Need Your Help

What is right Angular way for triggering handsontable loadData by mouse click

jquery angularjs angularjs-directive handsontable

I create directive that loads jquery handsontable in a true Angular way. But this is the true way only for the first loading of handsontable. What if I need to change table data after the mouse cli...

Why would a static inner interface be used in Java?

java interface static

I have just found a static inner interface in our code-base.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.