Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit: how to smoothly animate SKCameraNode while tracking node but only after node moves Y pixels?

This question and others discuss how to track a node in SpriteKit using a SKCameraNode.

However, our needs vary.

Other solutions, such as updating the camera's position in update(_ currentTime: CFTimeInterval) of the SKScene, do not work because we only want to adjust the camera position after the node has moved Y pixels down the screen.

In other words, if the node moves 10 pixels up, the camera should remain still. If the node moves left or right, the camera should remain still.

We tried animating the camera's position over time instead of instantly, but running a SKAction against the camera inside of update(_ currentTime: CFTimeInterval) fails to do anything.

like image 644
Crashalot Avatar asked Dec 12 '25 05:12

Crashalot


1 Answers

I just quickly made this. I believe this is what you are looking for? (the actual animation is smooth, just i had to compress the GIF)

enter image description here

This is update Code:

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */

    SKShapeNode *ball = (SKShapeNode*)[self childNodeWithName:@"ball"];
    if (ball.position.y>100) camera.position = ball.position;

    if (fabs(ball.position.x-newLoc.x)>10) {
        // move x
        ball.position = CGPointMake(ball.position.x+stepX, ball.position.y);
    }

    if (fabs(ball.position.y-newLoc.y)>10) {
        // move y
        ball.position = CGPointMake(ball.position.x, ball.position.y+stepY);
    }
}
like image 97
GeneCode Avatar answered Dec 13 '25 17:12

GeneCode