How to make camera follow player

Hello, i used this to follow playler:

setCamera(){
followPlayer = Follow::create(mPlayer, Rect(x1,y1,x2,y2));
followPlayer->retain();
 this->runAction(followPlayer);}

but it doesnot work when add physics. please help me. thanks u guys.

What is Follow? Cocos2d::Follow?

Show is More about it and what is your Camera setup?

What is the scope of these variables?

mPlayer, x1,y1,x2,y2

Have you looked in cpp-tests to see how it is used?

void GameLayer::setCam(){
    float x1= 0.0f;
//    float y1= 0.0f;
    float y1 = -150;
    float x2 = mMapLevel->getMapWidth() * mMapLevel->getTileWidth();
    float y2 =  mMapLevel->getMapHeight() * mMapLevel->getTileHeight() + 400;
     followPlayer = cocos2d::Follow::create(mPlayer, Rect(x1,y1,x2,y2));
    followPlayer->retain();
    this->runAction(followPlayer);
}

it work without physics sir. but doesnot work when add physics

and what is the physics implementation? Posting more code is always better than less code. Most of us don’t have time to guess :slight_smile:

1 Like

example:
helloworldScene.h :

#include "cocos2d.h"
class PlayerLayer;
class HelloWorld : public cocos2d::Scene
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();
    
    // a selector callback
   PlayerLayer * mPlayer;
    cocos2d::Follow * camera;
    void update(float dt)override ;
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

HelloWorldScene.cpp:

#include "HelloWorldScene.h"
#include "PlayerLayer.h"
USING_NS_CC;

Scene* HelloWorld::createScene()
{
    auto scene =cocos2d::Scene::createWithPhysics();
    scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
    scene->getPhysicsWorld()->setGravity(Vec2(0.0f,-200));
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
    printf("Error while loading: %s\n", filename);
    printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Scene::init() )
    {
        return false;
    }

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    mPlayer= new PlayerLayer;
    mPlayer->init();
    mPlayer->setPosition(Vec2(visibleSize.width / 2, visibleSize.height));
    this->addChild(mPlayer);
    camera= Follow::create(mPlayer,Rect::ZERO);
    runAction(camera);

    return true;
}

PlayerLayer.h :

#include "cocos2d.h"

USING_NS_CC;

class PlayerLayer: public cocos2d::Sprite {
public:
    PlayerLayer();
    ~PlayerLayer();
    static PlayerLayer* create();
    bool init() override ;

    Sprite* mPlayer;
    cocos2d::LayerColor * sceneColor;
    float mPrePlayerPs;
};

PlayerLayer.cpp :

#include "PlayerLayer.h"
PlayerLayer::PlayerLayer() {

}
PlayerLayer::~PlayerLayer(){}
bool PlayerLayer::init() {
    if(!Sprite::init()){
        return false;
    }
    log("player init");

//    sceneColor= LayerColor::create(Color4B(0, 255, 255, 255));
//    this->addChild(sceneColor);
    mPlayer= cocos2d::Sprite::create("HelloWorld.png");
    auto playerPhysics = PhysicsBody::createBox(mPlayer->getContentSize());

    this->setPhysicsBody(playerPhysics);
    this->addChild(mPlayer);

 
    return true;
}

when the player is in free fall, I want the camera to follow the player.

Scene* MainGame::createScene()
{
    auto scene =cocos2d::Scene::createWithPhysics();
    scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
    scene->getPhysicsWorld()->setGravity(Vec2(0.0f,-200));
    auto layer = MainGame::create();
    scene->addChild(layer);
    return scene;
}

2D physics

So it looks like you’re running the Follow action on the HelloWorld scene object itself and not the default camera for the scene, i.e. Director::getInstance()->getRunningScene()->getDefaultCamera();

When you call it on the HelloWorld scene object it and everything that is a child of it (including the PlayerLayer since it’s added as a child to HelloWorld) will be following the position of the PlayerLayer object as it moves through the HelloWorld scene. This likely creates some very odd behavior since we are moving the player and then moving the world containing and including the player, which then compounds on itself.

Try using the Follow action on the scene’s default camera instead, as you can think of the scene camera as the “eyes” that the player’s screen will see.

Hopefully this helps you!!

1 Like