【问题标题】:SKSpriteNode zPosition issue using the scene coordinates使用场景坐标的 SKSpriteNode zPosition 问题
【发布时间】:2013-11-04 16:38:01
【问题描述】:

精灵套件

我已经搜索了文档和 stackoverflow,但未能找到有用的材料。

当用户在屏幕上拖动我的 SKpriteNodes 时,我正在更新精灵的 zPosition 以在必要时正确地在其他精灵前面显示精灵。我需要它如何工作取决于精灵的 yPosition。具有较高 yPosition 的精灵需要位于具有较低 yPosition 的精灵前面。所以我只是将 yPosition 设置为 zPosition,效果很好……差不多。

由于 SpriteKit 的坐标系,当 sprite 到达屏幕的大约一半时,yPosition 会达到零,然后变为负数 - 这对我的 zPosition 有向后的影响。我尝试使用 [self convertPointToView] 方法将点从场景坐标转换为视图坐标,但没有帮助。我想知道这是否与将锚点指向 CGPointZero 的父节点(背景精灵节点)有关——这仍然让我有些困惑。

关于如何根据它的 yPosition 正确设置 zPosition 的任何建议——或者是否可以更改场景的坐标系。 (看来{0,0}不是我背景节点的左下角,而是屏幕的中心)

-(id)initWithSize:(CGSize)size
{
     if (self = [super initWithSize:size])
     {
          //_field is the background of my scene
          _field = [SKSpriteNode spriteNodeWithImageNamed:@"field"];
          [_field setName:@"field"];
          [_field setXScale:fieldMAX_x];
          [_field setYScale:fieldMAX_y];
          [_field setAnchorPoint:CGPointZero];
          [self addChild:_field];

           //creates an array of childNodes and positions them into a block
          for (int marcherNumber=1; marcherNumber < 101; marcherNumber++)
          {
               Marcher* marcher = [Marcher node];
               [_field addChild:[marcher createTypeOfNode:hornline 
                                                inSection:trumpet   
                                               atPosition:marcherPoint]];
               //here is where I initially set the zPosition based on it's yPosition
               marcher.zPosition = marcher.frame.origin.y;
               [arrayOfMarcherInstances addObject:marcher];                   

               //set the point for the next node to be positioned at
               marcherPoint.x += 30;
               if ((marcherNumber == 10) || 
                   (marcherNumber == 20) || 
                   (marcherNumber == 30) || 
                   (marcherNumber == 40) ||
                   (marcherNumber == 50) || 
                   (marcherNumber == 60) || 
                   (marcherNumber == 70) || 
                   (marcherNumber == 80) ||
                   (marcherNumber == 90))
               {
                    marcherPoint.x = defaultMarcherPoint.x;
                    marcherPoint.y += 30;
               } 
          }
     }
}

-(void)PanMethod
{
        for (Marcher * marcherInstance in arrayOfMarcherInstances)
        {
            //if you find one that is 'selected', then move it
            if (marcherInstance.isMarcherSelected)
            {
                //here I tried converting the Point -- (I tried convertPointFromView and convertPointToView)
                tempY = [self convertPointFromView:marcherInstance.marcherNode.position].y;
                //here I tried converting the negative yPos's into positive yPos's -  no help
                if (tempY < 0)
                    tempY = tempY * -1;
                marcherInstance.marcherNode.zPosition = tempY;
                [marcherInstance.marcherNode setPosition:CGPointMake(marcherInstance.marcherNode.position.x + translation.x, marcherInstance.marcherNode.position.y + translation.y)];
                [marcherInstance.marcherSelectionNode setPosition:[self setBoxPositionBasedOnMarcherPosition:marcherInstance.marcherNode]];
            }
        }
}

【问题讨论】:

    标签: ios coordinate-systems sprite-kit skspritenode


    【解决方案1】:

    作为一种快速解决方法,请尝试在每个点的 y 位置添加一些常数,只要该常数大于场景高度的一半即可。

    【讨论】:

      【解决方案2】:

      我怀疑您遇到的问题是试图从一个观点转换一个观点。 Sprite Kit 使用的坐标系分别为 0 位于屏幕底部。而传统的 UIKit 使用坐标系,其中 0 是屏幕的顶部。

      如果你只使用 nodeImTouching.position.y 那么你应该得到一个相对于场景的位置。您正在触摸的东西是否在不是场景的节点内部?如果是这种情况,那么您需要将该节点的位置转换为场景的坐标系,因为被触摸的节点正在报告它在其父节点内的位置。

       CGPoint pointInScene = [touchedNode.parent convertPoint:touchedNode.position toNode:theMainScene];
      

      【讨论】:

      • 如果你触摸的所有节点都是其父节点的包围体,那么你可以使用touchedNode.parent.position.y作为它在场景中的绝对位置。
      • 我将答案中的代码从touchedNode更改为touchNode.parent。使用这些 convertPoint 方法时,我的大脑总是崩溃。 :-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多