Currently I am working with cocos2d for the iPhone. I want to make my game structured and consequential. How to manage all the game components?

What do I mean with game components? A game component is e.g. the terrain or any other object like a character that is drawn on the scene. These components have attributes and duties like "let's clean up that place" or something similar.

So actually I want to wrap a component with all its attributes to make the code reusable. E.g. think of terrain. There may be a terrain in the front. Behind this terrain there is another terrain, e.g. hills, it's still terrain but just with maybe another color.

How do I accomplish that in cocos2d? In my scene there is a draw method where I normally would draw the terrain. But if I would put everything that is drawn in that method it would get confusing.

It would be cool to create an object of a terrain class and add this to the scene which would draw the terrain automatically. How do I do that? What would be the parent class of the terrain class?

  • \$\begingroup\$ did you try reading some basic manuals of cocos2d before asking this question? just studing cocos2d tests (or even helloworld sample) would give you the idea how to do all these things in cocos2d. \$\endgroup\$
    – Ali1S232
    Commented Jun 2, 2011 at 18:30
  • \$\begingroup\$ of course I tried ;) I already finished a game but without that technique I mention in my question. I just want that somebody who has the knowledge points out the basics... \$\endgroup\$ Commented Jun 2, 2011 at 18:45
  • 1
    \$\begingroup\$ then are you asking about the class hierarchy you are going to define? \$\endgroup\$
    – Ali1S232
    Commented Jun 2, 2011 at 18:59
  • \$\begingroup\$ yes and how to actually draw an object, because I know their is the draw method in the main layer but how would I draw a terrain object? Is it possible to have a draw method in the terrain class? \$\endgroup\$ Commented Jun 2, 2011 at 20:26
  • \$\begingroup\$ any object that inheriet CCNode can be placed in scene-graph, you can start by reading how CCSprite is implemented. I think it's easy enough to undestand and provide you with the whole idea of what you have to do to create some Cocos2d object. \$\endgroup\$
    – Ali1S232
    Commented Jun 2, 2011 at 20:56

1 Answer 1


You probably want a component-entity system. It's a lot like mixins (versus interface and inheritance).

From Evolve Your Hierarchy:

Up until fairly recent years, game programmers have consistently used a deep class hierarchy to represent game entities. The tide is beginning to shift from this use of deep hierarchies to a variety of methods that compose a game entity object as an aggregation of components. This article explains what this means, and explores some of the benefits and practical considerations of such an approach. I will describe my personal experience in implementing this system on a large code base, including how to sell the idea to other programmers and management.

One JavaScript library which implements this is Crafty. Entities, like players, trees, and bullets, are made up of components, like 2D, collidable, spritemap, text, etc.

Maybe this helps.

  • \$\begingroup\$ I don't think he wants a component system directly. He just wants to know how to organise his entities. \$\endgroup\$ Commented Jun 2, 2011 at 20:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .