2012-05-08 2 views
3

클릭했을 때 Cocos2d에서 메뉴 버튼의 위치를 ​​가져 오는 방법이 있습니까? 메뉴 항목을 클릭 할 때 메뉴 항목 위치 얻기 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라는 대상이 있고 selector는 someSelector 함수이며 매개 변수를 (메뉴 단추 위치)에 전달하려고합니다. 무엇 내가 여기서하고 싶은

,

내가 누르면 메뉴 버튼의 위치를 ​​얻을 수 있기를 원하는 시뮬레이터 프로그램을 실행

.

감사합니다. 귀하의 의견을 기다리고 있습니다.

은 내가 내 자신의 질문에 대한 해결책을 찾을 생각 :

-(void)someSelector:(CCMenuItem *) item 

-(void)someSelector:(id)selector 

이 변경되어야하고 그 다음이 수행 할 수 있습니다

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

를 늘리면 ! :) 핸들러에서

답변

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; 
} 
+0

큰 hspain의 그! 고마워. 조금 나 한테 설명해 주시겠습니까? 그래서 나는 selector가 눌려진 메뉴 항목에 할당 된 일종의 id라고 가정한다. 그게 맞습니까? – Eugene

+0

실제로 "보낸 사람"이라고 말하고 응답을 업데이트했습니다. sender 매개 변수는 눌려진 메뉴 항목에 대한 참조입니다. 그런 다음 이벤트를 호출하는 것임을 알기 때문에 CCMenuItem에이 이벤트를 캐스팅 할 수 있습니다. – hspain

관련 문제