나는 터치를 감지 할 수있는 확장 된 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를 구현하는 것이 처음이에요. 그걸로 뭔가 할 수있을 거라는 가정하에 ...
감사합니다. 한 줄짜리 .. Kobold2D를 확인해야 겠어. – Alex