Camera for a 2D project

Camera for a 2D project
0.0 0

#1

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?


#2

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.


#4

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.


#5

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:



#6

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”.


#7

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();


#8

I can get scene with this way. Thank you.

getDefaultCamera is also returning NULL now. :frowning:


#9

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


#10

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


#11

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.


#12

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


#13

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


#14

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?


#15

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 ?


#16

i use user3 for ui and user5 for gamplay


#17

@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)


#18

@KAMIKAZE

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


#19

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


#20

@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:


#21

Oh yea, smoothDamp is the best :wink: