2016-08-28 63 views
0

我的场景中有一些等角拼贴。有框架重叠,所以我正在使用一个等长Tile形状的路径,并检查触摸是否在内部。swift - 检测路径中的触摸

我的问题是我检测到触摸,但只沿着路径,不在路径内。我的路径或使用方式一定是错误的。

这是 “touchesEnded” 方法的代码:

for actualTouch in touches 
     { 
      let SKnode = self.nodeAtPoint(actualTouch.locationInNode(self)) 
      let spriteNode = SKnode as! SKSpriteNode 
      if spriteNode.name != nil{ 
       let tileSize = CGSizeMake(75, 38) 
       let locationInNode = actualTouch.locationInNode(SKnode) 

       let isometricPath = CGPathCreateMutable() 
       CGPathMoveToPoint(isometricPath, nil, 0, -(tileSize.height/2)) 
       CGPathAddLineToPoint(isometricPath, nil, (tileSize.width/2), 0) 
       CGPathAddLineToPoint(isometricPath, nil, 0, (tileSize.height/2)) 
       CGPathAddLineToPoint(isometricPath, nil, -(tileSize.width/2), 0) 
       CGPathCloseSubpath(isometricPath) 
       CGPathMoveToPoint(isometricPath, nil, 0, 0) 
       let isometricPathRef = isometricPath as CGPathRef 

       if CGPathContainsPoint(isometricPathRef, nil, locationInNode, true) == true 
       { 
        print("touchedTile:\(spriteNode.name!)") 
        spriteNode.alpha = 1 
       } 

      } 
     } 

u能请大家帮我看看我的错误? 谢谢。

+0

“让isometricPathRef = isometricPath为CGPathRef”也许我不能把它转换成一个不透明的路径。或者:“CGPathContainsPoint(isometricPathRef,nil,locationInNode,true)== true”这个函数不会给我这个路径里面的点..这两件事。该错误必须在那里,因为其余的工作 – frankibanki

回答

0

的解决方案是在遍历所有触摸节点,而不是“触摸”

let nodes = self.nodesAtPoint(touch.locationInNode(self)) 
     for node in nodes 
     { 

      let spriteNode = node as! SKSpriteNode 
      if spriteNode.name != nil{ 
       let tileSize = CGSizeMake(75, 38) 
       let locationInNode = touch.locationInNode(node) 

       let isometricPath = CGPathCreateMutable() 
       CGPathMoveToPoint(isometricPath, nil, 0, -(tileSize.height/2)) 
       CGPathAddLineToPoint(isometricPath, nil, (tileSize.width/2), 0) 
       CGPathAddLineToPoint(isometricPath, nil, 0, (tileSize.height/2)) 
       CGPathAddLineToPoint(isometricPath, nil, -(tileSize.width/2), 0) 
       CGPathCloseSubpath(isometricPath) 
       let isometricPathRef = isometricPath as CGPathRef 

       print("element") 
       if CGPathContainsPoint(isometricPathRef, nil, locationInNode, true) == true 
       { 
        print("touchedTile:\(spriteNode.name!)") 
        spriteNode.alpha = 1 
       } 

      } 
     }