2

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?
bshirley
  • 8,217
  • 1
  • 37
  • 43
  • getting distracted while asking a question leads to you not asking a question. :) Edited to add 3, just for good measure. – bshirley Apr 09 '12 at 21:44

3 Answers3

1

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?

skytz
  • 2,201
  • 2
  • 18
  • 23
  • I _am_ creating a `CCMenuItemImage`, not the abstract superclass. – bshirley Apr 09 '12 at 22:18
  • 1
    have you tried my code? does it work? what doesnt it work on it? – skytz Apr 09 '12 at 22:21
  • d'oh! just realized i added the new image to the file structure, but __not__ to the project. It was silently/safely failing when the image was not found. #usererror thanks for brainstorming it with me! – bshirley Apr 09 '12 at 22:32
  • lolz how do you add something to the file structure w/o adding to project? didn't you use add files? – skytz Apr 09 '12 at 22:34
  • i duplicated it in the file system, renamed it to "selected", edited the image to make it look different, . . . la, la, la, . . . *face* *palm* – bshirley Apr 09 '12 at 22:43
  • It sometimes happens whenever you duplicate the image n Change the Name of the image also you forget to add the reference of your image into the file structure – Marine Apr 10 '12 at 13:33
1

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"]];
Anshul Jain
  • 903
  • 6
  • 9
1

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.

bshirley
  • 8,217
  • 1
  • 37
  • 43