swift3sprite-kitios10hexagonal-tilessktilemapnode

Spritekit Hexagonal map: tile detection in end SKAction.


I ask this question because i didn't found any solution for this kind of issues. In fact Hex map support are not very popular.

I'am making a game with the SpriteKit Framework. I use SktileMapNode with an Hexagonal map, with 1 set of 4 groups tiles.

The playernode moves on each tiles, what i wan't it's when he move on a specifics tiles some event can be triggered ( print , function , Sktransition) but for the moment i'm stuck on just detecting those tiles.

I setup the user data ( as bool ) and apply them in the code , but nothing happened, even with a touch event on a tile.

    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")


        }


    }

Only the "different tile " output is fired.

Any help are welcome.

Link for image example : the thing I want


Solution

  • I think you want to run your finish block after the other actions have completed? You can do this as a sequence.

    var sequence = [SKAction]()
    let action = SKAction.move(to: location, duration: 1)
    let completionHandler = SKAction.run({
        // Check tile info here
    })
    
    sequence += [action, completionHandler]
    
    self.player.run(SKAction.sequence(sequence))
    

    Typically in a hexagonal map you would move the player to an adjacent tile and check the tiles attributes, then continue with the next move action. This requires the graph and pathfinding functionality from GameplayKit.