检查是CMenu中的当前CGPoint

时间:2012-04-11 19:55:01

标签: ios cocos2d-iphone

我们可以检查CGPoint是否已触及CMenu项目? 如果指向按钮则需要返回YES,如果点没有按钮则需要NO。

 -(BOOL)isButton:(CGPoint)point
    {
        if(controlMode == 1)
        {

            for (CCMenu *menu in self.children)
            {

                    // If point in button
                    return YES;
                    // else
                    return NO;


                }
            }

        }
        return NO;
    }

菜单按钮:

CCMenuItem *pauseItem = [CCMenuItemImage itemFromNormalImage:@"pause.png" selectedImage:@"pause_.png" target:self selector:@selector(pauseGame:)]; 
CCMenuItem *resetItem = [CCMenuItemImage itemFromNormalImage:@"reset.png" selectedImage:@"reset_.png" target:self selector:@selector(resetClicked:)]; 
CCMenu *Menu = [CCMenu menuWithItems:pauseItem,resetItem, nil];
Menu.position = ccp(450, 40); 
[Menu alignItemsVerticallyWithPadding:5]; 
[self addChild:Menu];

2 个答案:

答案 0 :(得分:2)

由于您所指的是触摸(来自评论),如果您触摸菜单上的按钮,则会选择该项目。

-(BOOL) isAButtonSelected
{
    for(CCMenuItem* item in menu.children)
    {
        if(item.isSelected) return YES;
    }
    return NO;
}

答案 1 :(得分:1)

你可以尝试这样的smth

for(CCMenuItem* item in yourMenu.children)
{
    CCPoint localPoint = [item convertToNodeSpace:point];
    if( (localPoint.x <= item.contentSize.width) && (localPoint.y <= item.contentSize.height) )
    {
        // in that case your point is in this item of your CCMenu
    }
}