gpt4 book ai didi

android - Cocos2d-x 在光标焦点上更改 MenuItemImage

转载 作者:行者123 更新时间:2023-11-28 06:22:50 26 4
gpt4 key购买 nike

在 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);

enter image description here

关于android - Cocos2d-x 在光标焦点上更改 MenuItemImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29047615/

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