精灵套件
我搜索了文档和 stackoverflow,但找不到有用的材料。
当用户在屏幕上拖动我的 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]];
}
}
}