由于cocos2dx的按钮创建过于复杂还不支持win32悬停功能
所以考虑自己封装一个
myButton.h
#pragma once
#pragma execution_character_set("utf-8")
#include"cocos2d.h"
#include"游戏数据.h"
using namespace std;
using namespace cocos2d;
class myButton:public Sprite
{
public:
myButton(string normalPath, string onPath, string downPath, Node* fatherNode, Point position);//普通myButton构造
//按下透明myButton构造
//音效myButton构造
std::function<void()> myButtonCall;
virtual ~myButton();
private:
string normalPath;
string onPath;
string downPath;
Rect rect;
Node* fatherNode;
Point position;
int mouseState;//0松开 1按下
int textureState;//0正常 1悬停
void update(float dt);
void setNoormalTexture();
void setOnTexture();
void setDownTexture();
};
myButton.cpp
#include "myButton.h"
myButton::myButton(string normalPath,string onPath, string downPath,Node* fatherNode, Point position)
{
this->normalPath = normalPath;
this->onPath = onPath;
this->downPath = downPath;
this->fatherNode = fatherNode;
this->position = position;
mouseState = 0;
textureState = 0;
initWithFile(normalPath);
autorelease();
setPosition(this->position);
fatherNode->addChild(this, 10);
auto fatherNodeRect = fatherNode->getBoundingBox();
rect = this->getBoundingBox();
rect.origin.x = rect.origin.x + fatherNodeRect.origin.x;
rect.origin.y = rect.origin.y + fatherNodeRect.origin.y;
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [=](Touch* touch, Event* event)
{
Point touchPosition = Director::getInstance()->convertToGL(touch->getLocationInView());
if (rect.containsPoint(touchPosition))
{
mouseState = 1;
setDownTexture();
return true;
}
return false;
};
listener->onTouchMoved = [=](Touch* touch, Event* event)
{
Point touchPosition = Director::getInstance()->convertToGL(touch->getLocationInView());
if (rect.containsPoint(touchPosition))
{
setDownTexture();
}
else
{
setNoormalTexture();
}
};
listener->onTouchEnded = [=](Touch* touch, Event* event)
{
Point touchPosition = Director::getInstance()->convertToGL(touch->getLocationInView());
if (rect.containsPoint(touchPosition))
{
myButtonCall();
}
mouseState = 0;
textureState = 0;
setNoormalTexture();
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
this->scheduleUpdate();//开启定时器
}
void myButton::update(float dt)
{
if (rect.containsPoint(游戏设置数据类::游戏设置数据.当前鼠标坐标) && mouseState == 0 && textureState == 0)
{
textureState = 1;
setOnTexture();
}
if (rect.containsPoint(游戏设置数据类::游戏设置数据.当前鼠标坐标) == false && textureState == 1)
{
textureState = 0;
setNoormalTexture();
}
}
void myButton::setNoormalTexture()
{
log("setNoormalTexture");
setTexture(normalPath);
}
void myButton::setOnTexture()
{
log("setOnTexture");
setTexture(onPath);
}
void myButton::setDownTexture()
{
log("setDownTexture");
setTexture(downPath);
}
myButton::~myButton()
{
}
创建过程
首先创建鼠标监听器 监控鼠标位置 因为要实现悬停功能 鼠标的坐标自己定义吧
auto listenerMouse = EventListenerMouse::create();
listenerMouse->onMouseMove = [&](EventMouse* event) {
游戏设置数据类::游戏设置数据.当前鼠标坐标.x = event->getCursorX();
游戏设置数据类::游戏设置数据.当前鼠标坐标.y = event->getCursorY();
//log("%f,%f", 游戏设置数据类::游戏设置数据.当前鼠标坐标.x, 游戏设置数据类::游戏设置数据.当前鼠标坐标.y);
//鼠标指针->setPosition(当前鼠标坐标);
};
_eventDispatcher->addEventListenerWithFixedPriority(listenerMouse, 1);
auto myButton1 = new myButton(
"picture/ui/biaoti/start1.png",
"picture/ui/biaoti/start2.png",
"picture/ui/biaoti/start3.png",
this, ccp(200, 300));
myButton1->myButtonCall = [=]()
{
log("我的按钮触发了");
};
可以在此基础上继续拓展其他功能
大家有什么意见欢迎评论。