スプライトキット
ドキュメントとスタックオーバーフローを検索しましたが、役に立つ資料を見つけることができませんでした。
ユーザーが画面上で SKpriteNodes をドラッグすると、スプライトの zPosition を更新して、必要に応じてスプライトを他のスプライトの前に正しく表示します。どのように動作させる必要があるかは、スプライトの yPosition に基づいています。yPosition が高いスプライトは、yPosition が低いスプライトより前にある必要があります。そこで、yPosition を zPosition に設定しただけで、正常に動作します...ほとんど。
SpriteKit の座標系により、スプライトが画面の半分ほど上に来ると、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]];
}
}
}