Search code examples
cocos2d-iphonetouch

what the code "if (![super itemForTouch:touch]) { return NO;}" does when under ccTouchBegan and how to make it working in cocos2d v3?


I can not find any reference to itemForTouch in coco2d v2 ccmenu, so could someone please tell me what this code does? The if (![super itemForTouch:touch]) part, and how to make it work in coco2d v3?

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
   if (![super itemForTouch:touch])  {
    return NO;  
}

}

thanks


Solution

  • Where are you getting this code from?

    There is an itemForTouch in CCMenu.m. I assume by looking at what you posted that someone derived a class from CCMenu, overridden their own ccTouchBegan, and called the super's itemForTouch method. Inside that method it is looping over all attached menu items, checking if they are visible and enabled, and see's if the touch position lands within that menu item's rectangle. The code you posted will simply return NO if none of the menu items where touched.

    You shouldn't have to call this method manually, which is why it is not declared in the header file. Without seeing the bigger picture of the code you posted I can't see why someone would be subclassing from CCMenu, assuming they even have a legit reason to begin with. Also in cocos2d v3 look at using CCButton, which an example is given when you create a new project using the basic cocos2d template.

    Edit

    I see now after looking at the code you linked in the comments that it is to create a menu popup class. All he is doing is seeing if one of the parents is a popup and, if so, closes the popup since he knows a button on the popup was touched. To do this in V3 assumes you have some parent node that is acting as CCMenuPopup. You can even go as far as to name it the same and recreate those classes if you feel the need to do so.