Scaling the map / using Follow

Hi,
I’m using tiled maps and the cocos class Follow, for follow the main player around the map. The problem that i have is when i want to scale the tiled map. It works. But when i scale the tiled map, Follow doesn’t work anymore. Videos:

Without scaling:
https://vimeo.com/359823935

Now, scaling:
https://vimeo.com/359823871

tiledMap->setScale(0.8);

Follow:
tiledMap->runAction(Follow::create(tiledMap->getChildByName("myCar")));

How can i fix it?
Thanks!

Quick fix is to just subclass Follow and fix it to cater for the scaling of the _target field, which you can see in Follow::step(float). In the video you posted, it seems to be moving the tile map while ignoring the scaling, so perhaps a fix would be:

void Follow::step(float /*dt*/)
{
    if(_boundarySet)
    {
        // whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
        if(_boundaryFullyCovered)
        {
            return;
        }

        Vec2 tempPos = _halfScreenSize - _followedNode->getPosition();

        _target->setPosition(clampf(tempPos.x, _leftBoundary, _rightBoundary) * _target->getScaleX(),
                                   clampf(tempPos.y, _bottomBoundary, _topBoundary)  * _target->getScaleY());
    }
    else
    {
        _target->setPosition(_halfScreenSize - _followedNode->getPosition() * _target->getScale());
    }
}

I’m simply guessing here, so that may or may not be the solution, but I’m certain that it shouldn’t be a tough issue to fix.

1 Like

Thanks @R101 !! That fixed my problem !!!
It’s a bug in Follow::step ? cc @zhangxm

Let’s ask @PatriceJiang or @coulsonwang to take a look at this thread. Is this a bug?

@tranthor did you create a GitHub issue yet and reference this thread?

Nope, First I would like to confirm if it’s a bug. Maybe not.

Issue: https://github.com/cocos2d/cocos2d-x/issues/20287

1 Like