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

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

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.

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.

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.

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

Related

Set position relative to scene in Cocos2d

I'm trying to use a DrawNode in cocos2d which is a child of a Sprite. The Sprite's position and rotation influences the child DrawNode's position and rotation as you would expect.
I'm trying to set the DrawNode's position and rotation to be not affected by it's parent (the Sprite).
I've tried changing self->addChild(self->drawNode) to self->getScene()->addChild(self->drawNode) which would work perfectly (if it worked), but I receive an error saying self->getScene() returns a nullptr. And this happens because it has not yet been added to any scene.
How can I position the DrawNode relative to the scene/world?
For example:
this->drawNode()->drawLine(Point::ZERO, Point(0, 100), Color4F::RED)
draw's a line going straight up from the sprite.
I am aiming for it to draw a line straight up from the bottom right of the window.
If you want to access the scene before adding the sprite to it, the current running scene can always be accessed from the director.
auto scene = Director::getInstance()->getRunningScene();
scene->addChild(draw_node);

Centering view on a moving position in SFML

I want to use sf::View in SFML in order to change the position of the view, such that the player sprite is always in the center of the screen. Thus I want to write a function which allows me to input a set of coordinates and thus center the screen around those coordinates. In addition I want to be able to set a limit to this, such that when the player reaches the side of the map, that axis of the camera stops following the player, as it has reached a "limit". How do I achieve this?
Thank you in advance.
The function you need is called sf::RenderWindow::setView .
Do something like this:
sf::RenderWindow window (sf::VideoMode(800,600),"Test");
sf::View view ();
view.setCenter (/*Set Center here*/);
window.setView (view);

Exchanging sprite content in cocos2d-x 3

I exchange sprite content with this code:
mySprite->setTexture(Director::getInstance()->getTextureCache()->addImage("newImage.png"));
The problem is that newImage.png is much smaller than the old content image of the sprite. But cocos2d-x scales newImage.png to have the same size as oldImage.png. How I can prevent this scaling. I need newImage.png to be its natural size, but to appear in the coordinates of oldImage.png (I want to retain the same sprite object, as far as I have a pointer on it and also the same position and same anchor point. That's why I use setTexture just to change the image.)
Well, this may not be the cleanest way, but it seems the most straigthforward to me :
Sprite *newSprite = Sprite::create("newImage.png");
newSprite->setAnchorPoint(mySprite->getAnchorPoint());
newSprite->setPosition(mySprite->getPosition());
mySprite->removeFormParentAndCleanup(true);
mySprite = newSprite; // <-- magic happens here
So basically you create a new sprite, place it based in old sprites position/anchor and then after removing the old one, you assigne the mySprite variable to point to the same place as newSprite.
mySprite->setTexture("newImage.png");
This sets the content Rect too.

How do you make a clickable sprite in SFML?

I've been looking through the SFML documentation for making clickable sprites, but so far I haven't found anything.
Do you guys think you could help me out?
There is nothing like sf::ClickableSprite in SFML so far, and probably there will never be. (Current list of classes in SFML)
However, you can obtain this behavior with the sf::Sprite object and the events. The idea is simple - as soon as you get the sf::Mouse::isButtonPressed(sf::Mouse::Left) event, check if the mouse is in the sprite. If it is, perform the action. You can perform another action (maybe undo) when button is released.
There is sf::Sprite::getGlobalBounds() function which returns you the position and the dimensions of the sprite. There's also sf::Mouse::getPosition() function, which returns the current position of the mouse. You can use sprite.getGlobalBounds().contains(mousePos) to check whether the mouse is in the sprite.
If you're using views, you'll need to add the view's position to sf::Mouse::getPosition(window), since it gets the mouse position relative to window coordinates.
(thanks to Chaosed0 for additional notes.)

Cocos2d scroll layer on Sprite drag

I have designed a small tutorial named "Stacker", As the name suggests, The game involves stacking blocks on each other. I have a large number of blocks to be stacked and hence all cant be accomodated in the screen itself... I m new to cocos2d and box2d but have managed to create a body with its adjoining sprite wen a user clicks on the screen. I have used MouseJoint to give movement to the body till the user performs the drag action that is till the user takes his finger off the screen.
The problem is that i need to follow the sprite (actually need the camera to follow the sprite) when the user drags it above the screen space, i referred the following links with no success... i guess wat i need is to move the body to a virtual coordinates which m not getting coz even if the screen does shift using the camera methods, but the sprite doesnt move with respect to the screen...
cocos2d forum link
flash concept but box2d
Can some1 guide me in case i need to have some pre-requisites before following camera in the manner i specified.. Thanx!
Ok Guys!
Got it guys! Had to take a global variable which records the increments per frame, The increments were equal to the layer movement which i did by setting the position of the layer to a unit less in every frame! Then set the Mouse join to the target which is (ScreenCoordinates + increment) dis too has to be done in every frame!
Done!
Cool method but needed a bit of brainstorming!!