Cocos Studio Assign action to button?

  • warning717
    Likes 0

    Problem Description

    From the docs there is an explanation on how to access it from the code but how do we actually give it an action?

    http://cocos.sonarlearning.co.uk/v1.0/docs/widgets-button

  • warning717
    Likes 1

    This worked for me:

    I wasnt able to change the scene directly so I just used a scheduler. Hopefully this will be of some use to others.

    In CPP

    cocos2d::ui::Button *button = ( cocos2d::ui::Button * )rootNode->getChildByTag( 8 );
        button->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
            switch (type)
            {
                    case ui::Widget::TouchEventType::BEGAN:
                            break;
                    case ui::Widget::TouchEventType::ENDED:
                            //std::cout << "Button 1 clicked" << std::endl;
                            //Change Scene
                            this->schedule(schedule_selector(MainMenu::ChangeScene), 1.0f);
                            break;
                    default:
                            break;
            }
    });
    
        return true;
    }
    void MainMenu::ChangeScene(float dt) {
        // run main menu
        Scene *scene = HelloWorld::createScene( );
        
        Director::getInstance( )->replaceScene( TransitionSlideInR::create( 1.0, scene ) );
    }
    

    In Header

    private:
        void ChangeScene(float dt);

     

  • Sonar Systems admin
    Likes 0

    You my good friend are amazing. We forgot all about putting it on there. We will update the Cocos API Guide tomorrow.

     

    That is the way of doing it BTW.

  • warning717
    Likes 1

    Haha okay and thank you very much for the API Guide. I find it very helpful :D

  • Sonar Systems admin
    Likes 0

    Updated the button section on the API Guide accordingly.

     

    Thank you very much for letting us know laugh


Login to reply