Feel free to fix/add documentation to the wiki

Hello World

cocos2d v0.8-beta 또는 그 이후 버전에 적용됩니다.

hello_world.jpg

이 예제는 단순히 스크린에 라벨을 렌더링하는 기본 단계를 보여줄 뿐입니다. 초기화를 수행하는 다른 옵션들은 설명을 단순화하기 위해 이 예제에서는 설명하지 않을 것입니다. 이 예제를 이해하신다면 여러분은 Hello Action 예제를 보실 수 있을 겁니다.

구현 파일

File: HelloWorld.m

Import headers

c / c++에서와 같이 헤더를 임포트합니다.

// UIWindow, NSAutoReleasePool, 다른 객체를 사용하기 때문에
#import <UIKit/UIKit.h>
 
// 인터페이스 헤더
#import "HelloWorld.h"

레이어

HelloWorld 구현. 마법이 일어나는 곳입니다.

// HelloWorld implementation
@implementation HelloWorld
 
// 인스턴스 초기화 "init" 
-(id) init
{
	// 항상 "super" init 호출하셔야 합니다.
	// 애플은 "super"의 리턴값을 "self"에 할당하는 것을 권장합니다.
	if( (self=[super init] )) {
 
		// 라벨 생성 및 초기화
		CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
 
		// 디렉터에 윈도우 사이즈를 알아본다.
		CGSize size = [[CCDirector sharedDirector] winSize];
 
		// 스크린의 한 가운데에 라벨을 위치시킨다.
		// 'ccp'는 한 점을 생성하는 헬퍼 매크로 입니다. 'CoCos Point'를 뜻하죠.
		label.position =  ccp( size.width /2 , size.height/2 );
 
		// 본 레이어의 자손으로 라벨 추가
		[self addChild: label];
	}
	return self;
}
 
// 할당된 객체들을 릴리즈할 때 "dealloc" 호출
- (void) dealloc
{
	// 이번 케이스에는 릴리즈할 필요가 전혀 없는 특별한 예제이므로 dealloc 할 것이 없습니다. 
	// cocos2d는 자동으로 자손을 릴리즈합니다.(Label)
 
	// "super dealloc" 호출을 잊으시면 안됩니다.
	[super dealloc];
}
@end

어플리케이션 델리게이트

어플리케이션 델리게이트의 구현입니다. 아마 여러분의 모든 게임은 이 어플리케이션 델리게이트와 비슷할겁니다. 아래 코드를 이해하지 못하신다면 당장은 별로 중요하지 않을 겁니다.

@implementation AppController
 
// window 는 속성입니다. @synthesize 는 조상 메소드를 생성할 겁니다.
@synthesize window;
 
// 어플리케이션 엔트리 포인트
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
	// UIWindow 생성/초기화
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
	// cocos2d를 window에 붙이고
	[[CCDirector sharedDirector] attachInWindow:window];
 
	// 레이어를 생성하기 전에 수평 모드로 셋팅
	[[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft];
 
	// 윈도우를 visible로 만들기
	[window makeKeyAndVisible];
 
	// 아무것도 없는 Scene 초기화/생성
	CCScene *scene = [CCScene node];
 
	// HelloWorld 레이어 초기화/생성
	CCLayer *layer = [HelloWorld node];
	// HelloWorld 레이어를 메인 scene의 자손으로 붙임
	[scene addChild:layer];
 
	// 실행!
	[[CCDirector sharedDirector] runWithScene: scene];
}
 
 
- (void) dealloc
{
	[window release];
	[super dealloc];
}
 
@end

메인 엔트리 포인트

objective-c도 c / c++ 처럼 메인 엔트리 포인트가 여전히 main함수입니다.

 
int main(int argc, char *argv[]) {
	// 보통 아래처럼 그냥 두는게 안전하지요. 이름만 "AppController"로 하는 것 빼고요.
	NSAutoreleasePool *pool = [NSAutoreleasePool new];
	UIApplicationMain(argc, argv, nil, @"AppController");
	[pool release];
	return 0;
}

헤더 파일

그리고 헤더파일:

File: HelloWorld.h

 
// 이 파일을 임포트 할 때 여러분은 모든 cocos2d 클래스를 임포트하게 됩니다.
#import "cocos2d.h"
 
// 어플리케이션 델리게이트 클래스
@interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate, UIApplicationDelegate>
{
	// main UIWindow
	// OpenGL 뷰는 UIWindow의 뷰가 됩니다.
	UIWindow *window;
}
 
// 메인 UIWindow를 속성으로 만드세요.
@property (nonatomic, retain) UIWindow *window;
@end
 
// HelloWorld Layer
@interface HelloWorld : Layer
{
}
@end