0
votes

I have multiple sprites placed onto a background sprite like this:

//my background
CCSprite *bg = [CCSprite spriteWithFile:@"imageName.png"];

[self addchild:bg];

And then I add my items onto bg

//this is how i add my items
CCSprite *items = [CCSprite spriteWithFile:@"itemName.png"];

[bg addchild:items];

Oh and not forgetting my car sprite

//my car
CCSprite *car = [CCSprite spriteWithFile:@"car.png"];
[self addchild:car];

I use a loop to add multiple sprites onto the bg.

Now the question is how do I detect whether the car collided with the multiple sprites that I have placed onto the bg?

I've tried using CGRectIntersectsRect and it doesn't work.

I've tried using the pythagoras theorem method and once again it doesn't work.

There was a method which involved adding the items sprites into a NSMutableArray and it doesn't work either.

Can anyone suggest a method whereby I can try?

Additional code:

-(void) initializeCarAndItems
{
    car = [CCSprite spriteWithFile:@"android.png"];
    car.position = ccp(screenSize.width/2, screenSize.height * 0.30);
    [self addChild:car z:1];
    carRect = [car boundingBox];
}

-(void) initializeMap
{
    bg1 = [CCSprite spriteWithFile:@"racingBG.png"];
    bg1.anchorPoint = ccp(0, 0);
    bg1.position = ccp(0, 0);

    [self addChild:bg1 z:-1];

    bg2 = [CCSprite spriteWithFile:@"racingBG2.png"];
    bg2.anchorPoint = ccp(0,0);
    bg2.position = ccp(0, bg1.boundingBox.size.height - 1);

    [self addChild:bg2 z:-1];

    convertedWidth = (int)bg1.boundingBox.size.width;
    convertedHeight = (int)bg1.boundingBox.size.height;

    for (y = 0; y < 15; y++)
    {   
        positionX = arc4random()%convertedWidth;
        positionY = arc4random()%convertedHeight;

        items = [CCSprite spriteWithFile:@"item.png"];
        items.position = ccp(positionX, positionY + 300);
        [bg1 addChild:items z:100];
        [itemsArray addObject:items];
    }

    for (y = 0; y < 15; y++)
    {   
        positionX = arc4random()%convertedWidth;
        positionY = arc4random()%convertedHeight;

        items = [CCSprite spriteWithFile:@"item.png"];
        items.position = ccp(positionX, positionY);
        [bg2 addChild:items z:100];
        [itemsArray addObject:items];
    }
}

-(void) accelerate
{
    bg1.position = ccp(0, bg1.position.y - accelerateNumber);
    bg2.position = ccp(0, bg2.position.y - accelerateNumber);

    if (bg1.position.y < -bg1.boundingBox.size.height)
    {
        questionCount++;
        bg1.position = ccp(0, bg2.position.y + bg2.boundingBox.size.height - 1);
        [self question];

        [bg1 removeAllChildrenWithCleanup:YES];
        for (y = 0; y < 15; y++)
        {   
            positionY = arc4random()%convertedHeight;
            positionX = arc4random()%convertedWidth;

            items.position = ccp(positionX, positionY);
            items = [CCSprite spriteWithFile:@"item.png"];
            [bg1 addChild:items z:100];
            [itemsArray addObject:items];
        }
    }
    else if (bg2.position.y < -bg2.boundingBox.size.height)
    {
        questionCount++;
        bg2.position = ccp(0, bg1.position.y + bg1.boundingBox.size.height - 1);
        [self question];

        [bg2 removeAllChildrenWithCleanup:YES];
        for (y = 0; y < 15; y++)
        {   
            positionY = arc4random()%convertedHeight;
            positionX = arc4random()%convertedWidth;

            items.position = ccp(positionX, positionY);
            items = [CCSprite spriteWithFile:@"item.png"];
            [bg2 addChild:items z:100];
            [itemsArray addObject:items];
        }
    }
}

-(void) update:(ccTime)deltaTime
{
    [self ifEdgeOfScreen];
    [self accelerate];

    for (CCSprite *itemFromArray in itemsArray)
    {
        CGRect itemRect = [itemFromArray boundingBox];
        if (CGRectIntersectsRect(carRect, itemRect))
        {
            NSLog(@"Collision!");
        }
    }

    if (leftButton.active == TRUE)
    {
        [self moveLeftRight:1];
    }
    else if (rightButton.active == TRUE)
    {
        [self moveLeftRight:2];
    }
}

UPDATE:

It's fixed :)

-(void) update:(ccTime)deltaTime
{
    car = [car boundingbox];

    [self ifEdgeOfScreen];
    [self accelerate];



    for (CCSprite *itemFromArray in itemsArray)
    {
        if (CGRectIntersectsRect(carRect, [itemFromArray boundingbox]))
        {
            NSLog(@"Collision!");
        }
    }

    if (leftButton.active == TRUE)
    {
        [self moveLeftRight:1];
    }
    else if (rightButton.active == TRUE)
    {
        [self moveLeftRight:2];
    }
}
1
can you write the code for intersection also? How you are accessing objects? As it will show how you are handling collision... :)Nikhil Aneja
Hey Nikhil Aneja! I've added more code into the question :) Thanks in advance!Yongzheng

1 Answers

0
votes

I found so many problems with the code....

  1. When you call removeAllChildren.. Make sure you also remove objects from array.. Removing sprite from parents does not remove it from array.

  2. update the car rect in update Method. So in your update method

    -(void) update:(ccTime)deltaTime {

        [self ifEdgeOfScreen];
    
        [self accelerate];
    
        carRect = [car boundingBox];
    ...........
    }
    

Hope this helps.. :)