Feel free to fix/add documentation to the wiki

Hello Actions

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

hello_actions.jpg

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

구현 파일

계속하기 전에 cocos2d framework을 인클루드 하는 방법은 다음을 참조하세요. http://monoclestudios.com/cocos2d_whitepaper.html (조만간 간단히 인클루드 할 수 있는 방법이 알려질겁니다.)

File: HelloActions.m

objective-c에서 구현체는 .m 파일에 있고, 인터페이스는 .h 파일에 정의됩니다.

Import Headers

c / c++ / java / 기타와 같이 헤더를 임포트합니다.

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

레이어

The magic occurs in the class. 이번에는 LabelSprite 두 객체를 포함하는 Layer를 생성할 겁니다.

  • Label 은 1 액션을 실행합니다. : scale.
  • Sprite 는 복합적인 액션을 실행합니다. : jump, rotate, spawn, sequence, repeat.
// HelloAction 구현
@implementation HelloActions
 
// 인스턴스 초기화 "init" 
-(id) init
{
	// 항상 'super' init 호출하셔야 합니다.
	// 애플은 'super'의 리턴값을 'self'에 할당하는 것을 권장합니다.
	if( (self=[super init] )) {
 
		//
		// Label
		//
 
		// 라벨 생성 및 초기화
		CCLabel* label = [CCLabel labelWithString:@"Hello Actions" 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];
 
		// objective-c 에서는 정적 언어도 동적 언어도 될 수 있습니다.
		// "id" 는 "타입에 무관한 객체입니다." 라는 뜻의 예약어입니다.
		// 라벨을 3초간 2.5x 확대
		id action = [CCScaleBy actionWithDuration:3.0f scale:2.5f];
 
		// "label"에 액션을 실행하도록 한다.
		// 액션은 스크린에 나타나는(이전이 아닌) 본 레이어에 한번만 실행됩니다.
		[label runAction:action];
 
		//
		// Sprite
		//
 
		CCSprite *sprite = [CCSprite spriteWithFile:@"grossini.png"];
		sprite.position = ccp( 0, 50);
 
		// z는 z-order 입니다. 더 큰 값이 낮은 값보다 위에 표시됨을 뜻합니다.
		// 디폴트 z값은 0입니다.
		// 그래서 스프라이트는 라벨의 가장 위에 위치하게 될 겁니다.
		[self addChild:sprite z:1];
 
		// RotateBy 액션 생성
		// "By"는 연관(relative)을 뜻합니다. "To"는 고정(absolute) 위치를 뜻합니다.
		id rotateAction = [CCRotateBy actionWithDuration:4 angle:180*4];
 
		// JumpBy 액션 생성
		id jumpAction = [CCJumpBy actionWithDuration:4 position:ccp(size.width,0) height:100 jumps:4];
 
		// spawn은 한번에 2개 이상의 액션을 실행하는 액션입니다.
		id fordward = [CCSpawn actions:rotateAction, jumpAction, nil];
 
		// 거의 모든 액션이 "reverse" 메소드를 지원합니다.
		// 이는 반대 액션이 되는 새 액션을 생성할 겁니다.
		id backwards = [fordward reverse];
 
		// Sequence는 순차적으로 실행되는 액션입니다.
		id sequence = [CCSequence actions: fordward, backwards, nil];
 
		// 마지막으로 여러분은 하고 싶은 만큼 여러번 액션을 반복할 수 있습니다.
		// "RepeatForEver"액션으로 여러분은 무한액션을 사용할 수 있습니다.
		id repeat = [CCRepeat actionWithAction:sequence times:2];
 
		// 스프라이트에 액션을 실행
		// 액션은 스크린에 나타나는(이전이 아닌) 본 레이어에 한번만 실행됩니다.
		[sprite runAction:repeat];
	}
	return self;
}
 
// 할당된 객체들을 릴리즈할 때 "dealloc" 호출
- (void) dealloc
{
	// 이번 케이스에는 릴리즈할 필요가 전혀 없는 특별한 예제이므로 dealloc 할 것이 없습니다.
	// ocos2d는 자동으로 자손을 릴리즈합니다.(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];
 
	// HelloAction 레이어 초기화/생성
	Layer *layer = [HelloActions node];
	// HelloAction 레이어를 메인 scene의 자손으로 붙임
	[scene addChild:layer];
 
	// 실행!
	[[Director 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: HelloActions.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
 
// HelloActions Layer
@interface HelloActions : Layer
{
}
@end