Possible to toggle groups of buttons?

Forums Programming cocos2d support (graphics engine) Possible to toggle groups of buttons?

This topic contains 1 reply, has 2 voices, and was last updated by  jamesgrote 4 years, 5 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
Author Posts
Author Posts
November 12, 2009 at 5:16 am #218384

XyrisKenn
@xyriskenn

Hi Forum, I’m trying to build a preferences screen where a player can select a game piece’s size. I’d like to have three buttons – small, medium, large, and have the selected choice toggle ‘on’ while the others toggle ‘off’. This sounds like what MenuItemToggle was made for, except it shows only one button and toggles between button 1 and button 2. I’ve tried a few solutions like manually setting button toggles, using sprites and even nesting ItemToggles. No joy.

Is MenuItemToggle only good for displaying a single menuItems in its container of menuItems, or can it toggle amongst ‘three-in-a-row’ MenuItems? I want to display all three toggle options on screen at the same time, not cycle through ‘hidden’ options in the toggle stack.

#pragma mark Game Piece sizes

// size one button
MenuItem *smallGamePieceBtn = [MenuItemImage itemFromNormalImage:@"pieceOne_small_up.png"
selectedImage:@"pieceTwo_small_down.png"
target:self
selector:@selector(fake:)];

[smallGamePieceBtn setPosition:ccp(-75,54)];

// size two button
MenuItem *mediumGamePieceBtn = [MenuItemImage itemFromNormalImage:@"pieceTwo_big_up.png"
selectedImage:@"pieceTwo_big_down.png"
target:self
selector:@selector(fake:)];

[mediumGamePieceBtn setPosition:ccp(64,60)];

// size three button
MenuItem *LargeGamePieceBtn = [MenuItemImage itemFromNormalImage:@"pieceThree_big_up.png"
selectedImage:@"pieceThree_big_down.png"
target:self
selector:@selector(fake:)];

[LargeGamePieceBtn setPosition:ccp(126,65)];

#pragma mark Game Piece size toggle

MenuItemToggle *sizeToggle = [MenuItemToggle itemWithTarget: self
selector:@selector(sizeSelect:)
items: SmallGamePieceBtn, MediumGamePieceBtn, LargeGamePieceBtn, nil];

This will toggle one image and it ignores the button positioning, but how does one toggle three images? Is it better to just code that action manually?

Thanks for sharing your ideas.

November 16, 2009 at 1:58 am #266185

jamesgrote
Participant
@jamesgrote

What I did for a similar situation is subclass MenuItem, adding a property for isActive. That way, when an item is pressed it will be active, and deactivate all other children of the parent Menu class. Here’s my activate class for my subclassed MenuItem:

-(void) activate
{
if(isEnabled)
{
if (activeImage_ != nil)
{
Menu *parentMenu = (Menu*)[self parent];

// deactive all items
for(SwatchMenuItem *item in parentMenu.children)
{
[item setIsActive:NO];
}

// set active indicator
isActive = YES;
}

[super activate];
}
}

I also overwrote the MenuItem draw function to show an active status:

-(void) draw
{
if(isEnabled) {
if( selected )
{
self.scale = 1.2f;
[normalImage_ draw];
}
else
{
self.scale = 1.0f;
[normalImage_ draw];

if(isActive && activeImage_ != nil)
{
[activeImage_ draw];
}
}

} else {
if(disabledImage_ != nil)
[disabledImage_ draw];

// disabled image was not provided
else
[normalImage_ draw];
}
}

I define an “active” image that is displayed when a menuitem is active.

HTH,

Jim

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.