2011-11-15 2 views
0

나는 터치를 감지 할 수있는 확장 된 CCSprite 클래스를 만들려고합니다. 나는 약간 연구를하고이 공개 토론 스레드 http://www.cocos2d-iphone.org/forum/topic/3196 (마지막 포스트)에있는 Anny에 의해 창조 된 http://anny.fm/c/iphone/cocos2d/TouchableSprite/에보기를 발견했다.Cocos2d의 터치 가능한 스프레드

나는 내 수업과 같이 설정을 가지고이 사용 : 같은 경우 감지으로,

@implementation PianoKey 

-(id)initWithKey:(int)key { 
if((self = [super initWithFile:[NSString stringWithFormat:@"key_%i.png",key]])) { 

} 
return self; 
} 


-(BOOL) tsTouchBegan:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog(@"tsTouchBegan"); 
return YES; 
} 
-(void) tsTouchMoved:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog(@"tsTouchMoved"); 

} 
-(void) tsTouchEnded:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog(@"tsTouchEnded"); 

} 
-(void) tsTouchCancelled:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog(@"tsTouchCancelled"); 
} 

// 
// Utilities. Don't override these unless you really need to. 
// 

-(CGRect) rect { 
CGSize s = [self.texture contentSize]; 
return CGRectMake(-s.width/2, -s.height/2, s.width, s.height); 
} 

-(BOOL) didTouch: (UITouch*)touch { 
return CGRectContainsPoint([self rect], [self convertTouchToNodeSpaceAR: touch]); 
} 

// 
// The actual touch listener functions. Don't override these either. 
// 

-(BOOL) ccTouchBegan:(UITouch*)touch withEvent: (UIEvent*)event { 
    NSLog(@"attempting touch."); 
    if([self didTouch: touch]) { 
     return [self tsTouchBegan:touch withEvent: event]; 
    } 
return NO; 
} 
-(void) ccTouchMoved:  (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch])  [self tsTouchMoved:  touch withEvent: event]; } 
-(void) ccTouchEnded:  (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch])  [self tsTouchEnded:  touch withEvent: event]; } 
-(void) ccTouchCancelled: (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch])  [self tsTouchCancelled: touch withEvent: event]; } 

@end 

내가 위의 방법을 이해 ...

헤더

#import <Foundation/Foundation.h> 
#import "cocos2d.h" 

@interface PianoKey : CCSprite <CCTargetedTouchDelegate> { 

} 

-(BOOL) tsTouchBegan:  (UITouch*)touch withEvent: (UIEvent*)event; 
-(void) tsTouchMoved:  (UITouch*)touch withEvent: (UIEvent*)event; 
-(void) tsTouchEnded:  (UITouch*)touch withEvent: (UIEvent*)event; 
-(void) tsTouchCancelled: (UITouch*)touch withEvent: (UIEvent*)event; 

@end 

및 구현하여 터치가 스프라이트의 범위 내에 있었지만 키를 클릭 할 때 응답이 표시되지 않는 이유는 무엇입니까? 나는 CCTargetdTouchDelegate를 구현하는 것이 처음이에요. 그걸로 뭔가 할 수있을 거라는 가정하에 ...

답변

0

타겟 접촉 위임자가 필요하지 않습니다. NSSet을 개별 ccTouchXXX 메시지로 분할하기 만하면됩니다. 귀하의 구현에는 단순히 CCTargetedTouchHandler의 등록 및 등록 취소가 없습니다. 이 모든 노드 유형과 작동뿐만 아니라 CCLayer 있도록 이것은 일반적으로, onEnter로 수행됩니다

-(void) onEnter 
{ 
    [super onEnter]; 
    [[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES]; 
} 

-(void) dealloc 
{ 
    [[TouchDispatcher sharedDispatcher] removeDelegate:self]; 
    [super dealloc]; 
} 

, BTW Kobold2D 이미 터치가 노드에서 (스프라이트, 레이블 인 경우 테스트 할 수있는 확장 CCNode 클래스가 , 등) :

[node containsTouch:uiTouch]; 

이 작업을 모두 수행하지 않아도된다고 말하면됩니다. ;)

+1

감사합니다. 한 줄짜리 .. Kobold2D를 확인해야 겠어. – Alex