【问题标题】:run and jump and return to running SpriteKit Swift跑跳并返回运行 SpriteKit Swift
【发布时间】:2017-08-06 00:54:18
【问题描述】:

我是使用 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


}

我觉得我必须用另一个 else 语句在 thouchesBagan 中做点什么。

提前致谢!莫伊

【问题讨论】:

  • 额外信息:我在 Actions.sks 文件中的 runaction 是 repeatForever。这可能是问题吗?如果是这样,我如何从第一次点击开始运行,再次点击跳跃,当角色再次着陆时运行(点击,下一次点击应该初始化跳跃)

标签: xcode swift3 sprite-kit action game-physics


【解决方案1】:

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

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

就在下面

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    相关资源
    最近更新 更多