Question

I am developing a space shooter with SpriteKit for testing purposes. The game field is 700 x 700 pixels/points large. obviously is does not fit into a iPhone screen. That means I need some sort of scrolling that affects players + enemies + bullet + asteroids. I searched google, but most subjects on scrolling refer to Cocos2D and nearly always to Cocos2D specific features, not provided by sprite kit. So the thing is this is my first game so I am not sure what the right way is to implement multidirectional scrolling. I hope you guys can give me a solution and/or hints/tutorials or anything else that helps me :D

Was it helpful?

Solution

Create your "spaceship", create a camera node and in the updates method make your camera always center on the spaceship.

Something similar to this question: How to make camera follow SKNode in Sprite Kit?

-(void)centerOnNode:(SKNode*)node {
    CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];
    cameraPositionInScene.x = 0;
    node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x, node.parent.position.y - cameraPositionInScene.y);
}

Then you'll have to figure out your own game mechanics on how the ship moves but if you simply imply the ship follows your finger you can use SKActions to animate to finger location. Example: https://stackoverflow.com/a/19172574/525576

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {

        for (UITouch *touch in touches) {

            CGPoint location = [touch locationInNode:self];

            CGPoint diff = CGPointMake(location.x - _myPlayer.position.x, location.y - _myPlayer.position.y);

            CGFloat angleRadians = atan2f(diff.y, diff.x);

            [_myPlayer runAction:[SKAction sequence:@[
                                [SKAction rotateToAngle:angleRadians duration:1.0],
                                [SKAction moveByX:diff.x y:diff.y duration:3.0]
                                ]]];
        }
    }
}

OTHER TIPS

Solved it, thanks to ray wenderlich

CGSize winSize = self.size;

int x = MAX(player.position.x, winSize.width / 2 );
int y = MAX(player.position.y, winSize.height / 2 );
x = MIN(x, worldSize.width - winSize.width / 2 );
y = MIN(y, worldSize.height - winSize.height/ 2 );
CGPoint actualPosition = CGPointMake(x, y);

CGPoint centerOfView = CGPointMake(winSize.width/2, winSize.height/2);
CGPoint viewPoint = ccpSub(centerOfView, actualPosition);

world.position = viewPoint;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top