SKNode convertPoint toNode& fromNode混乱? [英] SKNode convertPoint toNode & fromNode confusion?

查看:146
本文介绍了SKNode convertPoint toNode& fromNode混乱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 SKNode 方法 convertPoint:fromNode:的方式感到有些困惑convertPoint:ToNode:正在运行,我已经查看了文档,但它并不是那么明显。例如,这个(见下图)是我用 convertPoint:fromNode:做的一个小测试。黑色区域是 SKScene 背景,蓝色区域为 SKSpriteNode SKScene 的父级,红色area是蓝色精灵的另一个 SKSpriteNode 。两个精灵的锚点由小绿点表示。我想要做的是获取红色精灵的场景位置,我使用以下代码完成:

I am a little confused by how the SKNode methods convertPoint:fromNode: and convertPoint:ToNode: are working, I have looked at the docs but its not that obvious what they do. For example, this (see diagram below) is a small test I did using convertPoint:fromNode: The black area is the SKScene background, the blue area is an SKSpriteNode parented to the SKScene, the red area is another SKSpriteNode parented to the blue sprite. The anchor points for both sprites are shown by the small green dots. What I wanted to do was get the scene position for the red sprite, which I did using the following code:

CGPoint positionInScene = [self convertPoint:[self position] 
                                    fromNode:[self redSprite]];

这样的结果是

positionInScene = [105, 205]

这是我的预期,因为那将是场景空间中红场的起源。我很困惑的是争论。根据我的猜测:

Which is what I expected, as that would be the origin of the red square in scene space. What I am confused about is the arguments. From what I can guess:

[SKNode_A convertPoint: CGPoint_B toNode: SKScene_C]




  • SKNode_A =要转换为...的节点坐标空间

  • CGPoint_B =要转换的点(不确定为什么上面的[自我位置])

  • SKNode_C =要转换的节点坐标空间......

  • 我最初的尝试是 [self convertPoint:[redSprite position] fromNode:redSprite] 因为我想转换红色精灵起源于现场。如果有人可以投射一点光线,那么看起来有点笨拙。这个及其朋友的逻辑 convertPoint:toNode:非常感谢。

    My initial try at this was [self convertPoint:[redSprite position] fromNode:redSprite] because I was wanting to convert the red sprites origin to the scene. It just seems a bit clunky to get your head round, if anyone can cast a little light & logic on both this and its friend convertPoint:toNode: it would be very much appreciated.

    推荐答案

    - (CGPoint)convertPoint :( CGPoint)指向节点:(SKNode *)节点



    这实质上是说:转换指向表示的另一个节点的坐标系进入调用者的( self )坐标系。关键是必须在节点的坐标系中表达该点才能使其工作。如果您使用精灵的位置作为点,则需要将精灵的父节点作为最后一个参数传递。

    - (CGPoint)convertPoint:(CGPoint)point fromNode:(SKNode *)node

    This essentially is saying: convert a point expressed in another node's coordinate system into the caller's(self) coordinate system. The key is that the point has to be expressed in the node's coordinate system for this to work. If you are using a sprite's position as the point, you will need to pass the sprite's parent node as the last argument.

    示例:到在场景的坐标系中得到红色方块的位置:

    Example: To get the red square's position in the scene's coordinate system:

    CGPoint positionInScene = [self convertPoint:[redSprite position]
                                        fromNode:[self blueSprite]];
    

    [5,5] 在blueSprite的坐标系中 - >

    [5, 5] in blueSprite's coordinate system --> [105, 205] in the scene's coordinate system

    你的例子:

    CGPoint positionInScene = [self convertPoint:[self position] 
                                        fromNode:[self redSprite]];
    

    [0,0] 在redSprite的坐标系中 - >

    [0, 0] in redSprite's coordinate system --> [105, 205] in the scene's coordinate system

    你的代码结果与你的结果相同,只是因为[自我位置]是[0 ,0]你使用了redSprite的坐标系。你的初始尝试很接近,你只需要提供redSprite的位置表示的坐标系,这是父精灵的坐标系;在这种情况下,那是blueSprite。

    You ended up with the same result from your code, only because [self position] was [0, 0] and you used redSprite's coordinate system. Your initial try was close, you just needed to provide the coordinate system that redSprite's position was expressed in, which is the parent sprite's coordinate system; in this case, that's blueSprite.

    这种方法与第一种方法完全相反。它会将调用者坐标系中的 point 转换为另一个节点的坐标系。这种方法的关键在于给定的点必须在调用者的坐标系中表示。

    This method is very much the opposite of the first method. It will convert a point in the caller's coordinate system into another node's coordinate system. The key for this method is that the point given has to be expressed in the caller's coordinate system.

    示例:让我们说你的场景收到了触摸 [125,225] ,你想在那个位置添加一个新的精灵,它恰好位于redSprite的边界框内。因此,要添加一个新的精灵作为redSprite的子项,您需要在redSprite的坐标系中获取触摸的位置。为此:

    Example: Let's say your scene received a touch at [125, 225], and you wanted to add a new sprite at that location, which happens to be within the bounding box of redSprite. So to add a new sprite as a child of redSprite, you need to get the touch's location in redSprite's coordinate system. To get that:

    CGPoint positionInRedSprite = [self convertPoint:touchLocation 
                                              toNode:[self redSprite]];
    

    [125,225] - > [20,20] 在redSprite的坐标系中

    [125, 225] in the scene's coordinate system --> [20, 20] in redSprite's coordinate system

    这篇关于SKNode convertPoint toNode& fromNode混乱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆