让角色顺利跳跃

时间:2016-11-06 22:17:58

标签: swift sprite-kit physics

我正在进行跳跃游戏,我试图使角色跳跃顺畅,但它看起来像是飞行而不是跳跃

(视频附在主题末尾)

touchesBegan func

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

    //Check if the user is on PlayScene

    if scene?.name == "PlayScene" {

    //start timer on touch
    TouchTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerStart), userInfo: nil, repeats: true)
    }

for touch: AnyObject in touches {

    //If the Game is NOT Over!
    if isgameOver != true {

        //Before first touch
        if (isFirstBlock == true) {

            //Tap first time
            isFirstBlock = false

            //Call Run Action
            self.ojiRunFunc(sprite: ojiGame, name: "OjiRun")
            ojiGame.physicsBody?.affectedByGravity = true
            spawn = SKAction.run({
                () in
                self.createBlocks()
            })

            let delay = SKAction.wait(forDuration: 1.0)
            let spawnDelay = SKAction.sequence([spawn, delay])
            let spawnDelayforever = SKAction.repeatForever(spawnDelay)
            self.run(spawnDelayforever)

        }

        playSound(soundName: "Jump")
        ojiGame.physicsBody?.velocity.dy = 0
        jumpActionfunc()
        //**********************************************************//

        //The game is OVER!
    } else {
        TouchTimer.invalidate()
    }

timerStart func

func timerStart() {
    //Add time
    timeTouch = timeTouch + 0.1
    jumpActionfunc()
}

jumpActionFunc

func jumpActionfunc () {

    if (timeTouch <= 0.1 && timeTouch <= 0.2) {
        ojiGame.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30))
        print("1")
    }else if (timeTouch >= 0.3 && timeTouch <= 0.4) {
        ojiGame.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 10))
        print("2")
    }else if (timeTouch >= 0.5 && timeTouch <= 0.6) {
        ojiGame.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 15))
        print("3")
    }else if (timeTouch >= 0.7 && timeTouch <= 0.8) {
        ojiGame.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 20))
        print("4")
    }

}

touchesEnded func

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    ojiGame.physicsBody?.applyImpulse(CGVector(dx: 0, dy: -12))
    //Reset timer after touch ended
    TouchTimer.invalidate()
    timeTouch = 0.0

}

视频: here's the jump in my game

0 个答案:

没有答案