2
votes

I have created my SKScene subclass which sets the anchorpoint and then adds one SKSpriteNode for the world, the world has multiple SKSpriteNodes for the obstacles, player etc. I am also centering on the

The problem I am having is that as I have set the anchorpoint of the scene to (0.5, 0.5), the position of any child node that I add to the world starts at the center of the world. How do I fix the postion of the nodes so that position = (0,0) will be at the bottom left of the world node and any child nodes added to it, instead of the center?

@implementation LevelScene

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {

    NSLog(@"width:%f height:%f", self.view.bounds.size.width, self.view.bounds.size.height);


    // set the physics body
    self.physicsWorld.gravity = CGVectorMake(0,-5);
    self.physicsWorld.contactDelegate = self;
    self.anchorPoint = CGPointMake(0.5, 0.5);

    NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"LevelScene" ofType:@"plist"]];
    NSString *backgroundImage = [plistDict objectForKey:@"background"];

    // add a node that holds the background
    background = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:backgroundImage] size:CGSizeMake(1024, 768)];
    background.position = CGPointMake(0, 0);
    [self addChild:background];

    world = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(1024, 768)];
    world.position = CGPointMake(0, 0); // this should be bottom-left
    world.size = CGSizeMake(1024, 768);
    world.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:world.frame];
    world.physicsBody.categoryBitMask = worldCategory;
    [self addChild:world];

    // load in the game tiles (these are non-dynamic tiles the player can use)
    [self loadInTiles];

    // add in game object to the world skspritenode - this just creates a subclass of skspritenode and sets position to 0,0
    [self addGameObject:CGPointMake(0, 0)];

...
}

// ...setup functions, input handling, etc

-(void)didSimulatePhysics {

    // setup the player to move depending on their direction
    [player updatePosition];

    [self centreOnNode:player];
}

-(void)centreOnNode: (SKSpriteNode *)node {
    CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];

    CGFloat x = node.parent.position.x - cameraPositionInScene.x;
    CGFloat y = node.parent.position.y - cameraPositionInScene.y;
    NSLog(@"camera x:%f y:%f", x, y);

    NSLog(@"world frame origin x:%f y:%f", world.frame.origin.x, world.frame.origin.y);

    node.parent.position = CGPointMake(x, y);
}
2

2 Answers

2
votes

If you want to set the world sprite's origin to the bottom left side, just set it's anchor point.

world.anchorPoint = CGPointMake(0,0);

With this, the world sprite's coordinate system will be just like that of the scene's default.

Make sure to remove the line:

self.anchorPoint = CGPointMake(0.5, 0.5);
1
votes

Replace this row:

world.position = CGPointMake(0, 0);

by this:

world.position = CGPointMake(-CGRectGetMidX(self.frame), -CGRectGetMidY(self.frame));

(0,0) is the center of the scene, since you set anchor point of the SKScene to (0.5,0.5)