Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Ali.S Jun 2 '11 at 18:30
    
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... –  ArtWorkAD Jun 2 '11 at 18:45
1  
then are you asking about the class hierarchy you are going to define? –  Ali.S Jun 2 '11 at 18:59
    
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? –  ArtWorkAD Jun 2 '11 at 20:26
    
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. –  Ali.S Jun 2 '11 at 20:56
show 1 more comment

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
I don't think he wants a component system directly. He just wants to know how to organise his entities. –  The Communist Duck Jun 2 '11 at 20:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.