Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting menu item location when menu item is clicked Cocos2D (passing them into a function)
    primarykey
    data
    text
    <p>Is there any way to get a location of a menu button in Cocos2d when I've clicked it?</p> <p>So I have a menu:</p> <p>HelloWorld.h</p> <pre><code>//creating a menu CCMenu *menu; </code></pre> <p>HelloWorld.m</p> <pre><code>// 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 &lt;= 13; i++) { for (int j = 1; j &lt;= 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"); } </code></pre> <p>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).</p> <p>What I want to do here is,</p> <p>When I run the program in simulator I want to be able to get a location of a menu button pressed.</p> <p>Thanks, looking forward to hearing from you.</p> <p>I think I found a solution to my own question:</p> <pre><code>-(void)someSelector:(id)selector </code></pre> <p>has to be changed into</p> <pre><code>-(void)someSelector:(CCMenuItem *) item </code></pre> <p>and then you can do this:</p> <pre><code>NSLog(@"Grid is pressed %f %f", item.position.x, item.position.y); </code></pre> <p>and voila! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload