SpriteKit六角形地图:玩家节点移动时的平铺检测

时间:2017-05-30 17:21:38

标签: ios swift sprite-kit game-physics

我问这个问题,因为我没有找到解决这类问题的方法。事实上,Hex地图支持并不是很受欢迎。

我正在使用SpriteKit框架制作游戏。 我使用带有六角形地图的SktileMapNode,以及一组4组图块。

播放器节点在每个磁贴上移动,当他移动特定区块时,我想要的是某些事件可以被触发(打印,功能,Sktransition)但是目前我只是在检测那些磁贴时。 enter image description here

我设置了用户数据(如bool)并将其应用到代码中,但即使在磁贴上有触摸事件,也没有任何反应。

这是GameScene中的代码:

  func handleTapFrom(recognizer: UITapGestureRecognizer) {
    if recognizer.state != .ended {
        return
    }

    let recognizorLocation = recognizer.location(in: recognizer.view!)
    let location = self.convertPoint(fromView: recognizorLocation)



    let column = galaxieMapnode.tileColumnIndex(fromPosition: location)
    let row = galaxieMapnode.tileRowIndex(fromPosition: location)
    if let tile = galaxieMapnode.tileDefinition(atColumn: column, row: row) {

    let data = tile.userData?.value(forKey: "wormholeTile") as? Bool

    if (data == true) {

        print("wormhole touched")


    }


    }

}

触地得分播放器的另一个扩展文件中的代码:

func touchDown(atPoint pos : CGPoint) {




    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTapFrom(recognizer:)))
    tapGestureRecognizer.numberOfTapsRequired = 2
    view?.addGestureRecognizer(tapGestureRecognizer)

    let position = thePlayer.position

    let column = galaxieMapnode.tileColumnIndex(fromPosition: position)
    let row = galaxieMapnode.tileRowIndex(fromPosition: position)


    if let tile:SKTileDefinition = galaxieMapnode.tileDefinition(atColumn: column, row: row) {


        if  let tileBoolData = tile.userData?["wormholeTile"] as? Bool {

            if (tileBoolData == true) {

                print("Wormhole touched")


            }



        }




    } else {


        print("different tile")


    }



    }

也许我使用了bad gestureRecogniser,或者我把这些代码放在了不好的范围内。

欢迎任何帮助。

谢谢

image for description of the issue

编辑:

@KnightOfDragon

好的我擦除了所有的手势识别,以及移动功能中的代码:

extension GameScene  {



func move(theXAmount:CGFloat , theYAmount:CGFloat, theAnimation:String )  {


    let wait:SKAction = SKAction.wait(forDuration: 0.05)

    let walkAnimation:SKAction = SKAction(named: theAnimation, duration: moveSpeed )!

    let moveAction:SKAction = SKAction.moveBy(x: theXAmount, y: theYAmount, duration: moveSpeed )

    let group:SKAction = SKAction.group( [ walkAnimation, moveAction ] )

    let finish:SKAction = SKAction.run {



        let position = self.thePlayer.position

        let column = self.galaxieMapnode.tileColumnIndex(fromPosition: position)
        let row = self.galaxieMapnode.tileRowIndex(fromPosition: position)


        if let tile:SKTileDefinition = self.galaxieMapnode.tileDefinition(atColumn: column, row: row) {


            if  let tileBoolData = tile.userData?["wormholeTile"] as? Bool {

                if (tileBoolData == true) {

                    print("Wormhole touched")


                }



            }




        } else {


            print("different tile")


        }


    }

但是当移动结束时输出只是“不同的瓦片”。 无论如何,谢谢你的说明。

可能有任何想法吗?

0 个答案:

没有答案