Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting menu item location when menu item is clicked Cocos2D (passing them into a function)

Is there any way to get a location of a menu button in Cocos2d when I've clicked it?

So I have a menu:

HelloWorld.h

//creating a menu
CCMenu *menu;

HelloWorld.m

// initializing the menu and its position
menu = [CCMenu menuWithItems:nil];
menu.position = ccp(0,0);

// set cells in placing grid
[self setItem];
[self addChild:menu];

- (void)setItem
{
  //this method is a loop that creates menu items
  //but i've simplified it for this example, but please keep in mind that there are lots 
  //of menu items and tagging them could be troublesome

  for (int i = 1; i <= 13; i++) {
      for (int j = 1; j <= 8; j++) {

        // this creates a menu item called grid
        CCMenuItem grid = [CCMenuItemSprite 
          itemWithNormalSprite:[CCSprite spriteWithSpriteFrameName:@"menuItem.png"]
          selectedSprite:[CCSprite spriteWithSpriteFrameName:@"selected.png"] 
          target:self 
          // when button is pressed go to someSelector function
          selector:@selector(someSelector:)];

          //coordinates
          float x = (j+0.55) * grid.contentSize.width;
          float y = (i-0.5) * grid.contentSize.height;

          //passing the coordinates
          grid.position = ccp(x, y);

          //add grid to the menu
          [menu addChild:grid];

          //loop unless finished
       }
  }

}

-(void)someSelector:(id)selector
{
   //i know when the button is pressed but is there any way 
   //to pass selected menu coordinates to this function?
   NSLog(@"Grid is pressed");
}

Basically what happens above is - I create a menu, then I call a function that creates menu items, once those menu items are created they are added to the menu. Each menu items has a target of self and selector is someSelector function, which I want to pass parameters to(menu button location).

What I want to do here is,

When I run the program in simulator I want to be able to get a location of a menu button pressed.

Thanks, looking forward to hearing from you.

I think I found a solution to my own question:

-(void)someSelector:(id)selector

has to be changed into

-(void)someSelector:(CCMenuItem *) item

and then you can do this:

NSLog(@"Grid is pressed %f %f", item.position.x, item.position.y);

and voila! :)

like image 676
Eugene Avatar asked Dec 31 '25 18:12

Eugene


1 Answers

In your handler, you can cast the sender as a CCMenuItem and access its position from that:

-(void)someSelector:(id)sender
{
   //i know when the button is pressed but is there any way 
   //to pass selected menu coordinates to this function?
   NSLog(@"Grid is pressed");
   CCMenuItem* menuItem = (CCMenuItem*)sender;
   float x = menuItem.position.x;
   float y = menuItem.position.y;
}
like image 55
hspain Avatar answered Jan 02 '26 07:01

hspain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!