terça-feira, 24 de agosto de 2010

Basic Concepts using Cocos2D

Hi everyone! This post is about the game concepts that you can use on the cocos2d framework.

What is cocos2d? This is the information from cocos2d website:

"cocos2d for iPhone is a framework for building 2D games, demos, and other graphical/interactive applications. It is based on the cocos2d design: it uses the same concepts, but instead of using python it uses objective-c."


So, cocos2d is simply a bunch of codes that help us (a lot) with game development.



cocos2d for iPhone website: http://www.cocos2d-iphone.org/

Now let's understand how it works. I will use the same images from the cocos2d documentation for this.

Imagine that you have to make a movie. You have to choose one director, some actors and make a lot of scenes that will be together the final movie. Game basic concepts is just the same. You will create a CCDirector object that will control the changes of the scenes and each scene will have actors (called Sprites).

Check this out:


Each square is a scene. The game begins in the intro, goes to the menu and follow the signs. That scenes will be implemented by the CCScene object. So, what you have to do is create files for each, like these:

 - IntroScene.h
 - IntroScene.m
 - MenuScene.h
 - MenuScene.m
 - Level1Scene.h
 - Level1Scene.m
 - CutScene1.h
 - CutScene1.m
 - LevelScene2.h
 - LevelScene2.m
 - LosingCutScene.h
 - LosingCutScene.m
 - WinningCutScene.h
 - WinningCutScene.m
 - HighscoreScene.h
 - HighscoreScene.m

For more information about these files, check http://rauberlabs.blogspot.com/2010/07/codigo-na-cabeca.html (portuguese) or at Apple's Developer website http://developer.apple.com/ for english and other languages.

There's another important thing about scenes, the Layers. If you have used any image editor that uses layers, like Photoshop, you know what I'm talking about. Basically, you can put sprites under or above others without lose them, like MS Paint (Windows).



If you have two layers, each one with one or more sprites and layer1 is above layer2, every sprite that is in the layer 2 will be under the sprites on the layer1. It's that simple, like Steve like's to say (hehe)

Last but not least, let's talk about sprites. First of all, the bad thing, you have to use pointers on Objective-C because iOS doesn't have garbage collector. So, take care about you are doing.

This is the sprite declaration:

CCSprite *myactor;

Then, you can add an image to the sprite:

myactor = [CCSprite spriteWithFile: @"some_image.png"];

And finally, add it as a child to the scene:

[self addChild:myactor];

Note that we are coding in the implementation file of the scene (Ex: "Scene.m").

Sprites have some methods to do something with it. For example, you can set the position at the scene:

myactor.position = ccp( 50, 100 );

You can learn more about methods within the cocos2d documentation.

Ok, that's all for now! Maybe some other day I make a real tutorial of a basic game, thanks!

Nenhum comentário:

Postar um comentário