문제

I am new to Cocos2d development in ios . I want to implement a collision between my two sprites knight and coins. So for my coins sprite i tried something like below:

- (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];
    }
}
}

and put this in my update method

[self coinSidewaysRowOne];

Then I created an NSMutableArray property in my .h method:

@property (nonatomic, assign) NSMutableArray *coinArray;

As you can see, I have added this line [self.coinArray addObject:coinHorizontal]; in my coinSidewaysRowOne method

How can I write this to my array and detect a collision between the knight and the coins sprites.

I am expecting something like this:

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

Any help is strongly appreciated. Thank you.

도움이 되었습니까?

해결책

In your update method:

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

다른 팁

For detecting collision detection between sprites you can uses function CGRectIntersectsRect to check whether there is a collision between sprites.

You can refer this link for that.:http://www.raywenderlich.com/25736/how-to-make-a-simple-iphone-game-with-cocos2d-2-x-tutorial

Refer this link you can get idea of what you should do to achieve collision between sprites

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top