2013-11-04 47 views
0

SpriteKitSKSpriteNode z位置座標的問題

我已經搜索的文件和計算器

,但一直沒能找到有用的材料。

當用戶在屏幕上拖動我的SKpriteNodes時,我正在更新精靈的zPosition,以在必要時正確顯示其他精靈前面的精靈。我需要它的工作是基於精靈的yPosition。具有較高y位置的精靈需要位於具有較低y位置的精靈之前。所以我只是將yPosition設置爲zPosition,幾乎可以正常工作。

由於SpriteKit的座標系,精靈在屏幕的一半左右,yPosition命中爲零,然後輸入負數 - 這對我的zPosition有一個倒退的影響。我已經嘗試使用[self convertPointToView]方法從場景座標轉換點以查看座標,但沒有幫助。我想知道這是否與父母(背景spritenode)具有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]]; 
      } 
     } 
} 

回答

0

作爲速戰速決,嘗試加入一些常到每個y位置點,只要常數大於場景高度的一半。

0

我懷疑你正在試圖從視圖轉換一個點。 Sprite Kit使用座標系統,屏幕下方爲0。而傳統的UIKit使用座標系統,其中0是屏幕的頂部。

如果你只是使用nodeImTouching.position.y,那麼你應該得到一個相對於場景的位置。在一個不屬於場景的節點內,你感動的是什麼?如果是這種情況,那麼您需要將該節點的位置轉換爲場景的座標系,因爲所觸摸的節點正在報告它在其父節點內的位置。

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

,如果你所有的感動節點是其父節點的約束體積,那麼你可以只使用'touchedNode.parent.position.y',在場景中的絕對位置。 –

+0

我將touchNode的答案中的代碼更改爲touchedNode.parent。我的大腦總是使用這些convertPoint方法來打破。 :-) –