I am quite confused with the spritekit coordinate system. I need clarification on two cases.
Case 1:
When a sprite node is added as child to the scene:
SKSpriteNode * blueBall = [SKSpriteNode spriteNodeWithImageNamed:@"hRedBall.png"];
blueBall.name=@"blueball";
blueBall.size=CGSizeMake(75, 75);
blueBall.position=point; //I am varying this point
[self addChild:blueBall];
I have added the node at different points each time.These are the points I added the node at: (100x100)(200x200)(300x300)(400x400)(900,600)
And in my iphone 5 device, they look like this:
If you look into 100x100, when I add the sprite at point (100,100), on device, when I log the node position, it showed me (99.9,138.17) which is huge difference compared to the point (100,100).
Even though there are changes in the other points too, I neglect them as the difference is way too small for other points.
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
[self bounceTheBlueBallAtPoint:CGPointMake(100, 100)];
}
-(void)bounceTheBlueBallAtPoint:(CGPoint)point{
SKSpriteNode * blueBall = [SKSpriteNode spriteNodeWithImageNamed:@"hRedBall.png"];
blueBall.name=@"blueball";
blueBall.size=CGSizeMake(75, 75);
blueBall.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:blueBall.frame.size.width/2];
blueBall.physicsBody.categoryBitMask=blueBallHitCategory;
blueBall.physicsBody.collisionBitMask=blueBallHitCategory|pinkBallHitCategory|wallHitCategory;
blueBall.physicsBody.contactTestBitMask=blueBallHitCategory|pinkBallHitCategory|wallHitCategory;
blueBall.physicsBody.dynamic=YES;
blueBall.physicsBody.affectedByGravity=NO;
blueBall.physicsBody.restitution=1.0;
blueBall.physicsBody.friction=0.0;
blueBall.physicsBody.linearDamping=0.0;
blueBall.physicsBody.angularDamping=0.0;
blueBall.position=point;
[self addChild:blueBall];
//applying impulse to bounce off
// CGVector impulse = CGVectorMake(30.0,60.0);
// [blueBall.physicsBody applyImpulse:impulse];
//
}
I am checking the ball position in this method:
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
[self checkSpriteMovement];
}
-(void)checkSpriteMovement{
[self enumerateChildNodesWithName:@"blueball" usingBlock:^(SKNode * _Nonnull node, BOOL * _Nonnull stop) {
SKSpriteNode *ball=(SKSpriteNode *)node;
if (ball.physicsBody.resting) {
NSLog(@"The ball position is %@",NSStringFromCGPoint(ball.position));
}
}];
}
Case 2:
Let's say I have a background sprite node, and I add the ball node to this background node instead of adding the ball node to scene.
_background = [SKSpriteNode spriteNodeWithImageNamed:@"parkImage.jpg"];
_background.size = self.frame.size; //frame size is (1024x768)
_background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:_background];
and then I add the ball node to this background:
[_background addChild:blueBall];
This is the background node added:
Now I tried to add ball to the added background:
At point:(100,100):
At point:(-280,-150)
I would also like to discuss the third case:
Case 3:
I want to another node to sprite backgorund called ring. And later I want to add ball to the background. So there will be 2 child nodes.
_ringNode=[SKSpriteNode spriteNodeWithImageNamed:@"hue_ring.png"];
_ringNode.size=CGSizeMake(80, 80);
_ringNode.position=CGPointMake(0, 0); //center point of background in this case
_ringNode.anchorPoint=CGPointMake(0.5,0.5);
_ringNode.name=kRingNodeName;
[_background addChild:_ringNode];
so the ring will be here:
And now I want to add ball node to the background node:
I used the same code to add the ball at point (-280,-150):
Why is there the change in coordinate system of scene and sprite nodes?
Another issue I am facing is:
The nodes are not added to scene properly.Even though the scene is launched, the nodes are not visible to user, but I can see the node count which says that there are nodes on the screen.I need to run the code again to see the nodes added correctly to the scene. This is happening once in every 4 times. I just need to stop the code and run it again to see the balls on the scree, else I see just the background without any balls though I am adding them to background.
Why is this happening?
I am adding all the nodes in didMoveToView