我可以让CCFollow跟随更自然吗?

发布于 2025-01-08 18:58:03 字数 321 浏览 0 评论 0原文

我想用cocos2d/Box2D构建一个平台游戏。我使用 CCFollow 来跟随玩家精灵,但 CCFollow 不断将其放置在屏幕中央。我希望 CCFollow 能够更自然地跟随,就像人类转动摄像机一样,具有可接受的滞后、小超调等。

这是我的一种不起作用的方法:我将一个小型物理体(通过距离关节)附加到玩家身上,该物体不会与任何物体发生碰撞,由透明精灵表示。我CCFollow了透明精灵。我希望这个幽灵身体能像气球一样附着在玩家身上,从而实现视野的平滑转移。问题是距离关节因太重或太轻的物体而断裂。气球随机移动,当然,无论它有多轻,它都会将玩家拉回来一点。

平滑地跟随移动精灵的更好方法是什么?

I want to build a platform game with cocos2d/Box2D. I use CCFollow to follow the player sprite but CCFollow constantly puts it in the center of the screen. I want CCFollow to follow more naturally, like a human turning a camcorder with an acceptable lag, a small overshoot ...etc.

Here is a method of mine that didn't work: I attached (via a distance joint) a small physics body to the player that doesn't collide with anything, represented by a transparent sprite. I CCFollow'ed the transparent sprite. I was hoping this ghost body would act like a balloon attached to the player, hence a smooth shift in view. The problem is distance joint breaks with too heavy - too light objects. The balloon moves around randomly, and of course, it pulls the player back a little no matter how light it is.

What is a better way of following a moving sprite smoothly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮坠入山谷 2025-01-15 18:58:03

尝试将其添加到 cocos2d 库中的 CCActions 中。

-(void) step:(ccTime) dt
{
#define CLAMP(x,y,z) MIN(MAX(x,y),z)

    CGPoint pos;
    if(boundarySet)
    {
        // whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
        if(boundaryFullyCovered) return;

        CGPoint tempPos = ccpSub(halfScreenSize, followedNode_.position);
        pos = ccp(CLAMP(tempPos.x,leftBoundary,rightBoundary), CLAMP(tempPos.y,bottomBoundary,topBoundary));
    }
    else {
        // pos = ccpSub( halfScreenSize, followedNode_.position );
        CCNode *n = (CCNode*)target_;
        float s = n.scale;
        pos = ccpSub( halfScreenSize, followedNode_.position );
        pos.x *= s;
        pos.y *= s;
    }

    CGPoint moveVect;

    CGPoint oldPos = [target_ position];
    double dist = ccpDistance(pos, oldPos);
    if (dist > 1){
        moveVect = ccpMult(ccpSub(pos,oldPos),0.05); //0.05 is the smooth constant.
        oldPos = ccpAdd(oldPos, moveVect);
        [target_ setPosition:oldPos];
    }

#undef CLAMP

}

我从 cocos2d 论坛得到这个。

Try add this to CCActions in cocos2d libs.

-(void) step:(ccTime) dt
{
#define CLAMP(x,y,z) MIN(MAX(x,y),z)

    CGPoint pos;
    if(boundarySet)
    {
        // whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
        if(boundaryFullyCovered) return;

        CGPoint tempPos = ccpSub(halfScreenSize, followedNode_.position);
        pos = ccp(CLAMP(tempPos.x,leftBoundary,rightBoundary), CLAMP(tempPos.y,bottomBoundary,topBoundary));
    }
    else {
        // pos = ccpSub( halfScreenSize, followedNode_.position );
        CCNode *n = (CCNode*)target_;
        float s = n.scale;
        pos = ccpSub( halfScreenSize, followedNode_.position );
        pos.x *= s;
        pos.y *= s;
    }

    CGPoint moveVect;

    CGPoint oldPos = [target_ position];
    double dist = ccpDistance(pos, oldPos);
    if (dist > 1){
        moveVect = ccpMult(ccpSub(pos,oldPos),0.05); //0.05 is the smooth constant.
        oldPos = ccpAdd(oldPos, moveVect);
        [target_ setPosition:oldPos];
    }

#undef CLAMP

}

i get this from cocos2d forums.

尛丟丟 2025-01-15 18:58:03

也许这个 http://www.cocos2d-iphone.org/wiki/doku .php/prog_guide:actions_ease 可以帮助您通过CCFollow获得“加速”效果。

Perhaps this http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:actions_ease can help you get an "acceleration" effect with CCFollow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文