【问题标题】:How can I find the equivalent of -locationInNode for an NSTouch?如何为 NSTouch 找到 -locationInNode 的等价物?
【发布时间】:2014-09-05 13:08:13
【问题描述】:

我的 SpriteKit Mac 应用程序通过 -touchesBeganWithEvent:(NSEvent)event 响应 NSTouch 事件。但是,调用 -locationInNode 只会给我该 NSTouch 的鼠标坐标位置。但我正在尝试将触控板上的触摸坐标映射到屏幕,如下所示:

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 的调用。

【问题讨论】:

  • 你是如何让你的场景响应 -touchesBeganWithEvent: 的?

标签: macos cocoa-touch sprite-kit nsevent nstouch


【解决方案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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    相关资源
    最近更新 更多