Change the position on BOX2d Body in Cocos2d-iphone - cocos2d-iphone

I have a condition in which i have to continuously move box2d body.
I can guess two ways to move box2d body.
first, what can i do in transformed box2d body to new position
Second way is, I delete the previous body and recreate new one at new position.
Now, I want to know, which is the best way to do this? And also eager to here other way if possible.

Move the body. Reset the velocities if needed.
Deleting and recreating the body would work, but it would completely reset the body's state and you'd have to create the fixtures and shapes again too. That's overkill.

Related

How can I remove a physic node safely in cocos2d-x

I add some nodes that move around the screen. at some point, when I press a button (MenuItemImage), I need to change layer (not scene). So, I try to remove all moving nodes' PhysicBody from the PhysicWorld and then, remove nodes themselves. It works usually, but sometimes, I get crash from inside of PhysicsBody::update function. at that point, this refers moving object PhysicBody. and, it seems parent is NULL. But, I try all PhysicBody from the World. So, after I remove, they should not be drawn (I guess this is causing the problem)
It is too obvious that there are some concurrency problem. but, I don't want to touch cocos2d-x dispatcher. so, what is the proper way to remove a PhysicBody and node from the world.
now, I call removeFromWorld() for PhysicBody and removeChildren for Cocos2d node. Even, I put some delay between these two operation, I still get crash sometimes.

Trouble removing sprites from parent node

I'm making an endless running game in which users dodge obstacles, and I'm working on producing the obstacles right now. The plan I had where I'm spawning these obstacles is as follows:
obstacle->setPosition(CCPointMake(this->getContentSize().width, this->getContentSize().height*.75));
obstacle->setScale(.5);
this->addChild(obstacle);
_obstacles->addObject(obstacle);
obstacle->runAction(CCMoveBy::create(2.0, CCPointMake(-(this->getContentSize().width + obstacle->getContentSize().width/2), 0)));
obstacle->removeFromParent();
I set the position, set it's scale, add it to the scene, run an action on it so that it moves across the screen from right to left, add it to an array called _obstacles to be used elsewhere, and then I remove it from the scene so as to save memory. However, the problem is that once I try implement this, the obstacle doesn't show up at all as if it's nowhere to be seen. When I don't call obstacle->removeFromParent() it shows up and performs the action. What am I doing wrong here? If I don't call removeFromParent(), what do I call? Is there a problem in my code not related to removeFromParent()?
The reason that obstacle doesn't apeear at all is that it is removed as it start moving. You just have to create a sequence of move action and function call with obstacle as parameter and than remove this obstacle in that function , so that obstacle will be removed after moving out of screen.
CCCallFuncN *myCallFunc = CCCallFuncN::create(this, callfuncN_selector(CLASS_NAME::removeObstacles));
obstacle->runAction(CCSequence::create(CCMoveBy::create(2.0, CCPointMake(-(this->getContentSize().width + obstacle->getContentSize().width/2), 0)),myCallFunc,NULL));
Method to remove obstacle from array and from parent view
void CLASS_NAME::removeObstacles(CCObject* pSender){
// Type cast pSender to obstacle type e.g if obstacle is of CCSprite type.
CCSprite *tempObstacle = (CCSprite *)pSender;
_obstacle.pop_back(tempObstacle);
tempObstacle->removeFromParent();
}
Don't forget to replace CLASS_NAME by your class name
I would need more info on what removeFromParent is doing and how you use _obstacles
I'm taking a guess on how the parent is structured. I would guess that it has a list of children and when the parent is updated/render, it calls those functions for its children. If removeFromParent removes the object from its parent list, then that would explain why it is not being render or used in the scene. If you have a setup like this, then you should only call removeFromParent before destroying the parent object

Sprite does not jump when moving

I'm making a platform game, and everything was great. My sprite moved when i touched the left side of the screen, and jumped when i touched the right side. But then i decided to make it move by itself. so i added the ccmoveto function, but now it does not jump! I'm new to cocos2d but everything is working ok, except this, already searched but couldn't find the answer can someone please help me?
I tried everything, but it only jumps if i delete the ccmoveto action.
I'm using cocos2d 2.0
Thank you!!
CcMoveTo will override any manual position changes, inluding changes from other actions like CCJump. Your character is set to move to destination in a straight line, no matter what.
It's issues like these why I always recommend not to use actions for gameplay logic. Especially position, you need to retain full control over it. Use a direction vector and integrate position every update: and you're free to do everything you need.
my advice is to use one of the physics engines provided with cocos2d: Box2D and Chipmunk physics. Thanks to this engines you can define the characteristics of the world (i.e. gravity vector) a shape and a mass for your sprite (i.e. a rectangle with a weight). Then when you need it to jump you will just create a force vector with the characteristics you need (i.e. angle, etc.) and keep updated your sprite with its physical body. This will make your sprite jump and land quite realistically.

Board Game using SDL

I am building a board game in SDL and here is the problem I am currently facing.
I have a pawn on square 1 and I roll the dice. Based on the value I get on the dice the pawn moves to another square. I am bale to move the pawn after i read the SDL tutorials online. But the problem I am facing is that after moving the pawn to a new location the old pawn still stays at the old location. The tutorials I found on the internet moves a dot but also refreshes the background to cover up the old dot. But I cant do that as my game board is intricate and there are pawns from other players sitting there.
Is there a way in SDL that I could really move a pawn instead of having to create a new pawn at the new location and covering up the old pawn?
The fundamental concept of sprites: Before you insert the sprite, you save a copy of the original screen content. When you need to remove the sprite, you just paste the stored old content back in.
You will have to process all your objects in the correct order (LIFO) for this to work. Since you'll usually be double-buffered, this happens on the cold buffer, so this isn't an issue.
No, your code will need to be able to redraw that board position with the pawn missing. There isn't any way for the computer to automatically reconstruct what the board should look like without the pawn.
It sounds like your render code is mixed in with your game logic. You ought to separate rendering so that you can redraw the complete game scene with a single function call, which you can then use whenever a visible change is made to the game state.

Cocos 2d body & its fixtures

Well I try to implement the run action to move a specific sprite towards the specific destination, I can achieve that using the below code snippet.
[spriteA runAction:[CCSequence actions:[CCMoveTo actionWithDuration:0.2 position:realDest],[CCCallFuncN actionWithTarget:self selector:#selector(spriteMoveFinished:)],nil]];
But now the problem arise that the sprites moves perfectly but the fixture & bodydef of that sprite stays on it previous positions. Please guide me that how can I move the body & fixture along with the sprite using 'runAction' or in any other way.
Thanks & Regards,
Zahur
You have to apply force/impulse to the body and then set the sprite position to body position.