Setting touch areas

I have a sprite that i want to touch and drag across the screen but first i need to know how to create a touch area or touch around the sprite’s content size.

heres the code i used when using sprite builder and objective c:

- (void)touchBegan:(CCTouch *)touch withEvent:(CCTouch *)event{
    CGPoint touchLocation = [touch locationInNode:self];
    if (touchLocation.x > _coin.position.x - 50 && touchLocation.x < _coin.position.x + 50 && touchLocation.y > _coin.position.y - 50 && touchLocation.y < _coin.position.y + 50) {
        coinTouched = true;
    }
}

basically how do i convert that to c++?

I have the touch listeners and my touch begin method is pretty much blank

bool GameplayScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event){
    CCLOG("onTouchBegan x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
    
    return true;
}

Replace
CGPoint touchLocation = [touch locationInNode:self];
with
auto touchLocation = touch->getLocation;

then the ‘if’ can stay the same.

1 Like