I am using sprite sheet with 4 sprites. and using below code to add sprite sheet. The sprites are baseball bats at different angles and on animation these are moving like pins of clock but in anticlockwise from around 80 degree to 20 degree.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"baseball.plist"];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"baseball.plist"];
[self addChild:spriteSheet];
background = [CCSprite spriteWithSpriteFrameName:@"bat1.png"];
background.position = ccp(220, 185);
background.tag = 10;
[self addChild:background];
The size of bat1.png is {113, 135}
The size of bat2.png is {140, 134}
The size of bat3.png is {158, 125}
The size of bat4.png is {172, 110}
below is the code for animation
NSMutableArray *walkAnimFrames = [NSMutableArray array];
NSMutableArray *walkAnimFrames1 = [NSMutableArray array];
if(direction == 1){
for(int i = 1; i < 5; i++) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"bat%d.png", i]]];
}
}
else{
for(int i = 4; i > 0; i--) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"bat%d.png", i]]];
}
}
and to check the collision below is the code
for (CCSprite *monster in _monsters) {
if (CGRectIntersectsRect(background.boundingBox, monster.boundingBox)) {
(monster.contentSize.height/2));
if (background.position.x -5 > (monster.position.x + monster.contentSize.width/2)) {
isCollision = 1;
[monstersToDelete addObject:monster];
}
}
}
but it detects the collision with bat1.png always. but is should detect the collision with the current sprite in the sprite frame.
by this code even when sprite is far from the bat2.png the collision occurs because it use the boundingbox of bat1 to check the intersection always. but i want it to check the the intersection with rect of current spriteframe..
can any one please know how to do thie?