运行 并跳转 return 到 运行ning SpriteKit Swift

run and jump and return to running SpriteKit Swift

我是 SpriteKit 和 Swift 编程的新手。我遵循了一些教程,但我现在坚持了几天。

到目前为止,我唯一的其他选择是同时删除 运行 动画作为跳跃动画。他们不会卷土重来了。

这是我的英雄文件,如果有 运行 和跳跃的动作:

func runPlayer(){

        let playerRun:SKAction = SKAction(named: "run", duration: moveSpeed)!
        let runAction:SKAction = SKAction.moveBy(x: 0, y: 0, duration: moveSpeed)
        let runGroup:SKAction = SKAction.group([playerRun, runAction])
        thePlayerRuns.run(runGroup, withKey: "run")
        thePlayerRuns.physicsBody?.isDynamic = false
        thePlayerRuns.position = CGPoint(x: 0 , y: 0)
        thePlayerRuns.size = CGSize(width: 160.25, height: 135.55)
        thePlayerRuns.zPosition = 99

        addChild(thePlayerRuns)

    }

    func jumpPlayer(){

        let playerJump: SKAction = SKAction(named: "jump", duration: moveSpeed)!
        let jumpUp: SKAction = SKAction.moveBy(x: 0, y: 50, duration: 0.5)
        let jumpDown: SKAction = SKAction.moveBy(x: 0, y: -80, duration: 0.5)
        let jumpSeq: SKAction = SKAction.sequence([jumpUp, jumpDown])
        let jumpGroup: SKAction = SKAction.group([playerJump, jumpSeq])
        thePlayerJumps.run(jumpGroup, withKey: "jump")
        thePlayerJumps.physicsBody?.affectedByGravity = true
        thePlayerJumps.physicsBody?.allowsRotation = false
        thePlayerJumps.physicsBody?.isDynamic = true
        thePlayerJumps.position = CGPoint(x: 0 , y: 30)
        thePlayerJumps.size = CGSize(width: 160.25, height: 135.55)
        thePlayerJumps.zPosition = 99
        addChild(thePlayerJumps)

        thePlayerRuns.removeFromParent()


        thePlayerJumps.run(
            SKAction.sequence([SKAction.wait(forDuration: 1.0),SKAction.removeFromParent()])
        )

//        thePlayerRuns.run(
//            SKAction.sequence([SKAction.removeFromParent()])
//            
//        )

        let otherWait = SKAction.wait(forDuration: 1)
        let otherSequence = SKAction.sequence([otherWait, SKAction(runPlayer())])
        run(otherSequence)

        /*
        let otherWait = SKAction.waitForDuration(1)
        let otherSequence = SKAction.sequence([otherWait, SKAction.repeatActionForever(sequence)])
        runAction(otherSequence)
        */
    }

这是我的游戏场景:

hero = MTHero()

    hero.position = CGPoint(x: 70, y: 278)

    addChild(hero)
    hero.idlePlayer()

}


func start(){

    isStarted = true
    hero.stop()
    movingGround.start()
    hero.runPlayer()

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    if !isStarted {
        start()
    }else{
        hero.jumpPlayer()
    }

    }

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered


}

我想我必须在 thouchesBagan 中用另一个 else 语句做一些事情。

提前致谢!我

我已经设法解决了。我不确定这是最好的选择。但是在我的 jumpPlayer 函数中,我添加了以下代码:

self.run(SKAction.wait(forDuration: 1)) {
        self.addChild(self.thePlayerRuns)

略低于

thePlayerJumps.run(
        SKAction.sequence([SKAction.wait(forDuration: 1.0),SKAction.removeFromParent()])
    )

如果您有更好的解决方案建议,请告诉我。