I've started doing a small swift / spritekit project to teach myself game dev. It starts with an isometric map, which I managed to draw. But I'm having trouble getting a precise touch location on the different tiles of the map. It works, but is slightly out of place, and does not seem consistent.
Here are my functions :
class PlayScene: SKScene {
let map = SKNode()
override func didMoveToView(view: SKView) {
let origin = view.frame.origin
let mapOrigin = CGPointMake(origin.x + self.frame.width / 4, origin.y - self.frame.height / 4)
let mapConfig: Int[][] =
[[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[2, 2, 2, 2, 1, 2, 2, 2],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0]]
drawMap(mapConfig, mapOrigin: mapOrigin)
}
With :
func drawMap(mapConfig:Int[][], mapOrigin:CGPoint)
{
let tileHeight:CGFloat = 25.5
let numColumns:Int = 8
let numRows:Int = 8
var position = mapOrigin
var column: Int = 0
var row: Int = 0
for column = 0; column < numColumns; column++
{
for row = 0; row < numRows; row++
{
position.x = mapOrigin.x + CGFloat(column) * tileHeight
position.y = mapOrigin.y + CGFloat(row) * tileHeight
let isoPosition = twoDToIso(position)
placeTile(isoPosition, mapConfig: mapConfig[row][column])
}
}
self.addChild(map)
}
func placeTile(position:CGPoint, mapConfig:Int)
{
switch mapConfig
{
case 0:
let sprite = SKSpriteNode(imageNamed:"grassTile")
sprite.position = position
sprite.setScale(0.1)
sprite.name = "\(position)"
self.map.addChild(sprite)
case 1:
let sprite = SKSpriteNode(imageNamed:"roadTile")
sprite.position = position
sprite.setScale(0.1)
sprite.name = "\(position)"
self.map.addChild(sprite)
default:
let sprite = SKSpriteNode(imageNamed:"roadTileLTR")
sprite.position = position
sprite.setScale(0.1)
sprite.name = "\(position)"
self.map.addChild(sprite)
}
}
And then I want to hide the tile I touch (for testing) :
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
for touch: AnyObject in touches
{
let locationNode = touch.locationInNode(self)
nodeAtPoint(locationNode).hidden = true
}
}
But it does not always hide the correct tile. So how should I tackle this ? Is my code fundamentally wrong (possible) ? Or do I need to convert the location to iso coordinates in some way ? Or play with the tiles bitmask ?
Thanks for your help in any case !