Ios7 SKNode转换点到节点&;从节点混淆?

Ios7 SKNode转换点到节点&;从节点混淆?,ios7,sprite-kit,sknode,Ios7,Sprite Kit,Sknode,我对SKNode方法convertPoint:fromNode:和convertPoint:ToNode:的工作原理有点困惑,我已经看过了文档,但它们的作用并不明显。例如,这(见下图)是我使用convertPoint:fromNode:做的一个小测试,黑色区域是SKScene背景,蓝色区域是SKSpriteNode的父级,红色区域是另一个SKSpriteNode的父级。两个精灵的定位点都由绿色小点显示。我想做的是获取红色精灵的场景位置,这是我使用以下代码完成的: CGPoint position

我对
SKNode
方法
convertPoint:fromNode:
convertPoint:ToNode:
的工作原理有点困惑,我已经看过了文档,但它们的作用并不明显。例如,这(见下图)是我使用
convertPoint:fromNode:
做的一个小测试,黑色区域是
SKScene
背景,蓝色区域是
SKSpriteNode
的父级,红色区域是另一个
SKSpriteNode
的父级。两个精灵的定位点都由绿色小点显示。我想做的是获取红色精灵的场景位置,这是我使用以下代码完成的:

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

positionInScene = [105, 205]
这就是我所期望的,因为那将是场景空间中红场的起源。我感到困惑的是争论。据我所知:

[SKNode_A convertPoint: CGPoint_B toNode: SKScene_C]
  • SKNode_A=要转换为的节点坐标空间
  • CGPoint_B=要转换的点(不确定其上面的[自身位置]为何)
  • SKNode_C=要从中转换的节点坐标空间
我最初的尝试是
[self convertPoint:[redSprite position]fromNode:redSprite]
,因为我想将红色精灵的原点转换为场景。让你的头脑清醒起来似乎有点笨拙,如果有人能对它和它的朋友
convertPoint:toNode:
有一点了解和逻辑,那将非常感激

-(CGPoint)convertPoint:(CGPoint)point fromNode:(SKNode*)node 这实质上是说:将另一个节点坐标系中表示的点转换为调用方(自身)坐标系。关键是,该点必须在节点的坐标系中表示才能工作。如果使用精灵的位置作为点,则需要将精灵的父节点作为最后一个参数传递

示例:要获取红方块在场景坐标系中的位置:

CGPoint positionInScene = [self convertPoint:[redSprite position]
                                    fromNode:[self blueSprite]];
[5,5]在blueSprite的坐标系中-->[105205]在场景的坐标系中

您的示例:

CGPoint positionInScene = [self convertPoint:[self position] 
                                    fromNode:[self redSprite]];
[0,0]在红精灵的坐标系中-->[105205]在场景的坐标系中

您的代码得到了相同的结果,只是因为[self position]为[0,0],并且您使用了redSprite的坐标系。您最初的尝试很接近,您只需要提供表示redSprite位置的坐标系,即父sprite的坐标系;在这种情况下,这是蓝宝石

-(CGPoint)convertPoint:(CGPoint)point toNode:(SKNode*)节点 这种方法与第一种方法完全相反。它将调用方坐标系中的一个点转换为另一个节点的坐标系。这种方法的关键是,给定的点必须用调用方的坐标系表示

示例:假设您的场景在[125225]处收到一个触摸,您希望在该位置添加一个新的精灵,该精灵恰好位于redSprite的边界框内。因此,要添加一个新的精灵作为redSprite的子元素,需要获取触摸在redSprite坐标系中的位置。要实现这一点:

CGPoint positionInRedSprite = [self convertPoint:touchLocation 
                                          toNode:[self redSprite]];
[125,225]在场景的坐标系中-->[20,20]在redSprite的坐标系中为简单起见: 当比较节点B和节点A的位置时(试图找出它们之间的距离)

如果无法帮助可视化该位置,请在该位置添加精灵进行故障排除:

let childToSeeLocation = SKShapeNode(CircleWithRadius:50)
childToSeeLocation.fillColor = UIColor.redColor
ParentofA.addChild(childToSeeLocation)

在阅读了kevin的精彩答案后,我能够对SKNode进行一些扩展,这些扩展既是一个很好的示例,也是一套方便的工具

extension SKNode {

var frameInScene:CGRect {
    get {
        if let scene = self.scene {
            if let parent = self.parent {
                let rectOriginInScene = scene.convertPoint(self.frame.origin, fromNode: parent)
                return CGRect(origin: rectOriginInScene, size: self.frame.size)
            }
        }
        println("Just the normal frame, not the scene frame!!!")
        return frame
    }
}

var positionInScene:CGPoint {
    get {
        if let scene = self.scene {
            if let parent = self.parent {
                return scene.convertPoint(self.position, fromNode: parent)
            }
        }
        println("Just the normal position, not the scene position!!!")
        return position
    }
}
}
我相信你可能会删除每一个
自我。
,但我觉得这会让代码更加混乱。如果节点不属于任何场景或父节点,我不能完全确定该怎么办,因此我只返回了常规帧和位置,并使用
println()
给我一个警告


如果有人对此代码有任何更改/添加,我很乐意听到,尤其是如果它有助于进一步回答问题。

我知道这是一个延迟响应,但通常我希望使用节点的父节点作为节点位置的坐标系。因此,我发现这个扩展非常有用:

扩展SCNNode{
func convertPosition(节点:SCNNode)->scconvert3{
返回self.convertPosition(node.position,fromNode:node.parentNode)
}
}
例如,不要将其称为:

let position=scene.rootNode.convertPosition(node.position,fromNode:node.parentNode)
你只需称之为:

let position=scene.rootNode.convertPosition(节点)

您将此代码放在哪里:
[self convertPoint:[self position]fromNode:[self redSprite]]
?它是否属于场景?此代码位于SKScene(或其子类)的方法中。您错误地列出了参数的含义,或者您可能对定义的参数感到困惑。描述如何使用“convertPoint:toNode”非常感谢Kevin,这肯定会澄清问题,感谢您提供了一个非常清晰和信息丰富的答案。关键是要知道谁是来电者,因为它的协调系统是参考。我读了10遍这篇文章,终于发现了这一点:)
extension SKNode {

var frameInScene:CGRect {
    get {
        if let scene = self.scene {
            if let parent = self.parent {
                let rectOriginInScene = scene.convertPoint(self.frame.origin, fromNode: parent)
                return CGRect(origin: rectOriginInScene, size: self.frame.size)
            }
        }
        println("Just the normal frame, not the scene frame!!!")
        return frame
    }
}

var positionInScene:CGPoint {
    get {
        if let scene = self.scene {
            if let parent = self.parent {
                return scene.convertPoint(self.position, fromNode: parent)
            }
        }
        println("Just the normal position, not the scene position!!!")
        return position
    }
}
}