Move background - Cocos2D - cocos2d-iphone

I am trying to develop a car game in which opponents are coming from opposite direction. And we have to avoid collision. For this I want to move my background as the opponent cars approach the good car. What should I do to move the background?
Regards,
Stone

1.- You need to create a map: example with program "Tiled".
2.- Add to resources
3.- Insert code:
CCTMXTiledMap * tmxMap = [CCTMXTiledMap tiledMapWithGMXFile:#"Map.tmx"];
[tmxMap runAction:[CCMoveBy actionWithDuration:1.0 position:ccp(0,-400)];

Related

How to set up a score board using SFML and Box2D in C++

I want to set up a scoreboard for these two players (Pikachu)
My questions are:
how to detect when the ball touches the ground?
how to setup an object that can change during the game?
like the two in this picture:
I'm very new to SFML and Box2D, so even if I try to read the source code to see what I can use to implement things above, I still have no idea.
Can anyone give me some clues?
To create a scoreboard similar to the one in the image provided you can put text on the screen that holds the current score of the players and then placing it in the position where you want the score to be placed on the screen. In SFML this would be done by creating a sf::Text object.
To answer your other questions.
1.how to detect when the ball touches the ground
A simple way of doing this would be to check the Y value of the coordinates of the ball and from that determining if the ball is touching the ground.
2.how to setup an object that it can change during the game.
I am unsure what you mean by this.

How to create a b2body using box2d that is continuously moving left and right and doesn't fall down when player jumps on to it?

I am working on a platformer game using cocos2d with box2d in which i have ground body that must continuously move to left and right, but doesn't fall when player jumps onto it. I am not able to do so, my body falls when player jump onto it. Provide me some link or code to do so.
I think the best solution is to move the sprite for the platform, not the platform itsef.
Instead of creating a small platform for the jumping, create a rectangle that extends across the entire pit.
Only move the sprite over the region that you want the player to jump on to (i.e. move it left and right so the player knows the "safe" area to jump).
Use the b2ContactListener to detect the collision between the rectangle and the player. If they are in contact and the player is over the moving sprite, then don't do anything different.
If they are in contact and the player is NOT over the moving sprite, disable the collisions response in the PreSolve event of the b2Contact listener and for the entire time the player is in contact with it. The player should fall through to the pit below.
You could also use a sensor body for the moving platform to give you a better "contact mechanism" than just the sprite. So if the player is in contact with the "platform sensor" and the "rectangle across the pit", don't do anything in the PreSolve event. Otherwise, if they are only in contact with the "rectangle across the pit", let them drop through it.
Does this work?

Cocos2d change image of hero in accelerometer when an object hits it

Hi. I am new on this website and also in cocos2d. I am a student and I need your help.
I am making a game based on one of the tutorials in a cocos2d game development book. The concept is simple; different objects are falling from the top of the screen and I have to avoid or catch them by tilting the device. The main character, which is one which has to avoid objects, has different properties which can change by grabbing different objects (e.g. the player may have a shield if it grabs one). In order to display the shield I have to change the sprite of the player. I am not sure how I can achieve this. Could anyone help me in providing some guidelines on this?
Use setTexture to switch the image (texture) of your current sprite with another:
[playerSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"playerWithShield.png"]];

endless runner game like dino rush object placement in cocos2d

My game is mostly already completed, but I need some changes related to the placement of objects. I have the same objects like the dinorush game. Like (fruit, block, missile, ball etc). I need proper placement of all objects like in the dino rush game. Currenly I am using random positions and movement of objects. I have not used any physics in my game. Also if I design placement using a tile map, then can we have magnet effects when my player stands near to the banana object? (I mean all banana automatically are attracted by the player)
Please help me. I am newbie in cocosd. This is my first game in cocos2d.
You can add a new class inherited the ccNode named magnet(banana) then add it to your game.Calculate the distance between the player and the banana.If the distance is less than the number you want, then move it to the player.
-(void)update:(ccTime)delta
{
float distance = ccpDistance(self.position, player.position);
if(distance < d){
CCAction *action = [CCMoveTo actionWithDuration:1.0f position:player.position];
[self addChild:action];
}
}

Multiple cpShapes (chipmunk)

So I have created two shapes( poly's to be exact) and I need to be able to attach them to a single CCSprite/image, and have them stay in the position that I have created them at. Is there any way to do that?
So in short this is what I am trying to achieve.
I have a tire (CCSprite/image) and I have created 2 poly's/cpShapes, one for the top and one for the bottom of the tire so that when the user throws a football it can only go through the center of it.
I need to attach the top poly to the top of the tire and the bottom poly to the bottom of the tire
I also need to be able to use collision on the tire. For instance if the user throws the football and it hits the bottom of the tire, I need to be able to make the tire sway back and forth with both shapes moving with it.
So my question is really only how can I attach two cpShapes to one image/CCSprite?
I have created my shapes like so:
cpShape *UpperShape = [game.spaceManager addPolyAt:cpv(70,195) mass:STATIC_MASS rotation:0 numPoints:6 points:cpv(2,12), cpv(28,8), cpv(33,0), cpv(36,-10), cpv(-33,-10), cpv(-20,8)];
cpShape *LowerShape = [game.spaceManager addPolyAt:cpv(70,125) mass:STATIC_MASS rotation:0 numPoints:7 points:cpv(34,8), cpv(31,0), cpv(25,-9), cpv(7,-13), cpv(-20,-8), cpv(-30,0), cpv(-35,8)];
P.S I am using spaceManager+chipmunk
Basically, you create a single cpBody, and attach the 2 cpShape's to it. Using ccPhysicsSprite allows you to attach a sprite to a body, which is the result you're trying to get.
Unless spaceManager is doing something for you, it's up to you to determine how cpShapes and CCSprites attach. All I would do is subclass CCSprite and override -draw. In the -draw function update the position and rotation of the sprite to the center of the two shapes combined.
It would probably be easier to make two sprites and attach one sprite to one shape though.