2016-08-28 66 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 
       } 

      } 
     }