3

クリックしたときに Cocos2d のメニュー ボタンの位置を取得する方法はありますか?

だから私はメニューを持っています:

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");
}

基本的に上記の処理は、メニューを作成し、メニュー項目を作成する関数を呼び出します。これらのメニュー項目が作成されると、メニューに追加されます。各メニュー項目にはselfのターゲットがあり、セレクターはsomeSelector関数であり、パラメーターを渡したい(メニューボタンの場所)。

ここでやりたいことは、

シミュレーターでプログラムを実行するときに、メニュー ボタンが押された場所を取得できるようにしたいと考えています。

ありがとうございます。ご連絡をお待ちしております。

私は自分の質問に対する解決策を見つけたと思います:

-(void)someSelector:(id)selector

に変更する必要があります

-(void)someSelector:(CCMenuItem *) item

そして、これを行うことができます:

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

そして出来上がり!:)

4

1 に答える 1

4

ハンドラーで、送信者を CCMenuItem としてキャストし、そこからその位置にアクセスできます。

-(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;
}
于 2012-05-08T22:12:10.387 に答える