Tuesday, April 24, 2012

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;  

No comments:

Post a Comment