2012-01-15 3 views
0

저는 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)을 대체해야한다고 생각하지만, 어떻게해야할지 모르겠다.

나는 포럼에 며칠 밤을 보냈다하지만, 난 여전히

답변

0

것이 더 이런 식으로 뭔가를 시도 (이 ccTouchBeganccTouchesBegan하지 통지) ... 상단 스프라이트 흔들림을 만드는 방법을 이해할 수 없다 :

- (void)startShakingSprite:(CCSprite*)sprite { 

    [sprite stopAllActions]; 
    [sprite 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]; 
    [sprite runAction:[CCRepeatForever actionWithAction:rotSeq]]; 
} 

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent*)event { 
    //get the touch 
    UITouch* touch = [touches anyObject]; 
    //get its location 
    CGPoint location = [touch locationInView:[touch view]]; 
    //convert location to cocos2d coordinates 
    CGPoint point = CGPointMake(location.x, 480 - location.y); 

    //now we go through your array of objects and see which contains the touch 
    for(id sprite in draggableSprites) { 
     if(CGRectContainsPoint(sprite.boundingBox, point)) { 
      [self startShakingSprite:sprite]; 
     } 
    } 
} 

for 루프가 selectSpriteForTouch에 설정된 방법은 break 문 때문에 draggableSprites의 첫 번째 개체 만 선택하게됩니다. 이 새로운 코드를 사용하면 바운딩 박스에 터치가 포함 된 각 스프라이트가 애니메이션으로 표시됩니다. 코드를 Chrome에 직접 입력했기 때문에 코드가 완벽하지 않을 수 있지만 아이디어를 얻을 수 있습니다.

주목해야 할 또 다른 중요한 점은 다음과 같이 두 개의 서로 다른 스프라이트에 동일한 작업을 실행할 수 있다는 것입니다 :

id action = [CCScaleTo actionWithDuration:1 scale:1.0]; 
[someSprite runAction:action]; 
[someOtherSprite runAction:action]; 
[oneMoreSprite runAction:action]; 

이 코드는 의도 한대로 작동하지 않습니다; 마지막 스프라이트 (oneMoreSprite) 만 애니메이션 처리됩니다. 원래 스프라이트에서 제거하지 않고 다른 스프라이트 (someOtherSprite, oneMoreSprite)에 의해 운영 될 수 있도록 우리는 행동의 복사본을 만들

id action = [CCScaleTo actionWithDuration:1 scale:1.0]; 
[someSprite runAction:action]; 
[someOtherSprite runAction:[[action copy] autorelease]]; 
[oneMoreSprite runAction:[[action copy] autorelease]]; 

: 다시 사용하기 위해 여러 스프라이트와 CC 애니메이션을,이 코드를 사용 위해 , someSprite. 그런 다음 우리는 메모리 누출이 없도록 자동으로 풀어냅니다.

+0

감사합니다. Break 문을 제거하면 whith top sprite가 도움이됩니다. – OnOff

+0

다행스럽게 도울 수있었습니다! –