Daniel
Daniel

Reputation: 6842

Cocos2D camera movement?

What is the best way I center the screen to follow the character? Should I use CCCamera or just move the background around?

I am new to cocos2d so please show me exact code.

BTW: I am also using box2d in this project.

Thanks

Upvotes: 1

Views: 4042

Answers (2)

Srinivas
Srinivas

Reputation: 1059

Try this...

[self runAction:[CCFollow actionWithTarget:(your hero) worldBoundary:CGRectMake(0,0,1050,350)]];

It will follow your character (your hero).

Upvotes: 7

Sandro Meier
Sandro Meier

Reputation: 3051

Hey there, You have always to choose between the best way and the best way in your situation. A problem has multiple ways how you can solve it. So I can just tell you how I did it.

A few weeks ago I released my first game. (If you are interested check it out: Birdy!) I also used Cocos2d and had the same problem like you. The player should always be in the center of the screen. I've choosen the way to move the background. I added 2 things to the GameScene: The background and the bird. And I set the background as the bird's delegate. So as soon as the bird performs a jump for example, the background is moved in the opposite direction.

I think for my situation it was the best method. But I can't talk about CCCamera because I never did anything with that class...

I hope I could help you a little bit. But it's always a bit difficult to find the right way. So you should also think about things you will optionally add in the future. So perhaps my way does not fit your requirements...

I hope also my english is understandable...

Sandro Meier

Upvotes: 1

Related Questions