Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a CCparallaxNodeExtras that scrolls infinite (following the space game tutorial). I added as a child a CCSprite made of other CCSprite, like this:

_backgroundNode = CCParallaxNodeExtras::node();
this->addChild(_backgroundNode,-2);

    float acum = 0.0;
    back1 = CCSprite::create();
    for(int i = 0; i < num_repeats; ++i) {
        CCSprite *back = CCSprite::createWithSpriteFrameName("rock.png");
        back->setPosition(ccp(acum, 0));
        back1->addChild(back);
        acum+= back->getContentSize().width+150.0;
    }

    _backgroundNode->addChild(back1,  1 , ccp(0.1,0.1), ccp(0, winSize.height * 0.64));

now in my update I have this:

CCPoint backgroundScrollVert = ccp(-1024, 0);
_backgroundNode->setPosition(ccpAdd(_backgroundNode->getPosition(), ccpMult(backgroundScrollVert, dt)));

perfect, the background moves and disappears (that is what I want for now) but I need to get the collision between the sprites on that background and a fixed sprite as a child of the main node.

The problem is that whenever I try to get the collision by the simple way (intersecting the bounding boxes) it doesn't work, so I tried to just get the position of the sprites and all I get is the position fixed in the CCSprite (back1) composed by the sprites (back).

Now, is there a possible way to get the position of any individual sprite located in that parallax node? If i try something like:

CCSprite *tempsprite = (CCSprite*)_backgroundNode->getChildren()->objectAtIndex(0);
printf("%f\n", tempsprite->getChildren()->objectAtIndex(0)->getPositionX());

it prints always the same value, it is like the position is not being affected by the transformation of the parent in the background node... so, how do I get it correctly? how do I get the position relative to screen and not to the parent?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The position of the child is the relative position of the child plus the position of the parent.

So the x position of the child would be something like:

tempsprite->getChildren()->objectAtIndex(0)->getPositionX() + tempsprite->getPositionX();
share|improve this answer
    
This almost worked, I had to add the position of the backgroundnode plus half width of the winsize, I don't know why, but it worked. Thanks :) –  nosmirck Aug 14 '13 at 13:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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