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

廉价感情. 提交于 2019-12-24 00:48:51

问题


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.


回答1:


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)

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);
    }
}



回答2:


I would not put this in the update code, try to keep your update section clutter free, remember you only have 16ms to work with.

Instead create a sub class for your character node, and override the position property. What we are basically saying is if your camera is 10 pixels away from your character, move towards your character. We use a key on our action so that we do not get multiple actions stacking up and a timing mode to allow for the camera to smoothly move to your point, instead of being instant.

class MyCharacter : SKSpriteNode
{
    override var position : CGPoint
    {
        didSet
        {

            if let scene = self.scene, let camera = scene.camera,(abs(position.y - camera.position.y) > 10)
            {
                let move = SKAction.move(to: position, duration:0.1)
                move.timingMode = .easeInEaseOut
                camera.run(move,withKey:"moving")
            }
        }
    }
}

Edit: @Epsilon reminded me that SKActions and SKPhysics access the variable directly instead of going through the stored property, so this will not work. In this case, do it at the didFinishUpdate method:

override func didFinishUpdate()
{
    //character should be a known property to the class,  calling find everytime is too slow
    if let character = self.character, let camera = self.camera,(abs(character.position.y - camera.position.y) > 10)
    {
        let move = SKAction.move(to: character.position, duration:0.1)
        move.timingMode = .easeInEaseOut
        camera.run(move,withKey:"moving")
    }
}


来源:https://stackoverflow.com/questions/40969539/spritekit-how-to-smoothly-animate-skcameranode-while-tracking-node-but-only-aft

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!