Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 当触摸活动仍在进行时,如何停止精灵移动_Swift_Sprite Kit - Fatal编程技术网

Swift 当触摸活动仍在进行时,如何停止精灵移动

Swift 当触摸活动仍在进行时,如何停止精灵移动,swift,sprite-kit,Swift,Sprite Kit,我有一个蓝色的方形精灵和一个字母,还有一张形状各异的瓷砖地图。 蓝色方块应该能够在地图内自由移动 我的目标是限制瓷砖贴图中的蓝色方形精灵,即使触摸活动仍在进行 问题是,当蓝色方形精灵按某个方向移动时, 如果触摸仍在进行,精灵将在红瓦外移动 我尝试过的解决方案 逻辑: 创建一个布尔标志IsNeightUrtileNil,以检测更新中的红色分幅上的相邻分幅是否为nil 这一步我没有问题。 如果IsNeightUrtileNil为真,则即使触摸仍在移动/发生,玩家精灵也应停止移动 这一步我有困难。 我

我有一个蓝色的方形精灵和一个字母,还有一张形状各异的瓷砖地图。 蓝色方块应该能够在地图内自由移动

我的目标是限制瓷砖贴图中的蓝色方形精灵,即使触摸活动仍在进行

问题是,当蓝色方形精灵按某个方向移动时, 如果触摸仍在进行,精灵将在红瓦外移动

我尝试过的解决方案

逻辑:

创建一个布尔标志IsNeightUrtileNil,以检测更新中的红色分幅上的相邻分幅是否为nil

这一步我没有问题。 如果IsNeightUrtileNil为真,则即使触摸仍在移动/发生,玩家精灵也应停止移动

这一步我有困难。 我面临的挑战是,bool变量isNeighbourTileNil在每一帧都会更新。然而,touch event touchesbreated->touchesMoved->touchesend是一个线性过程,这意味着我无法将一个不断变化的变量(如isNeighbourTileNil)传递给touchesMoved函数并阻止玩家从那里移动。 以下是我尝试过的代码:

 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            if(i==0){
                firstTouch = touch.location(in: self)
            }
            if(i==1){
                secondTouch = touch.location(in: self)
            }
            i = i+1
        }
        direction = directionCalculator(firstTouch, secondTouch)
        print(direction)
        if isNeighbourTileNil == false {
            moveInDirection(direction, 10.0, 10.0, -10.0, -10.0)
        }
    }

这有点复杂,细节取决于如何设置,所以我不打算编写一堆代码,但我建议按如下方式组织:

有一个名为canMove的变量或类似的变量,当玩家处于可以移动的状态时,该变量应为true。 添加一个UISwipegestureRecognitor以识别每次刷卡。Google UIgestureRecognitor spritekit提供了如何实现这一点的示例。这将在发生刷卡时调用您的代码,并为您提供玩家想要移动的方向。当canMove为false时忽略滑动。 当您收到刷卡通知且canMove为真时,请查看玩家是否可以根据相邻的牌向所需方向移动。如果没有,则忽略刷卡。 最后,当canMove为真且滑动指示有效方向时。。。将canMove设置为false,并触发玩家移动到新方块的动画,运行SKAction。moveto:duration:或其他任何内容。使用完成处理程序运行操作,该处理程序将操作完成时canMove设置为true。
需要更多细节-如何阻止蓝色广场向外移动?是否通过接触/碰撞?是什么导致它移动?您是手动设置其位置还是通过物理移动?为什么你不能在触摸发生时设置一个标志,并在设置标志时停止移动?@Steveivs谢谢你的建议,我已经更新了:根据你编写触摸移动的方式,看起来你可能试图识别一个滑动手势,然后让玩家移动一个方块?在刷卡的方向上。是吗?@bg2b是的,没错
override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendere
        let playerPosition = player.position
        let column = mainTileMap.tileColumnIndex(fromPosition: playerPosition)
        let row = mainTileMap.tileRowIndex(fromPosition: playerPosition)
        let currentTile = mainTileMap.tileDefinition(atColumn: column, row: row)
        currentTileTuple.column = column
        currentTileTuple.row = row

        if !self.isMonsterCollisionDetected {
            self.detectMonsterCollisions()
        }
        detectMapCollision()

        if isNeighbourTileNil == true {
            print("player should stop moving")
            player.removeAllActions()
        }
    }