Node movement lag in Spritekit, Xcode

 ̄綄美尐妖づ 提交于 2019-12-11 04:24:11

问题


In my game I have a node that the player controls with the PanGesture. When the PanGesture function is called, it adds its velocity to that of the Node in question, making it move.

However, I have encountered a problem where the Node (under specific yet inconsistent circumstances) moves slower and not as smoothly as intended.

When I start the application it (sometimes) has the correct movement speed. As soon as I "die", and restart (remove all nodes and recognizers, restart the scene, remake all nodes and recognizers) the node does not move at the correct speed.

This is where things get weird.

After restarting the game and seeing that the node has the incorrect speed, I can access the "swipe up menu" and then promptly dismiss it, at which point the node will revert back to its intended speed. This however, does not always work. On top of that, I can leave the scene to have the same thing happen. This happens even less frequently.

This is the code I use to make a GestureRecognizer:

let Panned:UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GameScene.Panned))
view.addGestureRecognizer(Panned)

These lines are only run once per scene. (Making multiple GestureRecognizers causes the entire scene to experience dramatic frame drops each time the screen is touched.)

This is the code which adds the velocity of the movement to the Node:

func Panned(sender:UIPanGestureRecognizer){

    velocity = sender.velocity(in: view)

    if movement == true{

        Node.position.x = Node.position.x+(velocity.x/100)/1
        Node.position.y = Node.position.y-(velocity.y/100)/1

    }
}

This function is called each time the PanGestureRecognizer detects a "pan" movement.

This is my restart function, which prepares the scene for the next instance of the game:

func restartScene(){

    removeAllChildren()
    removeAllActions()

    createScene()

}

removeAllChildren will kill my stuttering node, and createScene() will revive it again. (It should be noted that createScene() is the same function used at the initial start of the game, and can therefor not be related to the problem.)

The restart is however where my problem starts. This function is missing certain (in my opinion) irrelevant lines that manipulate certain values and handle labels.

The main gist of my problem is as follows:

  1. Do I fully understand GestureRecognizers?

  2. What effect does the "swipe up menu" have on my current scene? Does it clear/restart certain things, or does it simply pause my scene?

Edit:

The most confusing thing about the problem is how it arises after a "restart", when the restart process does not touch the PanGesture in anyway. I'm considering remaking the game to create a version where the problem isn't present, but this leaves the problem unsolved.

来源:https://stackoverflow.com/questions/41325311/node-movement-lag-in-spritekit-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!