Showing posts with label cocos2d. Show all posts
Showing posts with label cocos2d. Show all posts

Tuesday, April 24, 2012

cocos2d Layer 에 rubber band interaction 넣기

1. 클래스 구현
인터랙션을 위한 클래스를 구현한다. ccTouchEnded 에서 레이어가 원위치로 돌아오도록 runAction 으로 CCEaseIn 를 실행시키면 됨. 다음과 같이 세가지 멤버가 필요하다.
 @interface MyRubberPan : NSObject {  
      NSInteger ActionTagRubberBandMovement;  
      CGPoint gameLayerPosition;  
      CGPoint lastTouchLocation;  
} 

2. MyLayer.h
 @class MyRubberPan;  
 @interface MyLayer : CCLayer {  
      ...  
      MyRubberPan* myRubberPan;  
 }  

3. MyLayer.m
 #import "MyRubberPan.h"  
 @implementation MyLayer  
 -(id) init  
 {  
      if( (self=[super init])) {  
           ...  
           myRubberPan = [[MyRubberPan alloc] initWithRubberActionTag:50 onLayer:self];  
           ...  
      }  
      return self;  
 }  
 -(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event   
 {   
      [myRubberPan ccTouchBegan:touch withEvent:event onLayer:self];  
      // Always swallow touches, MyLayer is the last layer to receive touches.   
      return YES;   
 }   
 -(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event   
 {  
      CGPoint moveTo = [myRubberPan ccTouchMoved:touch withEvent:event onLayer:self];  
      self.position = ccpAdd(self.position, moveTo);  
 }   
 -(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event   
 {  
      [myRubberPan ccTouchEnded:touch withEvent:event onLayer:self];  
 }  

ccTouchesMoved 에 넣는 방법도 있다.
  CGPoint moveTo = [myRubberPan ccTouchesMoved:touches withEvent:event onLayer:self];  
  self.position = ccpAdd(self.position, moveTo);  

cocos2d Layer 에 sprite animation 넣기

Sheet animation 을 다음과 같은 파일로 만들어 Resources 로 넣었다고 하자.
blob_idle.png
blob_idle.plist
MyLayer 에서 이 스프라이트 애니메이션을 보여주려면 다음과 같이 한다.

1. 스프라이트 애니메이션용 클래스 구현
MyCharWithAnimationLoop 라는 클래스를 구현한다.
 CCSprite *_character;  
 CCAction *_action;  
라는 두개의 멤버를 갖고, initFromFile() 에서 스프라이트를 씬그래프에 넣고 애니메이션 액션을 시작해주는 클래스.

2. MyLayer.h
 @class MyCharWithAnimationLoop;  
 @interface MyLayer : CCLayer {  
      MyCharWithAnimationLoop* myChar;  
 }  

3. MyLayer.m
 #import "MyCharWithAnimationLoop.h"  
 -(id) init  
 {  
      if( (self=[super init])) {  
           myChar = [[MyCharWithAnimationLoop alloc] initFromFile:@"blob_idle" numOfFrames:24 fps:30.0 onLayer:self];  
           ...  
      }  
      return self;  
 }  

cocos2d Layer 에 터치 입력받는 코드 넣기

MyLayer 라는 레이어 클래스에 touch input handler 를 추가한다고 하자.
그러면 MyLayer.m 에 다음 코드를 추가하면 된다.

방법 1
 -(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
 {  
 }  
 -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
 {
 }  
 -(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
 {
 }  

방법 2
 -(void) registerWithTouchDispatcher  
 {  
      [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];  
 }  
 -(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event  
 {  
      // Always swallow touches, MyLayer is the last layer to receive touches.  
      return YES;  
 }  
 -(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event  
 {  
 }  
 -(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event  
 {  
 }  

MyLayer.m 의 -(id) init 에서 touch 가 enable 되어 있어야 한다.
 self.isTouchEnabled = YES;  

cocos2d Layer 만들기

MyLayer 라는 레이어 클래스를 만든다고 하자.

1. New File
(Template = CCNode class, Subclass of CCNode, Class name = MyLayer)

2. 기본 골격
MyLayer.m 에 기본 함수 넣음
 @implementation MyLayer  
 +(CCScene *) scene  
 {  
      CCScene* scene = [CCScene node];  
      MyLayer* layer = [MyLayer node];  
      [scene addChild:layer];  
      return scene;  
 }  
 -(id) init  
 {  
      if( (self=[super init])) {      
           self.isTouchEnabled = YES;  
           [self scheduleUpdate];  
      }  
      return self;  
 }  
 -(void) update:(ccTime)delta  
 {  
 }  
 - (void) dealloc  
 {  
      [super dealloc];    
 }  
 @end  

3. 실행
AppDelegate.m 에서 applicationDidFinishLaunching 의 맨 뒤에 다음 넣음
 [[CCDirector sharedDirector] runWithScene: [MyLayer scene]];  

Apple Mach-O Linker (Id) Error "[function_name]", referenced from:

질문 : XCode에서 컴파일 시 Apple Mach-O Linker (Id) Error "[function_name]", referenced from: 의 원인?
답 : 필요한 것을 #import 안했을 때 종종 발생하는 에러

Monday, April 23, 2012

cocos2d 사용하는 프로젝트에서 클래스 간편히 만드는 법

  1. Command + N (또는 왼쪽 Navigator에 오른클릭 > New File..)
  2. Choose a template for your new file... 에서 CCNode class 선택 (디폴트 그대로)
  3. Subclass of CCNode (디폴트 그대로)
  4. Save As.. 에서 원하는 클래스 이름 넣음
  5. @interface MyClass : CCNode {

    @interface MyClass : NSObject {
    로 고침

cocos2d 에서 ccTouchBegan 으로 들어가지 않을 때 해결법

질문 : ccTouchesBegan 은 되는데 ccTouchBegan 으로는 들어가지 않을 때 해결법?
답 : 메인 layer의 멤버 함수로 다음을 넣어준다.
-(void) registerWithTouchDispatcher
{
      [[CCTouchDispatcher  sharedDispatcher]  addTargetedDelegate:self  priority:0  swallowsTouches:YES];
}

ccTouchesBegan 도 안된다면 메인 layer의 -(id) init 함수 내에 self.isTouchEnabled = YES; 를 넣어주는 것을 잊었을 것임

Sunday, April 22, 2012

Cocos2d의 coordinate system

Cocos2d 클래스 이름 바뀐 것들

CCSlideInBTransition → CCTransitionSlideInB
CCLabel → CCLabelTTF
키워드 : 에러, class, error

Error "Sending 'ccColor4B' (aka 'struct_ccColor4B') to parameter of incompatible type 'CIColor *'"

질문 : 해결법?
답 : 기존 코드를 다음과 같이 고쳐준다.
// 에러 나서 고친 것
//+ (id) layerWithColor:(ccColor4B)color
//{
// return [[[self alloc] initWithColor:color] autorelease];
//}
+ (id) layerWithColor:(ccColor4B)color
{
      return [[(CCColorLayer*)[self alloc] initWithColor:color] autorelease];
}
출처 : Error "Sending 'ccColor4B' (aka 'struct_ccColor4B') to parameter of incompatible type 'CIColor *'"
키워드 : cocos2d

Sunday, April 15, 2012

옛 기기와의 호환을 위한 텍스처 해상도

CAUTION: Unless you develop your game exclusively for iPhone 3GS, iPad, or iPhone 4 and
future devices, you should not use a canvas width or height of 2048. Older devices only support
texture dimensions up to a maximum of 1024×1024 pixels.
출처 : Learn iPhone and iPad Cocos2D Game Development 132 페이지
키워드 : resolution, texture atlas

Saturday, April 14, 2012

cocos2d에서 iPhone4 이상(retina display)을 위해 해줘야 할 것

cocos2d에서 retina display 쓸 수 있게 하는 법
디폴트로는 오리지널 아이폰 해상도인 320 x 480 으로 동작한다.
iPhone 4의 640 x 960 에서 그에 걸맞는 해상도로 동작하게 하려면 다음과 같이 해줘야 한다.

1. 레티나 디스플레이를 enable 해준다
우선 AppDelegate.m 에서 다음 부분을 uncomment 해준다.
// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
// if( ! [director enableRetinaDisplay:YES] )
// CCLOG(@"Retina Display Not supported");

2. 기존의 이미지의 hd 버전을 만들어 준다
예를 들어 게임 상에서 hero.png 라는 100 x 100 이미지를 사용하고 있다면
hero-hd.png 라는 200 x 200 이미지를 만들어 넣어준다.

출처 :
HowTo: Cocos2d – Enable Retina Display. Part One
HowTo:Cocos2d – Enable Retina Display. Part Two – The Graphics