仅在节点移动Y像素后才能平滑地为SKCameraNode设置动画

仅在节点移动Y像素后才能平滑地为SKCameraNode设置动画

本文介绍了SpriteKit:如何在跟踪节点时但仅在节点移动Y像素后才能平滑地为SKCameraNode设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题和其他人讨论如何使用SKCameraNode跟踪SpriteKit中的节点.

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

但是,我们的需求各不相同.

However, our needs vary.

其他解决方案(例如更新SKScene的update(_ currentTime: CFTimeInterval)中的摄像机位置)不起作用,因为我们仅希望在节点将Y像素向下移动到屏幕后才调整摄像机位置.

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.

换句话说,如果节点向上移动10个像素,则相机应保持静止.如果节点向左或向右移动,则相机应保持静止.

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.

我们尝试随时间推移而不是立即对摄像机的位置进行动画处理,但是在update(_ currentTime: CFTimeInterval)内部对摄像机运行SKAction却无能为力.

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.

推荐答案

我刚刚做了这个.我相信这就是您要寻找的?(实际的动画很流畅,只是我不得不压缩GIF)

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)

这是更新代码:

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

这篇关于SpriteKit:如何在跟踪节点时但仅在节点移动Y像素后才能平滑地为SKCameraNode设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:15