Problem with using an action on button

I’m making an effect that changes the size of my button, but when I run the action for the button when the mouse is down on it, it doesn’t run the action. Instead, the game just crashes.
Here is my code:

menuplay->runAction(menuplay_grow);

“menuplay_grow” is defined here:

auto menuplay_grow = ScaleTo::create(2.0f, 3.0f, 3.0f);

What does the console say when it crashes?

Just out of curiosity, if you want btn to scaleup on press then you can use btn->setPressedActionEnabled(true);
if you are using ui::Button

2 Likes

oh yeah and also the rest of my code works perfectly fine

It says nothing in the console after it crashes, but what it does do is give an error on this line:

menuplay->runAction(menuplay_grow);

and the error says “Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)”
But even though an error is there, Xcode still lets me build the project

Can you please post more code so we can see what is going on? I know brevity is a thing but for us, more code is always better. Often times the error isn’t on the exact line of the crash.

oh also the animation DOES play but just for a split second before it crashes

auto menuplay_grow = ScaleTo::create(2.0f, 3.0f, 3.0f);
    auto menuplay_shrink = ScaleTo::create(2.0f, 0.27f, 0.27f);
    auto menuplay = cocos2d::ui::Button::create("playBtn1.png", "playBtn1.png", "playBtn1.png", cocos2d::ui::Button::TextureResType::PLIST); // They are all the same because I'm trying to do the animation with "menuplay_shrink" and "menuplay grow"
    
    menuplay->setPosition(Vec2(visibleSize.width * 0.5f, visibleSize.height * 0.5f));
    menuplay->setScale(0.27f, 0.27f);
    menuplay->setPressedActionEnabled(true);
    
    menuplay->addTouchEventListener([&](Ref* sender, cocos2d::ui::Widget::TouchEventType type){
            switch (type)
            {
                case ui::Widget::TouchEventType::BEGAN: // When mouse is down on button
                    break;
                case ui::Widget::TouchEventType::ENDED: // When button released
                    menuplay->runAction(menuplay_grow);
                default:
                    menuplay->runAction(menuplay_shrink);
            }
    });
    this->addChild(menuplay);

That is not correct. You need to be very careful when capturing variables outside the lambda method by reference ([&]) rather than by value ([=]). In addition to that, even if you did capture by value, any cocos2d::Ref objects you’ve created (like ScaleTo) would have gone out of scope and been auto-released, so any references to them would no longer be valid. If you’re not why that is important, then spend some time looking over the cocos2d::Ref object code and documentation (there’s plenty of it). Most objects used in Cocos2d inherit from cocos2d::Ref, so you need to understand it completely in order to avoid mistakes like this.

You’re also missing a break; statement in the case ui::Widget::TouchEventType::ENDED: block, so it’s just flowing down to the default block.

This should work (untested):

auto menuplay = cocos2d::ui::Button::create("playBtn1.png", "playBtn1.png", "playBtn1.png", cocos2d::ui::Button::TextureResType::PLIST); // They are all the same because I'm trying to do the animation with "menuplay_shrink" and "menuplay grow"

menuplay->setPosition(Vec2(visibleSize.width * 0.5f, visibleSize.height * 0.5f));
menuplay->setScale(0.27f, 0.27f);
menuplay->setPressedActionEnabled(true);

menuplay->addTouchEventListener([menuplay](Ref* sender, cocos2d::ui::Widget::TouchEventType type){
    switch (type)
    {
        case ui::Widget::TouchEventType::BEGAN: // When mouse is down on button
            break;
        case ui::Widget::TouchEventType::ENDED: // When button released
        {
            auto menuplay_grow = ScaleTo::create(2.0f, 3.0f, 3.0f);
            menuplay->runAction(menuplay_grow);
            break;
        }
        default:
        {
            auto menuplay_shrink = ScaleTo::create(2.0f, 0.27f, 0.27f);
            menuplay->runAction(menuplay_shrink);
            break;
        }
    }
});
this->addChild(menuplay);

Here is some info on lambdas, with a section on captures: https://en.cppreference.com/w/cpp/language/lambda

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.