How to split screen or have variable layer size

How to split screen or have variable layer size
0.0 0


Isn’t that used for isometric games? But lets make it simple, the cpp test you asked me to refer, if I have to use the same example, but instead of drawing the layer on the entire screen, I need to draw it only on 80% of the screen, how will I do it? setContentSize isn’t working!


show me your code. setContentSize() does work just fine. I use it all the time.

#include "ParallaxTest.h"
#include "../testResource.h"




// Parallax1

    // Middle layer: a Tile map atlas
    auto tilemap = TileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16);
    // change the transform anchor to 0,0 (optional)
    tilemap->setAnchorPoint( Vec2(0, 0) );

    // Anti Aliased images

    // background layer: another image
    auto background = Sprite::create(s_back);
    // scale the image (optional)
    background->setScale( 1.5f );
    // change the transform anchor point (optional)
    background->setAnchorPoint( Vec2(0,0) );

    // create a void node, a parent node
    auto voidNode = ParallaxNode::create();
    // NOW add the 3 layers to the 'void' node

    // background image is moved at a ratio of 0.4x, 0.5y
    voidNode->addChild(background, -1, Vec2(0.4f,0.5f), Vec2::ZERO);
    // tiles are moved at a ratio of 2.2x, 1.0y
    voidNode->addChild(tilemap, 1, Vec2(2.2f,1.0f), Vec2::ZERO);

	voidNode->setContentSize(Size(10, 10));

    addChild( voidNode );


this is the code from cpp-tests.


Yes. And in this same code, if I wanna display the scene in just a small part of the window screen, how do I do that?


oh, a Scene, you can’t display multiple scene’s at once. I thought we were talking about Layers all this time.


Ok, what if I want two layers to be displayed, in which the content of first layer is very big, for example (4000x4000), and I want to display this layer on a window screen of (1920x1080) such that layer1 display contents in the first (1920x1000) part and layer2 display contents in the lower (1920x80) part? Now, 4000x4000 > 1920x1080 and it gets clipped by the window size. Fine. But I want it to get clipped by 1920x1000 because that is the layer size. That never happens.


show me how you are attempting this, then so I can see what is happening.


Earlier I tried attempting this with Camera:Follow + SetContentSize. My fifth comment in this thread shows the same. Then you suggested me to use Parallax Node + SetContentSize. Since I was new to the Parallax, I used the tutorial, and tried to add the SetContentSize Part in that. Because it is basically what I want to achieve, scrollable + clipping. But it is getting clipped by window size and not by SetContentSize. That’s what I did in the code I mentioned four-five comments above.


In fact, I have a doubt, what does SetContentSize actually do? Can it clip data? If I have content of 4000x4000 and I setContentSize to 1000x1000, will it clip the rest of 3000x3000?


I looked at the code you provided but I don’t see a single setContentSize call except in the Parallax Sample of 10,10.

Post you actual code that shows how you get the screen size and how you are using it to set the size of your layers, etc.


These are the only two codes. I assume we are talking only about the second code. And in second code, there is a setContentSize. Screen Size is bigger than 10x10, so I tested casually, I wanted only the 10x10 part of the layer to be displayed on the screen, rest all should have been clipped!

mGameHUDLayer = Layer::create();
	mGameWorldLayer = ParallaxNode::create();

	auto visibleSize = Director::getInstance()->getVisibleSize();
	mGameWorldLayer->setContentSize(cocos2d::Size(visibleSize.width, visibleSize.height * 0.8));
	mGameWorldLayer->setAnchorPoint(Size(0, 1));
	mGameWorldLayer->setPosition(cocos2d::Vec2(0, visibleSize.height * 0.2));

	addChild(mGameWorldLayer, 0);
	addChild(mGameHUDLayer, 2);

	auto map = TMXTiledMap::create("F:/DEN Games/Bang Bang/Game/Levels/Level2.tmx");
	map->setAnchorPoint(Size(0, 0));
	mGameWorldLayer->addChild(map, -1, Vec2(0.4f, 0.5f), Vec2::ZERO);
        mPlayer = Sprite::create("F:/DEN Games/Bang Bang/Game/Images/Tank.png");
	mPlayer->setAnchorPoint(Vec2(0, 1));
	mPlayer->setPosition(0, mPlayer->getContentSize().height);
	mGameWorldLayer->addChild(mPlayer, 1, Vec2(2.2f, 1.0f), Vec2::ZERO);


I have two layers. mGameWorldLayer, which is supposed to be displayed on 80% of the screen, so I set the appropriate content size. Second Layer is HUD Layer, I add both the layers to the scene.
Then I add a tilemap, and a character on the mGameWorldLayer. When I run it, GameWorldLayer gets drawn on the whole screen. I think now I have established my problem quite well.


can you post your images and TMX so I can just drop this into a new project and test?


this is wrong: mGameWorldLayer->setAnchorPoint(Size(0, 1));

#37 (4.5 KB)


I commented that line!

#39 (16.5 KB)

Sorry, forgot, the TMX file will require this too.


what is that image called? Discourse gives it a random name.

also, post a screenshot of what you are seeing, please.