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]];
}
}
}
,如果你所有的感動節點是其父節點的約束體積,那麼你可以只使用'touchedNode.parent.position.y',在場景中的絕對位置。 –
我將touchNode的答案中的代碼更改爲touchedNode.parent。我的大腦總是使用這些convertPoint方法來打破。 :-) –