How do I animate MenuItemSprite when I click on It?

Hi guys, I recently started making games and ran into this problem. I’m making a clone of FlappyBird and I want that when you click on this sprite on the screen, it disappears smoothly and only then moves to a new scene. Please help me, I have already searched the entire Internet.

Does the FadeOut action not work with MenuItemSprite? If it and other actions don’t work with it, then use a ui::Button, ui::ImageView or Sprite instead to handle the clicks, because actions do work on them.

For instance:

auto* buttonSprite = Sprite::create(....);

auto* touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = [](Touch* touch, Event* event) {
    auto* target = event->getCurrentTarget();

    //Get the position of the current point relative to the button
    const auto locationInNode = target->convertToNodeSpace(touch->getLocation());
    const auto& s = target->getContentSize();
    const auto rect = cocos2d::Rect(0, 0, s.width, s.height);

    //Check the click area
    if (rect.containsPoint(locationInNode))
    {
        return true;
    }
    return false;
};

touchListener->onTouchEnd = [this, buttonSprite](Touch* touch, Event* event) {
    auto* fadeOut = FadeOut::create(duration);
    auto* sequence = Sequence::create(fadeOut, CallFunc::create[this](){
        Director::getInstance()->replaceScene(newScene);
    }), nullptr);
    buttonSprite->runAction(sequence);
};

buttonSprite->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, buttonSprite);
2 Likes

Thank you so much, it was that easy. You are my savior. :grinning: :grinning: :grinning: :muscle:t2:

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