CCMenuItemImage isn't showing the selected state

1.9k Views Asked by At

The documentation for CCMenuItemImage doesn't actually say what it does.

There are quite a few subclass CCMenuItem. I've inherited a project that's using it as a button.

CCMenuItem *start;
start = [CCMenuItemImage itemFromNormalImage:[self prefixedImage:@"start button.png"]
                               selectedImage:[self prefixedImage:@"start button selected.png"]
                                      target:myTarget
                                    selector:@selector(start:)];

It was using the same button for both states. I modified it to have a different image for the selected state.

I was expecting/hoping that when I touch the item it will be highlighted, and when I release the button it will send my target action (which it does).

(aside: in iOS parlance, i know that highlighted and selected are two different things. But this library does not seem to have that difference.)

So:

  • Is it intended to use this "menu item" as a button?
  • When is the selected image of this menu item displayed?
  • How should I go about making it display as selected?
3

There are 3 best solutions below

0
On

The code above is correct.

The image resource for selection was not added to the project, so was not being displayed. It may have output an error message on creation (buried in other output), but did not output error message when tapped.

The silent/safe failure made the user error harder to track down.

0
On

Try this code...

CCMenuItemImage *backbtn = [CCMenuItemImage itemFromNormalImage:@"backbtn.png" selectedImage:@"backbtn_selected.png" target:self selector:@selector(LBback)];
CCMenu *Menu1 = [CCMenu menuWithItems:backbtn,nil];

[Menu1 alignItemsVerticallyWithPadding:15];
Menu1.position = ccp(160, 240);
[self addChild:Menu1];

By the help of this..when you touch on image is shows selected image other wise normal image...:)

and later when your function get called and you want to change its image then you can set like this..

 [backbtn setNormalImage:[CCSprite spriteWithFile:@"backbtn_selected.png"]];
6
On

CCMenuItem is an abstract class from which all of the other menu items inherit so what you did there in the code is technically wrong.

On the other hand you could subclass CCMenuItem to make your own custom class (for example: you can't use a button and a label on it as a menu item, you have to use either the button itself and the label is on top..just for show, or use the label and the button below is...pointless)

Subclassing CCMenuItem and making your own class would fix that problem (i mean you could make a method that would take an image and a string and returns a button)

What you want to do there is this:

 CCMenuItemImage *button= [CCMenuItemImage itemFromNormalImage:@"start button.png"
                           selectedImage:@"start button selected.png"
                                  target:self
                                selector:@selector(start:)];

 CCMenu *start=[CCMenu menuWithItems:button,nil];
start.position=ccp(200,200);
[self addChild:start];

When you put your finger on the menu it will replace the normal image with the selected one, but will only activate of you release it in the boundingbox of the button (aka..you can press on the button, move your finger away from the button and it wont activate). So in a sence the button is highlighted untill you release your finger, then its selected.

Did that answer your question?