在Windows7上使用Cocos2d-x 3.4。
如何更改" MenuItemImage"鼠标光标焦点上的图像? (在Windows上)
**我想要"鼠标悬停(悬停)效果"到MenuItemImage。 **
我知道,如何创造"不触摸"或者"点击"按钮。
auto button = MenuItemImage::create(
"button_normal.png",
"button_pressed.png",
[](Ref* ref){
// do anything if clicking
});
auto menuButton = Menu::create(button, NULL);
auto winSize = Director::getInstance()->getWinSize();
menuButton->setPosition(Vec2(winSize.width / 2.0, winSize.height / 2.0));
this->addChild(menuButton);
但是这种方法不会改变焦点上的图像。
答案 0 :(得分:1)
创建EventListenerMouse以查找鼠标悬停事件。
//
// MouseOverMenuItem.h
// MouseOver
//
// Created by Baris Atamer on 3/15/15.
//
//
#ifndef __MouseOver__MouseOverMenuItem__
#define __MouseOver__MouseOverMenuItem__
#include "cocos2d.h"
USING_NS_CC;
class MouseOverMenuItem : public MenuItemImage
{
public:
~MouseOverMenuItem();
static MouseOverMenuItem * create(const std::string &normalImage, const std::string &selectedImage, const std::string &disabledImage, const ccMenuCallback& callback);
protected:
EventListenerMouse* mouseListener;
private:
void onMouseMove(Event *event);
void setMouseListener();
};
#endif /* defined(__MouseOver__MouseOverMenuItem__) */
//
// MouseOverMenuItem.cpp
// MouseOver
//
// Created by Baris Atamer on 3/15/15.
//
//
#include "MouseOverMenuItem.h"
MouseOverMenuItem::~MouseOverMenuItem()
{
_eventDispatcher->removeEventListener(mouseListener);
}
MouseOverMenuItem* MouseOverMenuItem::create(const std::string &normalImage, const std::string &selectedImage, const std::string &disabledImage, const ccMenuCallback &callback)
{
MouseOverMenuItem *ret = new (std::nothrow) MouseOverMenuItem();
if (ret && ret->initWithNormalImage(normalImage, selectedImage, disabledImage, callback))
{
ret->setMouseListener();
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
void MouseOverMenuItem::onMouseMove(Event *event)
{
EventMouse* e = (EventMouse*)event;
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 locationInNode = convertToNodeSpace(Vec2(e->getCursorX(), e->getCursorY() + visibleSize.height ));
Rect r = Rect(0,0, getContentSize().width, getContentSize().height);
// Show selected image if mouse over
r.containsPoint(locationInNode) ? selected() : unselected();
}
void MouseOverMenuItem::setMouseListener()
{
// Create a mouse listener
mouseListener = EventListenerMouse::create();
mouseListener->onMouseMove = CC_CALLBACK_1(MouseOverMenuItem::onMouseMove, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
}
用法:
auto button = MouseOverMenuItem::create("button_normal.png", "button_pressed.png", "", [](Ref* ref){
// do anything if clicking
log("click!");
});
button->setPosition(Vec2(winSize.width*.5, winSize.height*.5) );
// create menu, it's an autorelease object
auto menu = Menu::create(button, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
答案 1 :(得分:0)
尝试将MenuItemImage添加到菜单。
auto button = MenuItemImage::create ...
auto menu = Menu::create(button , nullptr);
addChild(menu);