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);
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.
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);