如何在不导致 fps 滞后的情况下移动精灵
How to move a sprite without causing fps lag
我正在创建一个 spritekit 游戏,我对 swift 还很陌生。我想要两个按钮让玩家向右或向左移动。当按下一个按钮时,比如左按钮,精灵必须开始向左移动而不停止。当它撞到左边的墙时,它会改变方向并向右移动到另一面墙,依此类推......我设法让精灵通过使用更新功能来做到这一点。每次调用它时,它都会检查玩家是否按下按钮并相应地移动精灵,但是,它会导致 FPS 延迟(FPS 会下降到 50)。
我尝试使用 MoveBy 和 MoveTo 等 SKActions,但无法重新创建我希望精灵执行的操作。
所以我的问题是:如何使用这两个按钮使精灵按照我想要的方式移动而不导致 FPS 延迟。任何帮助,将不胜感激。谢谢
以下是我在更新函数中调用的函数,这些函数有效但导致延迟。
func moveRight() {
sprite.xScale = 1
sprite.position.x += 4
}
func moveLeft() {
sprite.xScale = -1
sprite.position.x -= 4
}
试试这个代码:
它会在按下按钮时一直运行移动动作,而在释放按钮时会删除动作
这将使玩家充满希望地移动而不会降低帧率。要在精灵撞到墙壁时改变精灵的方向,您必须检查碰撞。当它撞到墙上时,您可以检查正在应用的是 leftMove 还是 rightMove 动作,然后删除该动作并开始相反的动作。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if(leftButton.contains(location) { // check if left button was pressed
moveLeft()
} else if(rightButton.contains(location) { //check if right button was pressed
moveRight()
}
}
}
func moveLeft() {
//Check if it's already moving left, if it is return out of function
if((sprite.action(forKey: "leftMove")) != nil) {
return
}
//Check if its moving right, if it is remove the action
if((sprite.action(forKey: "rightMove")) != nil) {
sprite.removeAllActions()
}
//Create and run the left movement action
let action = SKAction.move(by: -100, duration: 1)
sprite.run(SKAction.repeatForever(action), withKey: "leftMove")
}
func moveRight() {
//Check if it's already moving right, if it is return out of function
if((sprite.action(forKey: "rightMove")) != nil) {
return
}
//Check if its moving left, if it is remove the action
if((sprite.action(forKey: "leftMove")) != nil) {
sprite.removeAllActions()
}
//Create and run the right movement action
let action = SKAction.move(by: 100, duration: 1)
sprite.run(SKAction.repeatForever(action), withKey: "rightMove")
}
我正在创建一个 spritekit 游戏,我对 swift 还很陌生。我想要两个按钮让玩家向右或向左移动。当按下一个按钮时,比如左按钮,精灵必须开始向左移动而不停止。当它撞到左边的墙时,它会改变方向并向右移动到另一面墙,依此类推......我设法让精灵通过使用更新功能来做到这一点。每次调用它时,它都会检查玩家是否按下按钮并相应地移动精灵,但是,它会导致 FPS 延迟(FPS 会下降到 50)。
我尝试使用 MoveBy 和 MoveTo 等 SKActions,但无法重新创建我希望精灵执行的操作。
所以我的问题是:如何使用这两个按钮使精灵按照我想要的方式移动而不导致 FPS 延迟。任何帮助,将不胜感激。谢谢
以下是我在更新函数中调用的函数,这些函数有效但导致延迟。
func moveRight() {
sprite.xScale = 1
sprite.position.x += 4
}
func moveLeft() {
sprite.xScale = -1
sprite.position.x -= 4
}
试试这个代码:
它会在按下按钮时一直运行移动动作,而在释放按钮时会删除动作
这将使玩家充满希望地移动而不会降低帧率。要在精灵撞到墙壁时改变精灵的方向,您必须检查碰撞。当它撞到墙上时,您可以检查正在应用的是 leftMove 还是 rightMove 动作,然后删除该动作并开始相反的动作。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if(leftButton.contains(location) { // check if left button was pressed
moveLeft()
} else if(rightButton.contains(location) { //check if right button was pressed
moveRight()
}
}
}
func moveLeft() {
//Check if it's already moving left, if it is return out of function
if((sprite.action(forKey: "leftMove")) != nil) {
return
}
//Check if its moving right, if it is remove the action
if((sprite.action(forKey: "rightMove")) != nil) {
sprite.removeAllActions()
}
//Create and run the left movement action
let action = SKAction.move(by: -100, duration: 1)
sprite.run(SKAction.repeatForever(action), withKey: "leftMove")
}
func moveRight() {
//Check if it's already moving right, if it is return out of function
if((sprite.action(forKey: "rightMove")) != nil) {
return
}
//Check if its moving left, if it is remove the action
if((sprite.action(forKey: "leftMove")) != nil) {
sprite.removeAllActions()
}
//Create and run the right movement action
let action = SKAction.move(by: 100, duration: 1)
sprite.run(SKAction.repeatForever(action), withKey: "rightMove")
}