1

I thought I would create a tile map with the new Tile Map Nodes available in iOS 10.

I have a tile which if you stand on it, it will crumble. So I though I would place a userData field called timer in the tileGroup definition.

    func crumbleTile(pos :CGPoint, dt : TimeInterval){
    let column = roomMap.tileColumnIndex(fromPosition: pos)
    let row = roomMap.tileRowIndex(fromPosition: pos)

    let objectTile = roomMap.tileDefinition(atColumn: column  , row: row)

    if var timer = objectTile?.userData?.value(forKey: "timer") as? Double {
        print("tile pos \(column),\(row)")
        print("timer-from tile:\(timer)")
                    timer = timer - dt
        objectTile?.userData?.setValue(timer, forKey: "timer")
        print("timer-update tile:\(timer)")
        if timer < 0.0 {
            //move to next frame
            if var crumbleFrame = objectTile?.userData?.value(forKey: "crumble") as? Int {

                crumbleFrame = crumbleFrame - 1
                print("crumble:\(crumbleFrame)")
                objectTile?.userData?.setValue(crumbleFrame, forKey: "crumble")

                if crumbleFrame < 1 {
                    //delete tile
                    print("delete tile")
                    removeTile(pos: pos)
                } else {
                    //move to next frame
                    print("next frame")

                    if crumbleFrame == 2 {
                        print("change frame 2")


                    }
                    if crumbleFrame == 1 {
                        print("change frame 1")


                    }
                    if crumbleFrame == 0 {
                        print("change frame 0")


                    }
                }


            }

        }


    }

}
func removeTile(pos : CGPoint) {
    let column = roomMap.tileColumnIndex(fromPosition: pos)
    let row = roomMap.tileRowIndex(fromPosition: pos)
             roomMap.setTileGroup(nil, forColumn: column, row: row)
}

Instead of each tile having it's own timer setting it appear that I am either adjusting a single timer or simultaneously adjust every timer for that tile.

It can be seen in the log file.

tile pos 51,11
timer-from tile:-0.104295339999993
timer-update tile:-0.125737270999991
crumble:-1
delete tile
2:crumble floor right
tile pos 52,11
timer-from tile:-0.125737270999991
timer-update tile:-0.164520885999991
crumble:-2
delete tile

tile at position 52,11 has the same timer value as 52,11 had ?

So the question is, is there a way to individually access each tiles userData ?

Thanks in Advance.

4

1 回答 1

1

您在文件中添加的 userDatasks应用于地图上使用的该类型的每个图块,而不是每个图块实例,例如,如果您有一个带有 userData 的山图块,mountain:true那么您放置在地图上的每个山图块都将返回 true。如果您将 userData 更新为 false,则所有山图块均为 false。

由于每个图块由列和行索引唯一标识,因此您可以创建一个像元组数组一样的数据结构来存储计时器信息;

var timerInfo:[(column: Int, row: Int, timer: Double)] = []
于 2016-09-23T22:30:40.333 回答