0
votes

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?

1

1 Answers

0
votes

I gone through your code and got the reason why it is detecting collision with bat1.png. For collision detection, you are checking the collision between all the element in _monsters and the background.

And you have set the position of background as follows:

 background.position = ccp(220, 185);

Now see, its y co-ordinate is 185 which is grater than all other sprites position. This is the reason why your ball always gets collision detection with bat1.png