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));
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.
Allright. My problem is more complicated 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”.
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();”
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:
World is rendered above UI. As I understand, default scene camera has DEFAULT flag and should render last (that is on top)
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 ?
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.