0

I am creating a toggle switch. I have a CCScene containing a CCLayer containing a ToggleNode. The ToggleNode shows properly with the sprites and labels I put in. The touch handling is not working because the ToggleNode's bounding box seems to remain zero. I catch the touch in the CCLayer (which works as the ccTouchBegan:withEvent: is being entered) and there I have this code:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

    NSLog(@"bounding box: %f, %f, %f, %f", toggleNode.boundingBox.origin.x, toggleNode.boundingBox.origin.y, toggleNode.boundingBox.origin.x + toggleNode.boundingBox.size.width, toggleNode.boundingBox.origin.y + toggleNode.boundingBox.size.height);
    NSLog(@"touch: %f, %f", touchLocation.x, touchLocation.y);

    if (CGRectContainsPoint(toggleNode.boundingBox, touchLocation)) {
        [toggleNode toggle];
    }

    return NO;
}

A touch on the ToggleNode results in:

bounding box: 512.000000, 384.000000, 512.000000, 384.000000
touch: 508.000000, 378.000000

Which makes me believe the bounding box remains zero. But why? A retain problem? I am still learning in cocos2d but I do not think this is normal behavior.

mmvie
  • 2,571
  • 7
  • 24
  • 39

2 Answers2

7

If ToggleNode is derived from CCNode, this behavior is normal. A CCNode has a 0-sized bounding box and the contentSize property is also zero.

If you derive your class from CCNode, it's up to you to set the contentSize property (this also updates the bounding box) to whatever size it should have.

Only if you use a class that use a texture (CCSprite, CCLabelTTF and others) does cocos2d set the contentSize by itself.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • What happens if you have a CCSprite with a CGRectZero, but give it children that have rects and proper content size? The parent still has a contentSize of 0.. I've noticed some really weird behavior where a parent like that with an anchor point of (0.5,0.5) will be displayed with expected positioning (centered), yet if you set the parent's contentSize to the size of the child, then its positioning is all screwed up. I don't understand why that happens, and I am curious if you're supposed to let CCSprite parents like that have a content size of 0. – patrick Jul 23 '15 at 05:29
  • You shouldn't be using a CCSprite if it doesn't have a texture, use a CCNode instead. Children do not alter their parent's contentSize, unless you tell them to. If you change a parent's contentSize from 0,0 to custom values and the parent has anchorPoint 0.5,0.5 then this will change the child node positions because they are anchored at the parent's lower left corner relative to the parent's anchorPoint and contentSize. – CodeSmile Jul 24 '15 at 09:29
  • Running out of comment space: by increasing the parent's contentSize with a non-zero anchorPoint, its "lower left" corner moves `(contentSize * anchorPoint)` points towards the left and down. But that's where the child nodes are anchored, not at their paren't position but at their `(parent.position - contentSize*anchorPoint)`. And that's why you should use anchorPoint 0,0 for the parent node, or just use a CCNode if the parent isn't supposed to draw anything by itself. – CodeSmile Jul 24 '15 at 09:31
1

You have to calculate your ToggleNode's contentSize yourself. You may also need to move ToggleNode's child nodes after calculating its contentSize, so they don't appear "outside" of its bounding box.

Kreiri
  • 7,840
  • 5
  • 30
  • 36