haxpanel
haxpanel

Reputation: 4678

Simple 2D game on iPhone with cocos2d - tips and tricks

I'd like to create a simple 2d game: There is a car in the middle of the screen and the background is moving back resulting the feeling of the speed of the car. The road has uphill and downhill as well so the car has to follow its shape. The user can accelerate and break. Thats all!

I did some research and found cocos2d. I downloaded it, read the tutorials and studied the examples, but to start developing my simple game I still have questions I haven't got the answer.

How should I create the road (2d) to put the car on it? How should I move the background (and the road) from right to left making the feel of the speed of the car? How should I create the car?

I don't want to have source code as answer but theoretical advices, good tutorials and examples. Thanks!!

Upvotes: 4

Views: 1199

Answers (2)

mm24
mm24

Reputation: 9596

I strongly reccomend you buy Steffen Itterheim's book "Learn Cocos2D" on Amazon. It helped me a lot getting started by reading it and playing wit his source code: http://www.learn-cocos2d.com/ If you don't want to spend money look at RayWenderlich tutorials series. There is a team working with him and there are lots of tutorials on how to build your first game (check them out, they are very good!): http://www.raywenderlich.com/tag/cocos2d. For moving a car is just a CCSprite object that moves during the update method. Position is relative to abstract coordinates (0-320 width, and 0-640 height) and you can set it with the position property (sprite.position = CGPointMake(10.0f, 200.0f) will place your sprite in pos x:10, y:200). you need to add the sprite as child of the scene and scheduleUpdate in the init method and there have the logic to move the sprite according to the input (you can choose accellerometery input, or opt for some cool controllers like SneakyInput. More on this on Steffen's book :).

You should add the "ParallaxBackground" as separate layer and get it to move by scheduling update. Is the same concept as the car but with a different input (you don't need a controller for it but the input will be relative to the car's position. You can get the car as a child from the parent node by using parent.getChildByTag(CARTAG)).

This is a bit raugh but I hope to give an initial idea..

Upvotes: 1

mr.Pony
mr.Pony

Reputation: 257

If you need a realistic result, you may consider using box2d or chipmunk (already embedded in cocos2d) to make the physics, so that the car will acts like those objects in "Doodle Truck" or "Tiny Wings". There are pretty much tutorial on the Internet. Of cox, that require some more tuning and settings to make the game work properly. Otherwise, you need to think about the data structure of the "road" - which make the car knows that it should be of which rotation angle. I think this is the most hard part of this simple game.

For moving the background, that is simple and should have many possible ways. The simplest I can think of is setting its position frame by frame according to the current speed of the car.

Creating the car: you can make a class representing it, including a CCSprite in it, which handle the animations, rotations, flip....and anything else related to displaying the car.

Upvotes: 0

Related Questions