2013-07-18 5 views
-2

ios에서 Cocos2d 개발을 처음 사용했습니다. 내 두 스프 라이트 knightcoins 사이의 충돌을 구현하고 싶습니다.CCSprite 간의 충돌 감지

@property (nonatomic, assign) NSMutableArray *coinArray; 

:

[self coinSidewaysRowOne]; 

가 그럼 난 내 .h 방법에 NSMutableArray 속성을 생성

- (void)coinSidewaysRowOne { 

self.visible = YES; 

if (coinSide1 == FALSE) 
{ 
    coinSide1 = TRUE; 
    NSLog(@"coinSide1 = TRUE"); 
    NSInteger originalX = 150; 
    for(int i = 0; i < 8; i++) 
    { 
     CCSprite *coinHorizontal = [CCSprite spriteWithFile:@"bubble.png"]; 
     coinHorizontal.position = ccp(originalX, 150); 
     originalX += 20; 
     [self addChild:coinHorizontal]; 
     [self.coinArray addObject:coinHorizontal]; 
    } 
} 
} 

내 업데이트 방법에 넣고 : 그래서 내 coins 스프라이트 나는 다음과 같은 시도 보시다시피,이 라인을 [self.coinArray addObject:coinHorizontal];에 내 coinSidewaysRowOne 방법을

어떻게 이것을 내 배열에 쓰고 기사와 동전 스프라이트 사이의 충돌을 감지 할 수 있습니까?

나는 이런 식으로 뭔가를 기대하고있다 :

(void)coinGotCollected { 
    coin.visible = FALSE; 
    coin.position = ccp(-MAX_INT, 0); 
    [Store addInAppCurrency:coinValue]; 
} 

어떤 도움이 강하게 감사합니다. 고맙습니다. 당신의 update 방법에서

답변

4

:

for (CCSprite *coin in self.coinArray) 
{ 
    if (CGRectIntersectsRect(knight.boundingBox, coin.boundingBox)) 
    { 
     [self processCollision];//do what you need when a collision is detected 
     break; 
    } 
} 
1

당신이 할 수있는 스프라이트 사이에 충돌 감지를 검출 스프라이트 사이에 충돌이 있는지 여부를 확인하는 기능 CGRectIntersectsRect를 사용합니다.

당신은 :이 링크를 참조 할 수 있습니다 http://www.raywenderlich.com/25736/how-to-make-a-simple-iphone-game-with-cocos2d-2-x-tutorial

당신이 스프라이트

의 충돌을 달성하기 위해 무엇을해야하는지의 아이디어를 얻을 수있는이 링크를 참조하십시오