Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:59:39+00:00 2026-06-03T13:59:39+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:59:40+00:00Added an answer on June 3, 2026 at 1:59 pm

    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.. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple containers like this <div class=container> <span> <!-- Inside the span is
I have multiple video elements on my page. They look like this: <video class=video-js
I have an image sprite of sorts with multiple frames for 1 button. I'm
I have multiple column headers that spans of multiple column and I would like
Within the container BubbleContainer I have multiple Bubble sprites. Each bubble's graphics object (a
I would like to use SASS & Compass to generate some specific sprites. This
I have a game object that manages several sprite objects. Each of the sprites
I have a one spritesheet image with all sprites, I downloaded this image from
I would like to load textures, and then have them be used by multiple
I have multiple lists like below [u'a', 11, u'P'] [u'a', 11, u'A'] [u'b', 2,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.