2014-09-05 34 views
0

My SpriteKit Mac應用程序通過-touchesBeganWithEvent:(NSEvent)事件響應NSTouch事件。但是,調用-locationInNode只會給我該NSTouch的鼠標座標的位置。但是我想要做的是地圖上的觸控板的觸摸座標到屏幕上,像這樣:如何找到NSTouch的-locationInNode的等價物?

NSTouch *touch = [touches anyObject]; 
CGPoint sceneCoord; 
sceneCoord.x = touch.normalizedPosition.x * self.size.width; 
sceneCoord.y = touch.normalizedPosition.y * self.size.height; 

這將轉化觸控板配合到窗口/場景的座標。但我需要知道觸摸位置在場景中的一個SKNode上。對於鼠標點擊,我只是使用[event locationInNode:theNode],但我無法使用該調用,因爲我手動計算上面的事件座標。

我很驚訝,SpriteKit沒有對付NSTouches任何電話。

+0

你是如何讓你的場景響應-touchesBeganWithEvent:? – 2016-03-11 20:09:56

回答

1

由於SKSceneSKNode的子類,因此您可以在您的SKScene上調用[SKNode convertPoint:toNode:]將座標轉換爲您的節點。

NSTouch *touch = [touches anyObject]; 
CGPoint sceneCoord; 
sceneCoord.x = touch.normalizedPosition.x * self.size.width; 
sceneCoord.y = touch.normalizedPosition.y * self.size.height; 

SKNode *nodeOfInterest = ... 
CGPoint locationInNode = [scene convertPoint:sceneCoord toNode:nodeOfInterest];