I have a question related to accessing tiles by screen coordinates. I have created a simple map 8 x 8 tiles, with two layers. When you click on a tile in the toplayer, it removes the tile and reveals what’s underneath in the bottom layer. If I load the map without changing the camera position or viewpoint, the touch function works great. I really want the map in the center of the screen though. As soon as I alter the camera position and viewpoint, the touch listener no longer works. Tapping on a tile removes a different tile. I know what’s going on, and I know I can fix it mathematically, but wondered if there was a simpler “dusk” way to handle this. Essentially, when the map is loaded at (0,0), using map.layer.tilesByPixels(x,y) works great because the x/y coordinates of the map match up with the x/y coordinates of the screen. However, as soon as I move the map, they no longer match up, so when I touch the screen at 524,524, it removes the bottom right tile of the map, which is the map pixel coordinate 524,524. Here’s my code:
local dusk = require("Dusk.Dusk") display.setDefault("minTextureFilter", "nearest") display.setDefault("magTextureFilter", "nearest") local centerX = display.contentCenterX local centerY = display.contentCenterY local map = dusk.buildMap("map001.json") local toplayer = map.layer["toplayer"] map.setCameraBounds({xMin = centerX, yMin = centerY, xMax = map.data.width - centerX, yMax = map.data.height - centerY}) map.positionCamera(map.data.width/2, map.data.height/2) map.setViewpoint(map.data.width/2 , map.data.height/2) local function touchTile(event) local t = event print(t.x, t.y) local tile = toplayer.tileByPixels(t.x, t.y) display.remove(tile) tile = nil return true end local function updateMap() map.updateView() end timer.performWithDelay(1,updateMap,-1) toplayer:addEventListener( "touch", touchTile )