Camera for a 2D project

Hi. I’m adding camera in my 2D game project. But if my scene is empty, camera looks like transparent and I see camera reflection and original scene. All of objects looks like double spawned. If I’m right, the camera object add the default scene and I see empty camera areas like transparent. And I see default scene projection except the camera view because of that.

Camera *camera=Camera::create();
camera->setCameraFlag(CameraFlag::DEFAULT);
this->addChild(camera);
camera->setPosition(Vec2(0,0));

How to fix that problem?

Hi.

This is not a problem because scene is creating the default camera on init, so when you are creating another camera you have two cameras on the scene. If you want to use more than one camera at the same time you should choose another camera flag, not a default and set camera mask to elements that should be visible by a new camera. Just try it and you will understand me.

Cheers.

Well, how can I get the default camera? I tried that;

Camera::getDefaultCamera();

But after, I’m getting BAD_EXC error in the runtime :frowning:

I want to use only one camera.

No, this static method is created not for actions like this. Please try this:

auto camera = your_scene->getDefaultCamera();

You can pay your attention to the following sections:


Allright. My problem is more complicated :smile: For example; I was thinking “HelloWorldScene” is an scene object. But it’s just a layer object. How can I get created scene object from this object?

In short; I want to get the default camera object from a layer object in an another object class.

I tried that;

targetSceneLayer->getScene()->getDefaultCamera();

(targetSceneLayer is HelloWorldScene)

I’m getting BAD_EXC error. Because getScene method return “NULL”.

I’m not sure I understand you correctly but if you want to get your default camera from everywhere you can try:

auto camera = Director::getInstance()->getRunningScene()->getDefaultCamera();

I can get scene with this way. Thank you.

getDefaultCamera is also returning NULL now. :frowning:

It’s very strange.
I can only advice to debug _defaultCamera and _cameras fields of scene. Sorry, can’t help you without more info.

1 Like

Ok, thank you so much. I will search defaultcamera problem.

Ok I fixed problem. The problem is that, init() function of layer is calling before director->runWithScene(HelloWorld). So when I call “Director::getInstance()->getRunningScene()”, the director don’t know what the running scene in this time. Because “director->runWithScene(scene);” is calling after “HelloWorld::createScene();”

Your codes works as well now.

1 Like

This is great that you found the issue. Good luck.

1 Like

Hi,

I’m trying to use two cameras for my 2D game - one for UI and second for the world. World camera will be moved around.
As there is default camera with DEFAULT flag in the scene, I tried to create just one additional camera in init method with USER1 for the world, set camera flag USER1 for world node after all children are added and moving camera works but there are two issues:

  1. World is rendered above UI. As I understand, default scene camera has DEFAULT flag and should render last (that is on top)
  2. I’m using scene transition with Director::getInstance()->replaceScene(TransitionFade…) and world camera starts rendering only after fade transition ends. So it looks weird as UI is visible and world pops in after delay finishes.

If I create second camera and add it as child to scene, then render order is correct but transition problem exist. After doing it Director::getInstance()->getRunningScene()->getCameras() size is 3.

Can someone explain how to use two cameras for 2D ?

Thanks

For any game I’ve created with cocos’s family I never used camera. Why do you need it? Just simple CCFollow or custom logic from this action solve anything. What is your goal?

Thanks, I just tried Follow action, but can’t figure out how to apply bounding box. Now if I pass Rect to create method, I get opposite result - it follow in the Rect and when player leaves Rect, it stops following. I want to have Rect around player and only if player gets outside that zone, follow action starts.

Do you have some demo code for such behaviour ?

i use user3 for ui and user5 for gamplay

@vsg dont use Director::getInstance()->replaceScene(TransitionFade…) if its causing problems.

simply create a sprite , tint it black, scale it, runAction(Sequence::create(FadeOut::create(), RemoveSelf::create(),NULL)

@anon98020523

I need camera follow with easing and zoom passibility. CCFollow as I understand does not have that flexibility ?

Try CCFollowDynamic it has smooth, but no zoom, as I said you need a custom logic…

@vsg i suggest not to use follow… instead lerp in update you will have way more control over it. To zoom simply change the z value :slight_smile:

Oh yea, smoothDamp is the best :wink: