so I have been converting my keyboard input system to use Touch events so I can play on phones and tablets.
On windows with a mouse it works fine.
On my iPad, I do get touch events and I can “scroll my screen” which uses pure delta values. But my “Which tile did I just click on” function fails to detect any tiles. Which makes me think that the co-ordinates or something are different.
here is my touch -> tile code
convertTouchEventToTilePosition(e:cc.Event.EventTouch) : cc.Vec2 {
//console.log("touch " + e.getLocation().toString() + “taken :” + this.disableMouse );
var cam = cc.Camera.main.node;
let visibleSize = cc.view.getVisibleSize();
var tilePos = new cc.Vec2(0,0);
let locInView = e.getLocationInView();
tilePos.x = Math.floor(locInView.x / this.mapInfo.tileSize.width) + cam["tileX"];
tilePos.y = this.mapInfo.max.height - Math.floor((visibleSize.height - locInView.y) / this.mapInfo.tileSize.height) - cam["tileY"];
return tilePos;
}
Where Map Info is initilised as so
var tiledMap = this.getComponent(cc.TiledMap);
let objGroups = tiledMap.getObjectGroups();
let layer = tiledMap.getLayer("Terrain");
this.layer = layer;
let mapMax = new cc.Size(layer.getLayerSize().width-1,layer.getLayerSize().height-1);
let mapTileSize = layer.getMapTileSize();
let winSize = cc.director.getWinSizeInPixels();
let visbibleTiles = new cc.Size(Math.floor(winSize.width / mapTileSize.width), Math.floor(winSize.height / mapTileSize.height));
this.mapInfo = new mapInfoClass(mapMax,visbibleTiles,layer.getMapTileSize());
and the constructor is defined as
constructor(max : cc.Size, visible : cc.Size, size : cc.Size) {
this.max = max;
this.visibleTiles = visible;
this.tileSize = size;
this.maxRect = cc.rect(0,0,max.width,max.height);
}
I guess this is a “it just so happens to work” on windows and is not the right way to do it. What would be the right way to do it?
Maybe the map is not inverted on the iPad, or the map and touches have the same “inverted” y?