gpt4 book ai didi

ios - 恒定速度,同时仍然能够施加脉冲

转载 作者:可可西里 更新时间:2023-11-01 01:37:11 24 4
gpt4 key购买 nike

我正在制作一款游戏,其中主要 Sprite /玩家不断移动,他/她需要跳过障碍。

我需要有关如何为我的移动 Sprite 设置恒定速度的帮助。当我尝试在 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.
}

最佳答案

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

override func update(currentTime: CFTimeInterval) {

updateSpritePosition()
mainSprite.physicsBody?.velocity = CGVectorMake(400, mainSprite.physicsBody?.velocity.dy)
}

关于ios - 恒定速度,同时仍然能够施加脉冲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35710481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com