简体   繁体   中英

How can I find the equivalent of -locationInNode for an NSTouch?

My SpriteKit Mac application responds to NSTouch events via -touchesBeganWithEvent:(NSEvent)event. However, calling -locationInNode only gives me the location of the mouse coordinate for that NSTouch. But what I'm trying to do is map the touch coordinate on the trackpad to the screen like so:

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

That will translate the trackpad coordinate to the window / scene coordinate. But I need to know the location of the touch in one of the SKNode's that's in the scene. For mouse clicks I'd just use [event locationInNode:theNode], but I can't use that call since I'm manually calculating the event coordinate above.

I'm surprised that SpriteKit doesn't have any calls for dealing with NSTouches.

Since SKScene is a subclass of SKNode , you can call [SKNode convertPoint:toNode:] on your SKScene to convert the coordinates to your node.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM