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);  

No comments:

Post a Comment