gpt4 book ai didi

c++ - 点击时如何更改 Sprite 图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:02:24 26 4
gpt4 key购买 nike

我知道这可能是有史以来最容易回答的问题之一,但我已经进行了一些搜索,但似乎无法找到答案...当用户点击时如何更改 Sprite 图像它在 Cocos2d-x 中?

我知道的唯一方法是使用这样的菜单图像:

auto box = MenuItemImage::create("box_untapped.png", "box_tapped.png");

但这只会在用户点击图像时改变图像。即使在他们松开按钮后我如何让它保持改变?

最佳答案

这段代码不需要任何菜单/按钮/...但是一个Touch Listener:

auto mySprite = Sprite("A.png");

auto touchListener = EventListenerTouchOneByOne::create();
///
touchListener->onTouchBegan = [=](Touch* touch, Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
if (rect.containsPoint(locationInNode))
{
mySprite->setTexture("B.png"); // Here
return true;
}
return false;
};

touchListener->onTouchEnded(Touch* touch, Event* event)
{
mySprite->setTexture("B.png"); // Or Here
}
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, mySprite);

希望有所帮助

关于c++ - 点击时如何更改 Sprite 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28225917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com