点击时如何更改 Sprite 图像
How To Change A Sprite Image When Tapped
我知道这可能是有史以来最容易回答的问题之一,但我已经进行了一些搜索,但似乎找不到答案...当用户使用时如何更改精灵图像在 Cocos2d-x?
中点击它
我知道的唯一方法是使用这样的菜单图像:
auto box = MenuItemImage::create("box_untapped.png", "box_tapped.png");
但这只会在用户点击图像时改变图像。即使在他们松开按钮后我如何让它保持改变?
此代码不需要任何 menu/button/... 但 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);
希望有所帮助
我知道这可能是有史以来最容易回答的问题之一,但我已经进行了一些搜索,但似乎找不到答案...当用户使用时如何更改精灵图像在 Cocos2d-x?
中点击它我知道的唯一方法是使用这样的菜单图像:
auto box = MenuItemImage::create("box_untapped.png", "box_tapped.png");
但这只会在用户点击图像时改变图像。即使在他们松开按钮后我如何让它保持改变?
此代码不需要任何 menu/button/... 但 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);
希望有所帮助