Is there a way to get the location of the menu button in Cocos2d when I clicked it?
So I have a menu:
HelloWorld.h
CCMenu *menu;
HelloWorld.m
menu = [CCMenu menuWithItems:nil];
menu.position = ccp(0,0);
[self setItem];
[self addChild:menu];
- (void)setItem
{
for (int i = 1; i <= 13; i++) {
for (int j = 1; j <= 8; j++) {
CCMenuItem grid = [CCMenuItemSprite
itemWithNormalSprite:[CCSprite spriteWithSpriteFrameName:@"menuItem.png"]
selectedSprite:[CCSprite spriteWithSpriteFrameName:@"selected.png"]
target:self
selector:@selector(someSelector:)];
float x = (j+0.55) * grid.contentSize.width;
float y = (i-0.5) * grid.contentSize.height;
grid.position = ccp(x, y);
[menu addChild:grid];
}
}
}
-(void)someSelector:(id)selector
{
NSLog(@"Grid is pressed");
}
Basically, what happens above, I create a menu, then I call a function that creates menu items, after creating these menu items they are added to the menu. Each menu item has a purpose of self and selector - someSelector function, which I want to pass to the parameters (the location of the menu button).
What I want to do here
When I run the program in the simulator, I want to be able to find the location of the menu button.
Thanks, expecting to hear from you.
I think I found a solution to my question:
-(void)someSelector:(id)selector
need to change to
-(void)someSelector:(CCMenuItem *) item
and then you can do this:
NSLog(@"Grid is pressed %f %f", item.position.x, item.position.y);
and voila! :)