Proper way of changing scenes with a button touch event listener?

  • warning717
    Likes 0

    Problem Description

    Placing a scene change inside of a button touch event under TouchEventType::ENDED gives me an error

    Ive been able to pull it off by using a schedule selector where TouchEventType::ENDED had started the schedule with 0.1 secs and then the schedule would run the scene but this causes transitions to be choppy/not play/blank screen then show the next scene.

  • Sonar Systems admin
    Likes 0

    Don’t declare stuff inside a switch statement, do it before. Initialising inside is ok though.

  • warning717
    Likes 0

    So change this

    void MainMenu::touchEventPlay( 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:
            auto scene = LevelSelect::createScene( );
            Director::getInstance()->replaceScene( scene );
                break;
                
            case cocos2d::ui::Widget::TouchEventType::CANCELED:
                
                break;
                
            default:
                break;
        }
    }
    

    to this?

    void MainMenu::touchEventPlay( 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:
            
            Director::getInstance()->replaceScene( scene );
                break;
                
            case cocos2d::ui::Widget::TouchEventType::CANCELED:
                
                break;
                
            default:
                break;
        }
    }
    

    So if I place in the init

    auto scene = LevelSelect::createScene( );

    what goes in the header?

  • Sonar Systems admin
    Likes 0

    No replace it to this

    void MainMenu::touchEventPlay( Ref *sender, cocos2d::ui::Widget::TouchEventType type )
    {
        auto scene = LevelSelect::createScene( );
    
        switch ( type )
        {
            case cocos2d::ui::Widget::TouchEventType::BEGAN:
                
                break;
                
            case cocos2d::ui::Widget::TouchEventType::MOVED:
    
                break;
                
            case cocos2d::ui::Widget::TouchEventType::ENDED:
                Director::getInstance()->replaceScene( scene );
    
                break;
                
            case cocos2d::ui::Widget::TouchEventType::CANCELED:
                
                break;
                
            default:
                break;
        }
    }

     

  • warning717
    Likes 0

    That works thank you

  • Sonar Systems admin
    Likes 0

    COOL :D

  • mrtnnora
    Likes 0

    It sounds like the issue may be related to the timing of the scene change and the button touch event. One possible solution could be to use a delay function within the button touch event to allow the button animation to complete before transitioning to the new scene. Here's an example code snippet that might work:

    void YourButton::onTouchEnded(Touch* touch, Event* event) { this->runAction(Sequence::create( DelayTime::create(0.2), // Add a delay to allow button animation to complete CallFunc::create([&]() { Director::getInstance()->replaceScene(NewScene::create()); }), nullptr )); }

     

    In this example, we're adding a delay of 0.2 seconds before calling the replaceScene function to transition to the new scene. You may need to adjust the delay time to fit the specific timing of your button animation.

    Alternatively, you could try using a different touch event, such as TouchEventType::BEGAN, to trigger the scene change. This would ensure that the transition occurs as soon as the button is touched, rather than waiting for the touch to end.

    I hope this helps! Let me know if you have any further questions.

Login to reply