How to add a sprite that is always on the screen in Cocos2d?

心不动则不痛 提交于 2019-12-12 02:39:15

问题


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.

Let's say I want to show a sprite in the top right corner of the screen and it would be in this position even when the screen is following the object.

Using the object position doesn't do it.

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


回答1:


As it's written here, you whould use convertToWorldSpace

convertToWorldSpace converts on-node coords to SCREEN coordinates.convertToWorldSpace will always return SCREEN position of our sprite, might be very useful if you want to capture taps on your sprite but need to move/scale your layer. Generally, the parent node call this method with the child node position, return the world’s postion of child’s as a result. It seems make no sense calling this method if the caller isn’t the parent…

So, as you can read,

Point point = node1->convertToWorldSpace(node2->getPosition()); 

the above code will convert the node2‘s coordinates to the coordinates on the screen. For example if the anchor position of node1 is which will be the bottom left corner of the node1, but not necessarily on the screen. This will convert the position of the node2 which is to the screen coordinate of the point relative to node1 ).

Or if you wish, you can get position relative to scenes' anchor points with function convertToWorldSpaceAR.




回答2:


So there are some assumptions that will have to be made to answer this question. I am assuming that you are using a Follow action on your layer that contains your map. Check here for example. Something like:

// If your playerNode is the node you want to follow, pass it to the create function.
auto cameraFollowAction = Follow:create(playerNode);
// running the action on the layer that has the game/map on it
mapLayer->runAction(cameraFollowAction);

The code above will cause the viewport to "move" to where the player is in world position. So following the player on your map that's bigger than the current viewport. What I did for my in-game menu/hud is add the Hud onto a different layer and add it to the root of the main game scene. The scene that does not have the follow action running on it. Something like below.

// Hud inherits from layer and has all the elements you need on it.
auto inGameHud = HudLayer::create();
// Add the map/game layer to the root of main game scene
this->addChild(mapLayer, 0);
// Add the hud to the root layer
this->addChild(inGameHud, 1);

The code above assumes 'this' to be your MainGameScene. This restricts the Follow action from scrolling the element off the screen. Your element will be on the screen no matter where in World space your scene currently is.

Let me know if this is clear enough. I can help you out more if you get stuck.




回答3:


I've managed to do it using a Parallax Node, and using the velocity which the sprite goes to Vec2(0,0), this way it stays always on the same spot in the screen.




回答4:


You can always just put that sprite into different node / layer that everything else is. That way moving this layer / node won't move the sprite



来源:https://stackoverflow.com/questions/33323910/how-to-add-a-sprite-that-is-always-on-the-screen-in-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!