cocos2d-x toggle button

  • mik4s_
    Likes 0

    Problem Description

    Hello, I am beginer in cocos2d . Could you help me to create toggle button (for e.g. to Play/Pause music) in cocos2d-x, c++. I know that this method is build into Sonar Cocos Helper but I want to know how to implement it from scratch. Hope You can help me.

  • Sonar Systems admin
    Likes 0

    Hello,

     

    I can indeed. How much Cocos2d-x you already know

  • mik4s_
    Likes 0

    I know the basic. I already tried to create some build in ui components, switch between scenes, transform sprites, basic animations, play sounds, perform actions.

  • Sonar Systems admin
    Likes 0

    I would say learn how to do the following then we can move onto the next steps for a toggle button.

     

    • Create and draw a button
    • Change image after creation
  • mik4s_
    Likes 0

    I can not think or find a way how to change an image of ui::button after creation. Is this even possible? Should I use MenuItemImage instead of ui::button. Thanks for helping me out.

  • Sonar Systems admin
    Likes 0

    Try doing .loadtexture

  • mik4s_
    Likes 0

    Ok, I managed to change the sprite. Ho do I pass my button to touch listener function?

  • Sonar Systems admin
    Likes 0

    Make it a object in the header file so it can be accessed anywhere in the cpp file.

  • mik4s_
    Likes 0

    Looks like I finally created this button:

    //MainMenuScene.h
    Button *sound;
    bool isSoundOn;
    //MainMenuScene.cpp
    bool MainMenuScene::init(){
    bool isSoundOn = true;
    isSoundOn = true;
    sound = Button::create("SoundOn.png", "SoundOnSelected.png");
    sound->setPosition(Vec2(visibleSize.width * 0.2 + origin.x, visibleSize.height * 0.3));
    this->addChild(sound, 2);
    sound->addTouchEventListener(CC_CALLBACK_2(MainMenuScene::touchFx, this));
    sound->setTag(MAIN_MENU_SOUND_BUTTON_TAG);
    }
    
    void MainMenuScene::touchFx(Ref *sender, Widget::TouchEventType type) {
    	Node *node = (Node*) sender;
    	switch(type) {
    		case Widget::TouchEventType::ENDED:
    			if(MAIN_MENU_SOUND_BUTTON_TAG == node->getTag()) {
    				if(isSoundOn == true) {
    					sound->loadTextures("SoundOff.png", "SoundOffSelected.png");
    					isSoundOn = false;
    					CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic();
    				}
    				else if(isSoundOn == false) {
    					sound->loadTextures("SoundOn.png", "SoundOnSelected.png");
    					isSoundOn = true;
    					CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("music.mp3", true);
    				}
    			}
    		default:
    			break;
    	}
    }

    >What is the proper way to implement this?
    >Is it possible to avoid global variables?
    >How to save sound button state that when I reopen the game it keeps my previous choice?

  • Sonar Systems admin
    Likes 0

    Having variables in your class header is fine.

     

    This is how you store data https://www.youtube.com/watch?v=_FYsuSFKCZU 


Login to reply