Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

cocos2d - CCMenuItem - check which button was touched

AchmesAchmes Posts: 32Registered Users
edited December 2010 in iPhone SDK Development
Hey guys.

I have 30 buttons (they're used to select levels).
Every button call the same function if it's touched.
But inside this function I really need to know which button was touched.
How can I do that?

Here is my code:
CCMenuItem* levelsItem[30];

....

for(int i=0; i<30; i++)
{
	levelsItem[i] = [CCMenuItemImage itemFromNormalImage:@"lev_ukonczony.png" 
				selectedImage:@"lev_ukonczony.png" target:self selector:@selector(onLevelsItem:)];

	xy_levele[i][0] = -winSize.width/2 + levelsItem[i].contentSize.width/2 + 15*i + levelsItem[i].contentSize.width*i + 50 + winSize.width;
	xy_levele[i][1] = 60-(50*i);
			
			
	[levelsItem[i] setPosition:ccp(xy_levele[i][0],xy_levele[i][1])];
}

....

- (void) onLevelsItem: (id) sender
{
	//How to check which button was touched?
}
Post edited by Achmes on

Replies

  • smashersmasher Posts: 3,859Registered Users @ @ @ @ @
    edited December 2010
    The "sender" is the CCMenuItemImage that was clicked. If you set the tag or the userData for the menuItem when you create it then you can tell the difference; or if you keep a pointer around and check if sender==myButton.

    - (void) onLevelsItem: (CCMenuItemImage*) sender {
    	//check tag, userData, or pointer
    }
    
    
    TinyCo is Hiring Mobile Game Programmers (C++)
    http://jobvite.com/m?3Ho5wgwr
  • balasaidabalasaida Posts: 5New Users
    edited December 2010
    Achmes wrote: »
    Hey guys.

    I have 30 buttons (they're used to select levels).
    Every button call the same function if it's touched.
    But inside this function I really need to know which button was touched.
    How can I do that?

    Here is my code:
    CCMenuItem* levelsItem[30];
    
    ....
    
    for(int i=0; i<30; i++)
    {
    	levelsItem[i] = [CCMenuItemImage itemFromNormalImage:@"lev_ukonczony.png" 
    				selectedImage:@"lev_ukonczony.png" target:self selector:@selector(onLevelsItem:)];
    
    	xy_levele[i][0] = -winSize.width/2 + levelsItem[i].contentSize.width/2 + 15*i + levelsItem[i].contentSize.width*i + 50 + winSize.width;
    	xy_levele[i][1] = 60-(50*i);
    			
    			
    	[levelsItem[i] setPosition:ccp(xy_levele[i][0],xy_levele[i][1])];
    }
    
    ....
    
    - (void) onLevelsItem: (id) sender
    {
    	//How to check which button was touched?
    }
    



    Hi,

    U just use tag value for each button when u create a button.
Sign In or Register to comment.