2010-07-04 4 views
1

먼저 스프라이트가 포함 된 레이어를 더 크게 확장했습니다. 이제 스프라이트에 터치 감각이 필요합니다. 나는 다음과 같이 시도하지만 캔트 참고확대/축소 상태에서 터치 액션이 스프라이트에 닿았는지 여부를 어떻게 확인할 수 있습니까?

CGRect tRect= [[aSprite displayedFrame] rect]; 
    if(CGRectContainsPoint(tRect, touchedPosition)) 
{ 
    NSLog(@"touched:>> touch at (%f,%f)",touchedPosition.x,touchedPosition.y); 
    // Do something, maybe return kEventHandled; 
} 
else{ 
    NSLog(@"NOT touched: touch at (%f,%f)",touchedPosition.x,touchedPosition.y); 
} 

을 goal-에 도달 : 나는

+0

방금 ​​iPhone 프로그래밍을 시작했습니다. – Sadat

답변

0

, 여기에 내가 전에 지정한으로 '자기'스프라이트 홀더 층 인 코드

CGPoint location = [touch locationInView: [touch view]]; 
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location]; 

CGPoint tapPosition = [self convertToNodeSpace:convertedLocation]; 

입니다. 이 레이어는 터치 이벤트를 듣고 있습니다.

1

가 먼저 올바르게 UITouch에서 위치를 얻을 수 있는지 확인해야합니다적인 Cocos2D 프레임 워크를 사용하고 있습니다.

CGPoint location = [touch locationInView:[touch view]]; 
location = [[CCDirector sharedDirector] convertToGL:location]; 

둘째, 스프라이트의 경계 상자에 대해 터치를 테스트해야합니다.

if (CGRectContainsPoint([sprite boundingBox], location)) { 
    // The sprite is being touched. 
} 
+0

@Mitchell, 이미 해 보았습니다. – Sadat

+0

@Mitchell, 답장을 보내 주셔서 감사합니다. – Sadat

0

Frank Mitchell이 ​​맞습니다. 또 다른 접근법은 코코스가 당신을 위해 일할 수 있도록 듣기 코드를 스프라이트 자체에 추가하는 것입니다. 실제로 만진 경우에만 스프라이트 ccTouchesBegan 이벤트를 보냅니다.

내가 솔루션을 : 발견 마지막으로
+0

감사합니다. @cc,이 기술을 시도합니다 ... – Sadat