Probably quite a simple question! I'm making a side scrolling shooter game using Xcode5, with Cocos2d Version 3.
When the main character (on the left of the screen) flies up and down, I want the enemy (on the right of the screen) to follow him along the Y axis.
I've set up an action so the enemy enters, then moves to the main character's Y position. But the enemy moves to the main characters starting Y position, not it's current Y position.
Is there code to get the current position of a sprite? Or is there perhaps a better way to achieve this? Basically we want the enemy sprite to know the main characters Y position and move to it continuously.
Thanks
Every CCNode has a position property.
#property(nonatomic,readwrite,assign) CGPoint position;
So use this:
CGPoint position = yourSprite.position;
Related
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
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);
I'm following the tutorial here to mimic Flappy Bird. At the part to scroll the game scene:
- (void)update:(CCTime)delta {
_hero.position = ccp(_hero.position.x + delta * scrollSpeed, _hero.position.y);
_physicsNode.position = ccp(_physicsNode.position.x - (scrollSpeed *delta), _physicsNode.position.y);
...
}
Ideally the whole world will scroll left, and _hero (which is a child node of _physicsNode) move right with the same speed it would stay still on the screen. But when I run the code on simulator the _hero sprite just blast off to the right at the speed of light (about 10~20 times faster than the scrolling speed of _physicsNode). The _physicsNode and every other things inside it is scrolling left at normal speed as intended.
If I don't give _hero any movement it would scroll along with _physicsNode normally.
I have tried other method like using CCAction at game start:
CCAction *moveAction = [CCActionMoveBy actionWithDuration:1 position:ccp(-scrollSpeed,0)];
CCActionRepeatForever *repeatAction = [CCActionRepeatForever actionWithAction:(CCActionInterval *)moveAction];
[_physicsNode runAction:repeatAction];
And still get the same result. The speed value that _hero receive is always different from the speed that _physicsNode receive.
Could anyone explain to me why this is happening and how to fix it?
I'm using cocos2d 3.3 if it helps.
I finally figured it out.
The _hero node had its positionType set to CCPositionTypeNormalized (because I was trying to center him on the screen) while the _physicNode has its positionType as CCPositionTypePoints. Any change to the node's position (including movement actions) are based on the node's positionType. The result is that when I move the _hero node by 2, it doesn't move 2 points but 2% of the parent node's width instead.
I fixed it by aligning the _hero on SpriteKit using CCPositionTypeNormalized, then switching it to CCPositionTypePoints at game start.
I have a problem when I try to update my camera.
I want to change the pitch and yaw of the the camera (where its looking) via the mouse
But I want the mouse to stay positioned to the center of the window.
//where MouseP.x .y is the mouse position
//(which is centered to the current window)
//get old position of the mouse
OldP.x = MouseP.x;
OldP.y = MouseP.y;
//work out the distance traveled
Delta.x = MouseP.x - OldP.x;
Delta.y = MouseP.y - OldP.y;
//update the camera(using distance traveled)
Rot.Yaw -=Delta.x/5;
Rot.Pitch -= Delta.y/5;
//move mouse to the center of the screen
SetCursorPos(CENTER_SCREEN_X,CENTER_SCREEN_Y);
the problem is the camera snaps back to a certain point as the mouse is set to return to the origin.
I want to update the camera by the distance traveled from the origin but not the distance back to the origin.
If I take it out, it works wonderfully but the then mouse can go out of the window.
One has to be careful with cursor and 3D mouse movement. People tend to think it is related, but in fact it is not.
read the article from the msdn:
http://msdn.microsoft.com/en-us/library/windows/desktop/ee418864%28v=vs.85%29.aspx
"Taking Advantage of High-Definition Mouse Movement"
this is how one must get mouse input in a 3D application.
the cursor should be hidden.
If you try to recenter it, it will create a horrible jaggy feeling where the cursor is trying to escape the center with the user moving the mouse but is kept in place by an invisible spring. Which doesn't look very profesional. You can't fight that because your application is not scheduled before mouse cursor display.
I believe the issue here is that your block of code probably is inside the catch of a WM_MOUSEMOVE event?
When you call SetCursorPos, it itself generates another WM_MOUSEMOVE event, so you process this block of code once when you move the mouse, and once again when you call SetCursorPos and it does the opposite of this.
You probably don't want to put SetCursorPos inside of a WM_MOUSEMOVE event catch, or else you'll generate an endless loop of messages (every SetCursorPos generates another one).
Perhaps you can move this code outside of the message pump, and just run it once per frame in your update loop: query the current mouse position, make your camera transform, then set the cursor back to the origin.
if(g_States::Instance().MouseLook())
{
//Test the mouse input
POINT mousePos;
GetCursorPos(&mousePos);
mouseX = mousePos.x; //g_InputEngine::Instance().GetX();
mouseY = mousePos.y; //g_InputEngine::Instance().GetY();
mouseX = mouseX - m_HalfWidth;
mouseY = mouseY - m_HalfHeight;
mouseFloat = mouseX * C_MOUSESMOOTHINGFACTOR;
g_Scene::Instance().GetCamera()->RotateYaw(-mouseFloat);
mouseFloat = mouseY * C_MOUSESMOOTHINGFACTOR;
g_Scene::Instance().GetCamera()->RotatePitch(mouseFloat);
//Reset mouse to center on the screen
SetCursorPos(m_HalfWidth,m_HalfHeight);
}
So this is the Mouselook function of a spacegame prototype I was developing for fun a while back what I did was change it to use GetCursorPos(&mousePos); instead. This will get the current position of the cursor no matter when your input code updates the mouse cursor location. The rest of the math in the function is just for the sensitivity and actually rotating the camera. Hopefully this code helps a little getting yours working.
Let me know if you need more explanation.
Edit: I just remembered the reason i did this. It was because the screen was flickering it would move but then the input engine would get updated by the SetCursorPos() call because i was using WM_MOUSEMOVE to update the input engine. I'm not sure how your getting your input but this should still help you.
I'm writing a simple game and I'm going to have the mouse control the camera (using GlutPassiveMotionFunc).
I'm going to pitch and yaw based off the mouse difference between callbacks, however I think it would be a good idea to "force" the mouse back to the center of the screen every time they tried to move it. This way their cursor won't be at the edge of the screen and they can't move any further in that direction.
What Glut / OpenGL command can I use to force the position of the mouse to change?
Use glutWarpPointer(x, y), where x and y (both ints) are in pixels (relative to the window's origin). For example:
glutWarpPointer(windowWidth / 2, windowHeight / 2);
Sorry for the late answer. What i meant was that after i use glutWrapPointer function, if i print out the x and y values, i could not catch the change on the cursor and it always prints out the values inside the glutWrapPointer function that i provided. What i did to fix it was to move this function inside the Animate function of openGl and it worked fine.