Reputation: 36013
I have this
CCMenuItem *play =
[CCMenuItemSprite itemWithNormalSprite:[CCSprite spriteWithSpriteFrameName:@"play.png"]
selectedSprite:[CCSprite spriteWithSpriteFrameName:@"stop.png"]];
CCMenuItem *stop =
[CCMenuItemSprite itemWithNormalSprite:[CCSprite spriteWithSpriteFrameName:@"stop.png"]
selectedSprite:[CCSprite spriteWithSpriteFrameName:@"play.png"]];
CCMenuItemToggle *stopPlayButton =
[CCMenuItemToggle itemWithTarget:self
selector:@selector(togglePlayStop:)
items:Play, botaoStop, nil];
CCMenu *togglePlayStop = [CCMenu menuWithItems:stopPlayButton, nil];
This is a play/stop button. It is normally showing the play image. The user taps the button, the movie plays and the button toggles to show the stop image, so the user can stop playing the movie. The movie reaches its end. The app stop playing the movie. Now I have to toggle the image on the button, to show the play image again.
How do I "tap" the button programmatically?
Thanks.
Upvotes: 1
Views: 2498
Reputation: 1749
From Cocos2d-x 3.0, the API changes to MenuItemToggle, the usage is like this:
//in SomeLayer init()
auto onItem = MenuItemImage::create(...);
auto offItem = MenuItemImage::create(...);
auto aToggle= MenuItemToggle::createWithCallback(CC_CALLBACK_1(SomeLayer::onAToggle, this),
onItem, //0th
offItem, //1st
NULL);
aToggle->setSelectedIndex(getSomeBoolValueForThisToggle()?0:1); //true to show 0th, false 1st
Upvotes: 0
Reputation: 10860
CCMenuItemToggle
instance has property selectedIndex
. You can both read and write it. In your case item at index 0 will be your play button, item at index 1 will be your stop button. So it is enoough to declare your toggle in your interface, then just change selectedIndex
property. Both
[m_stopPlayToggle setSelectedIndex:0];
and
m_stopPlayToggle.selectedIndex = 0;
will set your play button as active.
Upvotes: 8