Reputation: 884
SpriteKit
I have searched the documentation and stackoverflow, but have not been able to find helpful material.
As users drag my SKpriteNodes around the screen, I am updating the sprite's zPosition to correctly show the sprite in front of other sprites where necessary. How I need it to work is based on the sprite's yPosition. Sprites with a higher yPosition need to be in front of those with a lower yPosition. So I just set the yPosition to the zPosition, which works fine... almost.
Because of SpriteKit's coordinate system, as sprite's get about halfway up the screen, the yPosition hits zero and then into the negative numbers - which has a backwards effect for my zPosition. I have tried Converting point from scene coordinates to view coordinates using [self convertPointToView] method but did not help. I am wondering if this has something to do with the parent (background spritenode) having the anchor point to CGPointZero - which still confuses me slightly.
Any recommendations on how to correctly set the zPosition based on it's yPosition -- or is it possible to change the scene's coordinate system. (It appears that {0,0} is not the lower left corner of my background node, but rather the center of the screen)
-(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]];
}
}
}
Upvotes: 0
Views: 479
Reputation: 2534
I suspect the problem you are having is attempting to convert a point from a view. Sprite Kit uses a coordinate system were 0 is bottom on the screen. Whereas traditional UIKit uses a coordinate system where 0 is top of the screen.
If you just use nodeImTouching.position.y then you should get a position that is relative to the scene. Is the thing that you are touching, inside of a node that isn't the scene? If that's the case then you would need to convert the position of that node into the coordinate system of the scene, since the touched node is reporting it's position inside of its parent.
CGPoint pointInScene = [touchedNode.parent convertPoint:touchedNode.position toNode:theMainScene];
Upvotes: 0
Reputation: 7403
As a quick fix, try adding some constant to the y position of each point, as long as the constant is larger than half of the scene's height.
Upvotes: 0