Ios 恒定速度,同时仍能施加脉冲

Ios 恒定速度,同时仍能施加脉冲,ios,swift,sprite-kit,physics,Ios,Swift,Sprite Kit,Physics,我正在制作一个游戏,当主要的精灵/玩家不断移动时,他/她需要跳过障碍 我需要帮助如何设置一个恒定的速度为我的移动精灵。当我尝试在SpriteKit更新功能中执行此操作时,我无法在用户点击屏幕时应用跳转冲动 这是我的密码。我评论了我遇到问题的地方: override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { /* Called when a touch begins */ i

我正在制作一个游戏,当主要的精灵/玩家不断移动时,他/她需要跳过障碍

我需要帮助如何设置一个恒定的速度为我的移动精灵。当我尝试在SpriteKit更新功能中执行此操作时,我无法在用户点击屏幕时应用跳转冲动

这是我的密码。我评论了我遇到问题的地方:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */
    if (gameStarted == false) {
        gameStarted = true

        mainSprite.physicsBody?.affectedByGravity = true
        mainSprite.physicsBody?.allowsRotation = true
        let spawn = SKAction.runBlock({
            () in

            self.createWalls()
        })


        let delay = SKAction.waitForDuration(1.5)

        let spawnDelay = SKAction.sequence([spawn, delay])

        let spawnDelayForever = SKAction.repeatActionForever(spawnDelay)

        self.runAction(spawnDelayForever)

        let distance = CGFloat(self.frame.height + wallPair.frame.height)

        let movePipes = SKAction.moveByX(0, y: -distance - 50, duration: NSTimeInterval(0.009 * distance)) // Speed up pipes

        let removePipes = SKAction.removeFromParent()

        moveAndRemove = SKAction.sequence([movePipes, removePipes])

    } else {
        if died == true {

        }
        else {
            mainSprite.physicsBody?.applyImpulse(CGVectorMake(0, 20)) // TRYING TO APPLY AN IMPULSE TO MY SPRITE SO IT CAN JUMP AS IT MOVES
        }
    }



    for touch in touches {
        let location = touch.locationInNode(self)
    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    updateSpritePosition()
    mainSprite.physicsBody?.velocity = CGVectorMake(400, 0) // SETS A CONSTANT VELOCITY, HOWEVER I CAN NOT APPLY AN IMPULSE.
}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
如果(gameStarted==false){
gameStarted=真
mainSprite.physicsBody?重力影响=真
mainSprite.physicsBody?.allowsRotation=true
让spawn=SKAction.runBlock({
()在
self.createWalls()
})
让延迟=SKAction.waitForDuration(1.5)
让spawnDelay=SKAction.sequence([spawn,delay])
让spawnDelayForever=SKAction.repeatActionForever(spawnDelay)
self.runAction(永远)
让距离=CGFloat(self.frame.height+wallPair.frame.height)
让movePipes=SKAction.moveByX(0,y:-距离-50,持续时间:NSTimeInterval(0.009*距离))//加快管道速度
让removePipes=SKAction.removeFromParent()
moveAndRemove=SKAction.sequence([movePipes,removepippes])
}否则{
如果死亡==真{
}
否则{
mainSprite.physicsBody?.applyImpulse(CGVectorMake(0,20))//尝试对我的精灵应用一个脉冲,以便它可以在移动时跳跃
}
}
接触{
let location=touch.locationInNode(自)
}
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
updateScreetPosition()
mainSprite.physicsBody?.velocity=CGVectorMake(400,0)//设置恒定速度,但我无法应用脉冲。
}

问题在于您正在覆盖
更新方法中的速度。因此,即使您添加了一个脉冲,它也会立即被
更新中的代码覆盖。尝试只覆盖速度的
dx
部分

override func update(currentTime: CFTimeInterval) {

    updateSpritePosition()
    mainSprite.physicsBody?.velocity = CGVectorMake(400, mainSprite.physicsBody?.velocity.dy)
}
非常感谢D