Detect touch on a scaled CCSprite

Hi,
I cant detect the touch on a Scaled CCSPrite.
Below is the method I am using:
THIS IS WORKING FINE WHEN NOT SCALED THE SPRITE, BUT NOT WHEN SCALE IT :frowning:

void GameplayWindow::ccTouchesBegan(CCSet pTouches, CCEventpEvent)
{
CCSetIterator it = pTouches~~>begin;
for; iTouchCount++ )
{
touch = ;
CCPoint pt = touch~~>getLocation();
CCRect recTemp= sprite->boundingBox();
if(recTemp.containsPoint(pt ))
{
//TOUCHED
}
else
{
//NOT TOUCHED
}
}
}

Please help me….

What exactly does not work when scaled?

Try this snippet:

bool detectTouchOnNode ( CCNode* pNode, const CCTouch* const pTouch,
                         CCSize margin )
{
    assert ( pNode );
    assert ( pTouch );

    const CCPoint touchLocation = pTouch->getLocation();
    CCRect fieldRect;
    fieldRect.origin = pNode->convertToWorldSpace ( CCPointZero );
    fieldRect.size = gui::Positioner::getScaledSize ( pNode ); // you have to implement it

    fieldRect.size.width += margin.width;
    fieldRect.size.height += margin.height;

    return fieldRect.containsPoint ( touchLocation );
}

Hi Jakub,
Thanks for the snippet.
How to get gui:: reference please in Cocos2d-X?
Yes, it is not working when scaled.

Thanks

As I wrote: you have to implement it :slight_smile:

float getScaledWidth ( CCNode* pNode )
{
    assert ( pNode );
    return pNode->getContentSize().width * pNode->getScaleX();
}

float getScaledHeight ( CCNode* pNode )
{
    assert ( pNode );
    return pNode->getContentSize().height * pNode->getScaleY();
}

CCSize getScaledSize ( CCNode* pNode )
{
    assert ( pNode );
    return CCSizeMake ( getScaledWidth ( pNode ), getScaledHeight ( pNode ) );
}

Thanks Jakub… now its working :slight_smile: