它以等距地图开始,我设法绘制.
但是我在地图的不同瓷砖上获得精确的触摸位置时遇到了麻烦.
它有效,但有点不合适,似乎不一致.
这是我的功能:
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,1,0],[0,[2,2,2],0]] drawMap(mapConfig,mapOrigin: mapOrigin)}
用:
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) }}
然后我想隐藏我触摸的瓷砖(用于测试):
overrIDe func touchesBegan(touches: NSSet,withEvent event: UIEvent){ for touch: AnyObject in touches { let locationNode = touch.locationInNode(self) nodeAtPoint(locationNode).hIDden = true }}
但它并不总是隐藏正确的瓷砖.
那我该怎么解决呢?我的代码是否根本错误(可能)?或者我需要以某种方式将位置转换为iso坐标?或者玩瓷砖位面罩?
无论如何,谢谢你的帮助!
解决方法 我在等轴测图中遇到了类似的问题.问题是您单击的节点大于显示的节点(它具有透明部分).有关该问题的更好解释,请参阅my question here.
以下是我如何解决它(抱歉代码在Objective-C中):
1.在tile的边缘之后创建一个CGPathref(tileSize是纹理的大小).此代码适用于常规等距切片,而不是六边形,但想法是相同的.
// ObjC-(CGPathref)createTextureFrame:(CGSize)tileSize{ CGMutablePathref path = CGPathCreateMutable(); CGPathMovetoPoint(path,NulL,-(self.tileSize.height / 2)); CGPathAddlinetoPoint(path,(self.tileSize.wIDth / 2),0); CGPathAddlinetoPoint(path,(self.tileSize.height / 2)); CGPathAddlinetoPoint(path,-(self.tileSize.wIDth / 2),0); CGPathCloseSubpath(path); return path;}// Swiftfunc createTextureFrame(tileSize:CGSize) -> CGPathref { CGMutablePathref path = CGPathCreateMutable() CGPathMovetoPoint(path,nil,-(self.tileSize.height / 2)) CGPathAddlinetoPoint(path,0) CGPathAddlinetoPoint(path,(self.tileSize.height / 2)) CGPathAddlinetoPoint(path,0) CGPathCloseSubpath(path) return path}
2.创建一个函数,检查给定的点是否在CGPathref(textureFrame)中.
// ObjC-(BOol)isPointOnNode:(CGPoint)point{ return CGPathContainsPoint(textureFrame,point,YES);}// Swiftfunc isPointOnNode(point:CGPoint) -> Bool { return CGPathContainsPoint(textureFrame,YES)}
3.对于每个触摸的节点,检查我们所在的textureFrame.
// ObjCUItouch *touch = [touches anyObject];NSArray *nodes = [self nodesAtPoint:[touch locationInNode:self]];for (SKNode *node in nodes){ CGPoint locationInNode = [touch locationInNode:node]; if ([node isPointOnNode:locationInNode]) { node.hIDden = YES; }}// Swiftvar touch = touches.anyObject() as UItouchvar nodes = self.nodesAtPoint(touch.locationInNode(self))for node in nodes as [SKNode] { var locationInNode = touch.locationInNode(node) if node.isPointOnNode() { node.hIDden = true }}
我不知道它是否是最好的解决方案,但效果很好.
我希望它有帮助:)
编辑:添加了Swift版本的代码
总结以上是内存溢出为你收集整理的ios – Swift Spritekit等距地图触摸位置全部内容,希望文章能够帮你解决ios – Swift Spritekit等距地图触摸位置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)