저는 Cocos2d와 Objective-C를 배우면서 터치 감지에 문제가 있습니다.터치 감지 관련 문제
HelloWorldLayer
에 몇 가지 스프라이트가 있고 draggableSprite (NSMutableArray)가 있습니다. 드래그 가능한 Sprite 중 두 개가 서로 위에 위치합니다 (아래쪽이 더 큼).
그런 다음 나는 감동 스프라이트를 흔들 일부 코드가 있습니다
- (void)selectSpriteForTouch:(CGPoint)touchLocation {
CCSprite *touchSprite = nil;
for (CCSprite *sprite in dragableSprites) {
if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {
touchSprite = sprite;
break;
}
}
if (touchSprite != selSprite) {
[selSprite stopAllActions];
[selSprite runAction:[CCRotateTo actionWithDuration:0.1 angle:0]];
CCRotateTo * rotLeft = [CCRotateBy actionWithDuration:0.1 angle:-1.0];
CCRotateTo * rotCenter = [CCRotateBy actionWithDuration:0.1 angle:0.0];
CCRotateTo * rotRight = [CCRotateBy actionWithDuration:0.1 angle:1.0];
CCSequence * rotSeq = [CCSequence actions:rotLeft, rotCenter, rotRight, rotCenter, nil];
[touchSprite runAction:[CCRepeatForever actionWithAction:rotSeq]];
selSprite = touchSprite;
}
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self selectSpriteForTouch:touchLocation];
return TRUE;
}
을하지만 상단 또는 하단 스프라이트를 터치하면, 단 아래 하나가 (이 초기화에 처음으로 추가 된) 흔들리고.
나는 코드 CGRectContainsPoint(sprite.boundingBox, touchLocation)
을 대체해야한다고 생각하지만, 어떻게해야할지 모르겠다.
감사합니다. Break 문을 제거하면 whith top sprite가 도움이됩니다. – OnOff
다행스럽게 도울 수있었습니다! –