Search code examples
cocos2d-xcocos2d-x-3.0

How to get the current position of my screen in Cocos2d?


I'm doing a platformer game using cocos2d-x v3 in c++, where the maps are usually very large, the visible screen follows the object through the map but I can't find a way to get the position of the visible screen in the map.

Using Size visibleSize = Director::getInstance()->getVisibleSize();gives me the dimensions of the entire map.

Let's say I want to show a sprite in the top right corner os the screen whenever the object reaches a point in the map, and this sprite would be always in the top right corner os the screen.

Using the object position doesn't do it.

Is there a way to get the position of the current screen? Or is there a way to show a sprite or whatever in the screen and it would be in the screen even when the screen is moving?

Ps. I'm super noob in game development


Solution

  • There is a simple resolution:

    You need two layer: 1: gamelayer 2: uilayer

    Gamelayer is the one which you can move.Your map is added on gamelayer and when you need move the map you can just move the gamelayer.

    Uilayer doesnt't move and its zorder is larger than the gamelayer.The sprite that shuold be always in the top right corner of the screen is on this layer.

    Also: There is a more complicated way: You doesn't need to move the gamelayer.Just bind a camera to gamelayer and bind another camera to uilayer.

    When you need to see the rest of your big map,you just move the camera of the gamelayer where you want.

    BTW:

    This is my first answer on stackoverflow.

    English is not my first language,not very fluent.

    Hope this can help you :)