I comment all code, and then un comment line by line to identify the bug, I recognize that in the code to create the menu buttons for game. It have a MenuItemToggle that I’ve add the the Menu it cause the error. Below is the code to add the menu
// add a close menu, which is clicked to quit the program
auto closeItem = MenuItemSprite::create(
Sprite::createWithSpriteFrameName(kClose),
Sprite::createWithSpriteFrameName(kClose1),
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width / 2,
origin.y + visibleSize.height - closeItem->getContentSize().height / 2));
closeItem->setZOrder(1);
closeItem->setTag(ExitGame);
// add sound button to allow people option turn on or turn off the sound
auto on = MenuItemSprite::create(
Sprite::createWithSpriteFrameName(kSoundon),
Sprite::createWithSpriteFrameName(kSoundon), NULL);
auto off = MenuItemSprite::create(
Sprite::createWithSpriteFrameName(kSoundoff),
Sprite::createWithSpriteFrameName(kSoundoff), NULL);
auto toggleSoundItem = MenuItemToggle::createWithTarget(this, menu_selector(HelloWorld::menuToggleSoundCallback), on, off, NULL);
toggleSoundItem->setPosition(Vec2(closeItem->getPosition().x - toggleSoundItem->getContentSize().width - kDistance/2,
closeItem->getPosition().y));
toggleSoundItem->setSelectedIndex(soundMode == 1 ? 0 : 1);
toggleSoundItem->setZOrder(1);
auto optionItem = MenuItemSprite::create(
Sprite::createWithSpriteFrameName(kOption),
Sprite::createWithSpriteFrameName(kOption1),
CC_CALLBACK_1(HelloWorld::menuOptionCallback, this));
optionItem->setPosition(Vec2(toggleSoundItem->getPosition().x - optionItem->getContentSize().width - kDistance / 2,
closeItem->getPosition().y));
optionItem->setZOrder(1);
// add pause button to allow the player pause the game
auto pauseItem = MenuItemSprite::create(
Sprite::createWithSpriteFrameName(kPause),
Sprite::createWithSpriteFrameName(kPause1),
CC_CALLBACK_1(HelloWorld::menuPauseCallback, this));
pauseItem->setPosition(Vec2(optionItem->getPosition().x - pauseItem->getContentSize().width - kDistance / 2,
closeItem->getPosition().y));
pauseItem->setZOrder(1);
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, toggleSoundItem, optionItem, pauseItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 2);
The menuToggleSoundCallback callback method
void HelloWorld::menuToggleSoundCallback(Ref* pSender)
{
auto pToggleItem = (MenuItemToggle *)(pSender);
if (pToggleItem->getSelectedIndex() == 0) // sound on
{
soundMode = 1;
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
else // sound off
{
soundMode = 0;
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
}
When i click the pause button at second time, it cause crash, when i remove the toggleSoundItem from menu, the pause and resume run ok.
//auto menu = Menu::create(closeItem, toggleSoundItem, optionItem, pauseItem, NULL);
auto menu = Menu::create(closeItem, optionItem, pauseItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 2);
auto menu = Menu::create(closeItem, optionItem, pauseItem, NULL);
And one more code that cause error, after I remove the MenuItemToggle, I’ve add a sprite tree.png image that i put to a spritesheet file. When i add this sprite to the the game when I click pause at the second time it also cause crash. Below is the code that I’ve add to the game.
// add the Tree 1
tree1 = Sprite::createWithSpriteFrameName(kTree);
tree1->setPosition(Vec2(origin.x + 60 * scaleX, origin.y + visibleSize.height / 2 + kGroundHeight*1.75f*scaleY));
tree1->setScale(0.65f);
tree1->setZOrder(2);
this->addChild(tree1);
The tree1 variable type Sprite that I’ve declare at the HelloWorldScene.h file
cocos2d::Sprite* tree1;
kTree is a constant that is the name of the sprite.
The attach .png file name tree.png is the sprite that i add the the spritesheet file. It 36KB.
I don’t know what happen with this sptite, and what cause crash when i add the MenuItemToggle to the menu