How do I make something invisible/hidden?

  • warning717
    Likes 0

    Problem Description

    Im trying to hide a UIPanel until a button is clicked

  • warning717
    Likes 0

    I just found it in the API guide :P, my bad

  • Sonar Systems admin
    Likes 0

    Good to hear

  • warning717
    Likes 0

    Spoke too soon :/
    Ive tried using panel, UIPanel, UILayout and layout but I keep getting them as not a member of ‘cocos2d::ui’

    // panel accessed as a node using it's tag
        Node *panel = rootNode->getChildByTag( 20 );
    
        // panel cast to a button using it's tag
        cocos2d::ui::layout *panel = ( cocos2d::ui::layout * )rootNode->getChildByTag( 20 );
        panel->setVisible( false );
    

     

  • Sonar Systems admin
    Likes 0

    Change layout to Layout

     

    The l needs to be capital 

  • warning717
    Likes 1

    Oh wow haha, smiley thank you

    Then how would I add it to a button?

    I get an error: ‘panel’ was not declared in the scope.

    In the init
        cocos2d::ui::Layout *panel = ( cocos2d::ui::Layout * )rootNode->getChildByTag( 20 );
    
    Button Event
    void MainMenu::touchEventSettings( Ref *sender, cocos2d::ui::Widget::TouchEventType type )
    {
        switch ( type )
        {
            case cocos2d::ui::Widget::TouchEventType::BEGAN:
                
                break;
                
            case cocos2d::ui::Widget::TouchEventType::MOVED:
    
                break;
                
            case cocos2d::ui::Widget::TouchEventType::ENDED:
            //show panel
            panel->setVisible( true );
                break;
                
            case cocos2d::ui::Widget::TouchEventType::CANCELED:
                
                break;
                
            default:
                break;
        }
    }

     

  • Sonar Systems admin
    Likes 0

    You want to declare it in the header but initialise it in the cpp.

  • warning717
    Likes 0

    This built and ran until clicking the button which crashes the app

    Header
    
    cocos2d::ui::Layout * panel;
    
    CPP
    
    Init
    cocos2d::ui::Layout *panel = ( cocos2d::ui::Layout * )rootNode->getChildByTag( 20 );
    
    Button Event
    void MainMenu::touchEventSettings( Ref *sender, cocos2d::ui::Widget::TouchEventType type )
    {
        switch ( type )
        {
            case cocos2d::ui::Widget::TouchEventType::BEGAN:
                
                break;
                
            case cocos2d::ui::Widget::TouchEventType::MOVED:
    
                break;
                
            case cocos2d::ui::Widget::TouchEventType::ENDED:
            //show panel
            panel->setVisible( true );
                break;
                
            case cocos2d::ui::Widget::TouchEventType::CANCELED:
                
                break;
                
            default:
                break;
        }
    }

     

  • Sonar Systems admin
    Likes 0

    Try commenting out the panel code inside the button function to see if it's been setup properly.

  • warning717
    Likes 0

    Removing

    //show panel
            panel->setVisible( true );

     

    no longer crashes the program on button click so the setup must be correct?

  • warning717
    Likes 0

    So then what is the correct way of doing it?

  • Sonar Systems admin
    Likes 0

    panel = ( cocos2d::ui::Layout * )rootNode->getChildByTag( 20 );

    Use this code instead inside the innit method 

  • warning717
    Likes 1

    That works laugh, Thank you!

  • Sonar Systems admin
    Likes 0

    great :D

  • warning717
    Likes 0

    For buttons inside the panel is there a different way of adding a touch event? I used the same method as previously but the app crashes.

    //This button is inside a panel in cocos studio
    confirmbutton = ( cocos2d::ui::Button * )rootNode->getChildByTag( 25 );
    confirmbutton->addTouchEventListener( CC_CALLBACK_2( MainMenu::Confirm, this ) );
    
    void MainMenu::Confirm( Ref *sender, cocos2d::ui::Widget::TouchEventType type )
    {
        switch ( type )
        {
            case cocos2d::ui::Widget::TouchEventType::BEGAN:
                
                break;
                
            case cocos2d::ui::Widget::TouchEventType::MOVED:
    
                break;
                
            case cocos2d::ui::Widget::TouchEventType::ENDED:
            
                break;
                
            case cocos2d::ui::Widget::TouchEventType::CANCELED:
                
                break;
                
            default:
                break;
        }
    }
    
    //Header
    void Confirm( Ref *sender, cocos2d::ui::Widget::TouchEventType type );
    

    This works up until giving the button a touch event listener.

  • Sonar Systems admin
    Likes 0

    How have you declared confirmButton?

  • warning717
    Likes 0

    Under Public in Header:

    void Confirm( Ref *sender, cocos2d::ui::Widget::TouchEventType type );

     

  • warning717
    Likes 0

    So to recap

    CPP

    confirmbutton = ( cocos2d::ui::Button * )rootNode->getChildByTag( 25 );
    //This touch event listener is what crashes the app.
    confirmbutton->addTouchEventListener( CC_CALLBACK_2( MainMenu::Confirm, this ) );
    
    
    void MainMenu::Confirm( Ref *sender, cocos2d::ui::Widget::TouchEventType type )
    {
        switch ( type )
        {
            case cocos2d::ui::Widget::TouchEventType::BEGAN:
                
                break;
                
            case cocos2d::ui::Widget::TouchEventType::MOVED:
    
                break;
                
            case cocos2d::ui::Widget::TouchEventType::ENDED:
            panel->setVisible( false );
                break;
                
            case cocos2d::ui::Widget::TouchEventType::CANCELED:
                
                break;
                
            default:
                break;
        }
    }

    Header

    void Confirm( Ref *sender, cocos2d::ui::Widget::TouchEventType type );
    
    private:
    cocos2d::ui::Button * confirmbutton;

    and this sort of setup works just fine with regular buttons but doesnt seem to work when the button is inside of a panel?

  • Sonar Systems admin
    Likes 0

    Try getting the panel instead of the button then get the child from the panel.

  • warning717
    Likes 0

    So I get the panel by its tag and then get child by tag?

    panel = ( cocos2d::ui::Layout * )rootNode->getChildByTag( 20 );
        confirmbutton->getChildByTag(25);
        cancelbutton->getChildByTag(26);

     

  • Sonar Systems admin
    Likes 0

    confirmbutton = panel->getChildByTag(25);
  • Sonar Systems admin
    Likes 0

    try that

  • warning717
    Likes 0

    I got an error, Invalid converison from ‘cocos2d::Node*’ to ‘cococs2d::ui::Button*’ [-fpermissive]

    Im assuming because im still using touch event listener actions

    //Confirm
    void MainMenu::Confirm( Ref *sender, cocos2d::ui::Widget::TouchEventType type )
    {
        switch ( type )
        {
            case cocos2d::ui::Widget::TouchEventType::BEGAN:
                
                break;
                
            case cocos2d::ui::Widget::TouchEventType::MOVED:
    
                break;
                
            case cocos2d::ui::Widget::TouchEventType::ENDED:
            //hide panel
            panel->setVisible( false );
                break;
                
            case cocos2d::ui::Widget::TouchEventType::CANCELED:
                
                break;
                
            default:
                break;
        }
    }

     

  • warning717
    Likes 0

    In my Header i have the following under private:

        cocos2d::ui::Button * confirmbutton;

    That might also play a role in this issue

  • Sonar Systems admin
    Likes 0

    Could do, remove it or change the name.

  • warning717
    Likes 0

    So I cant remove it or else then I get ‘confirmbutton’ was not declared in this scope.

    Ive tried changing it to the following with no luck. The error is usually an invalid conversion from ‘cocos2d::Node’ to ‘cocos2d::ui::Button’ or that Node doesnt contain Button

    //From "cocos2d::ui::Button * confirmbutton;"
    //to
    
    cocos2d::ui::Layout * confirmbutton;
    
    cocos2d::ui::Layout::Button * confirmbutton;
    
    cocos2d::ui::Button * confirmbutton;
    
    cocos2d::ui::Node * confirmbutton;
    
    cocos2d::ui::Node::Button * confirmbutton;
    
    cocos2d::Button * confirmbutton;
    
    cocos2d::Layout * confirmbutton;

     

  • Sonar Systems admin
    Likes 0

    Could you show us the button code for converting it.

  • warning717
    Likes 0

    CPP
        panel = ( cocos2d::ui::Layout * )rootNode->getChildByTag( 20 );
        confirmbutton = panel->getChildByTag(25);
        confirmbutton->addTouchEventListener(CC_CALLBACK_2(MainMenu::touchEventConfirm, this));
    
    void MainMenu::touchEventConfirm( Ref *sender, cocos2d::ui::Widget::TouchEventType type )
    {
        switch ( type )
        {
            case cocos2d::ui::Widget::TouchEventType::BEGAN:
                
                break;
                
            case cocos2d::ui::Widget::TouchEventType::MOVED:
    
                break;
                
            case cocos2d::ui::Widget::TouchEventType::ENDED:
            //show panel
            panel->setVisible( false );
                break;
                
            case cocos2d::ui::Widget::TouchEventType::CANCELED:
                
                break;
                
            default:
                break;
        }
    }
    

     

  • Sonar Systems admin
    Likes 0

    Try changing this

     confirmbutton = panel->getChildByTag(25);

     

    to

     

     

     confirmbutton = (cocos2d::ui::Button *)panel->getChildByTag(25);

     

  • warning717
    Likes 1

    Perfect! Thanks again smiley

  • Sonar Systems admin
    Likes 0

    Your very welcome :D

Login to reply