티스토리 뷰

어떠한 스프라이트 터치 이벤트 구현 관련이다.

화면 전체가 아닌 스프라이트 영역에만 터치 이벤트를 받기 위해서 <CCTargetedTouchDelegate>를 통해서 구현 가능하다


*.h 파일

@interface Sprite : CCSprite<CCTargetedTouchDelegate>{

BOOL isTargeted;

}

@property (assign)BOOL isTargeted;


*.m

@implementation Sprite


@synthesize isTargeted;


- (void)onEnter

{

[[CCTouchDispatcher sharedDispatcheraddTargetedDelegate:self priority:0 swallowsTouches:YES];

[super onEnter];

}


- (void)onExit

{

[[CCTouchDispatcher sharedDispatcherremoveDelegate:self];

[super onExit];

}


- (BOOL)containsTouchLocation:(UITouch *)touch

{

return CGRectContainsPoint(self.textureRect, [self convertTouchToNodeSpace:touch]);

}


- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

{

NSLog(@"터치");

if ( ![self containsTouchLocation:touch] ){

self.isTargeted = NO;

NSLog(@"터치 NO");

return NO;

else {

self.isTargeted = YES;

NSLog(@"터치 YES");

return YES;

}

}

터치 이벤트를 통해 얻은 터치 위치를 cocos2d의 화면 좌표로 변환 방법

CGPoint point_ = [self convertToWorldSpace:[self convertTouchToNodeSpace:touch]];


[self convertTouchToNodeSpace:touch]를 얻은 좌표는 해당 스프라이트를 기준으로 한 CGPoint가 넘어온다.
그러므로 절대좌표를 얻기 위해선 convertToWorldSpace으로 한번 더 변환시켜줘야 한다.

'Apps 제작' 카테고리의 다른 글

[iPhone 앱 개발]iPhone에 앱 설치하기  (0) 2010.07.08
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함