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