在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);
但是这种方法并不能改变焦点图像。
最佳答案
创建一个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);
关于android - Cocos2d-x更改光标焦点上的MenuItemImage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29047615/