0

Hello I am making a cocos2d side scroller. I am trying to test for collisions between two sprites. I checked and the rects I am making for the sprites are what they are supposed to be, but it says that the two rects intersect all of the time whether or not they actually do. Here is the code:

-(void)checkForRedEnemyCollisions{
    CGRect playerRect = CGRectMake(player.position.x -     (player.playerSprite.contentSize.width/2),
                                   player.position.y -     (player.playerSprite.contentSize.height/2),
                                   player.playerSprite.contentSize.width,
                                   player.playerSprite.contentSize.height);

    CGRect redEnemyRect = CGRectMake(redEnemy.position.x -     (redEnemy.bulletSprite.contentSize.width / 2) ,
                                     redEnemy.position.y -     (redEnemy.bulletSprite.contentSize.height /2 ),
                                     redEnemy.bulletSprite.contentSize.width,
                                     redEnemy.bulletSprite.contentSize.height);
    if (CGRectIntersectsRect(playerRect, redEnemyRect)) {
        CCLOG(@"collision");
    }
}

Here is more code:

-(id)init{
if((self = [super init])){
    CGSize size = [[CCDirector sharedDirector]winSize];
    screenWidth = size.width;
    screenHeight = size.height;

    gravity = 2;

    playerSprite = [CCSprite spriteWithFile:@"thefinalcharacter.png"];
    playerSprite.scale = 1.5;
    playerSprite.position = ccp(screenWidth/3.4, screenHeight/2);
    [self addChild:playerSprite z:-3];

    [self schedule: @selector(flight:)interval:1.0f/7.0f];

    }
return self;
}

-(void)flight:(ccTime)delta{
flightCounter ++;

if (flightCounter % 2){
    [playerSprite setTexture:[[CCSprite spriteWithFile:@"thefinalcharacter.png"]texture]];
}else{
    [playerSprite setTexture:[[CCSprite    spriteWithFile:@"thefinalcharacter2.png"]texture]];
}
[self schedule:@selector(updatePosition:)interval:1.0f/30.0f];
}

-(void)updatePosition:(ccTime)delta{
if(playerSprite.position.y < 35){
    gravity = 0;
}else if(playerSprite.position.y > screenHeight - 150) {
        playerSprite.position = ccp(playerSprite.position.x, playerSprite.position.y -    100);
}else{
    gravity = 2;
}

playerSprite.position = ccp(playerSprite.position.x, playerSprite.position.y - gravity);
}
1
  • 3
    you should compare player.boundingBox and redEnemy.boundingBox
    – CodeSmile
    Aug 31, 2013 at 18:01

1 Answer 1

0

make sure that you are creating the bounding rectangles in world coordinates.

try to convert the points to world coordinates like the following;

CGPoint playerWorldPosition = [player ConvertToWorldSpace:player.position];
CGRect playerRect = CGRectMake(playerWorldPosition.x -  (player.playerSprite.contentSize.width/2),
                               playerWorldPosition.y -  (player.playerSprite.contentSize.height/2),
                               player.playerSprite.contentSize.width,
                               player.playerSprite.contentSize.height);

do the same for redEnemy. this will make sure you are comparing them in world space but not in another space down the hierarchy of layers.

if you using anchor point instead of the default (0.0,0.0) on your player or redEnemies. use the ConvertToWorldSpaceAR function instead of the ConvertToWorldSpace.

5
  • hello @Yucel Karamanli, it is still saying there is a collision all the time whether or not there actually is a collision.
    – PoKoBros
    Sep 2, 2013 at 18:17
  • @PoKoBros if this doesn't work then I can not tell where the problem lies with the given code info. from the code you have I dont think player is derived from a CClayer. I came to this conclusion because you have a playerSprite which is a member of player. This makes me think that playerSprite is a CCSprite which is a child of player layer. same for redEnemy. if its so. how are you moving your player sprite on the scene? are you moving the player position? or are you moving the playersprite position in your game update call? Can you add player class implementation code on your question.
    – Yucel_K
    Sep 2, 2013 at 18:43
  • Sure I changed the code. The new code is at the bottom. The original code I posted is in a GameLoop class. The new code is in a different class called Player. I made my redEnemy the same way.
    – PoKoBros
    Sep 2, 2013 at 18:48
  • I just checked your code. you are moving the position of the sprite and adding it as a child to player.playerSprite.position = ccp(screenWidth/3.4, screenHeight/2); [self addChild:playerSprite z:-3]; this movement is relative to player's position. think about it for a minute. player.position is not set at all during initialisation. what you doing is moving the sprite that is attached to player a reletive distance. When you creating the CGRect for coll, if you use the CGPoint playerSpriteWorldPosition = [playersprite ConvertToWorldSpace:playersprite.position]; you should ok.
    – Yucel_K
    Sep 2, 2013 at 18:58
  • @PoKoBros make sure redEnemy also changed to CGPoint bulletSpriteInWorldPosition = [bulletSprite ConvertToWorldSpace:bulletSprite.position];
    – Yucel_K
    Sep 2, 2013 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.