查看原文或更新请移步到我的wiki: https://github.com/cheyiliu/All-in-One/wiki/cocos2d-x-3.3-018-joystick4cocos3.3
bool Joystick::init() { bool result = false; do { // 父类初始化 if (!Layer::init()) { break; } // joystick的背景 mJsBg = Sprite::create("joystick_bg.png"); if (nullptr == mJsBg) { break; } mJsBg->setposition(mJsPos); addChild(mJsBg); // joystick的中心点 mJsCenter = Sprite::create("joystick_center.png"); if (nullptr == mJsCenter) { break; } mJsCenter->setposition(mJsPos); addChild(mJsCenter); // touch event监听 auto touchListener = EventListenertouchOneByOne::create(); if (nullptr == touchListener) { break; } touchListener->setSwallowtouches(true); touchListener->ontouchBegan = CC_CALLBACK_2(Joystick::ontouchBegan,this); touchListener->ontouchmoved = CC_CALLBACK_2(Joystick::ontouchmoved,this); touchListener->ontouchended = CC_CALLBACK_2(Joystick::ontouchended,this); _eventdispatcher->addEventListenerWithSceneGraPHPriority(touchListener,this); result = true; } while (0); return result;}bool Joystick::ontouchBegan(touch *touch,Event *unused_event) { log("ontouchBegan"); auto point = touch->getLocation(); if (mJsCenter->getBoundingBox().containsPoint(point)) { // 若触摸点在joystick的中心点,则继续接受事件 return true; } // 否则不接受后续事件 return false;}voID Joystick::ontouchmoved(touch *touch,Event *unused_event) { log("ontouchmoved"); // 1. 获得角度, //第一象限是0,90度 //第二象限是90,180度 //第三象限是-90,-180度 //第四象限是-90,0度 Vec2 point = touch->getLocation(); double y = point.y - mJsPos.y; double x = point.x - mJsPos.x; double angle = atan2(y,x) * 180 / PI; log("------------------------------------ %f",angle); // 2. 更新joystick中心点位置,目的是想让中心点始终在它的背景图范围 // joystick背景图半径 double JsBgRadis = mJsBg->getContentSize().wIDth * 0.5; //触摸点到中心点的实际距离 double distanceOftouchPointToCenter = sqrt( pow(mJsPos.x - point.x,2) + pow(mJsPos.y - point.y,2)); if (distanceOftouchPointToCenter >= JsBgRadis) { //利用等比关系计算delta x y double deltX = x * (JsBgRadis / distanceOftouchPointToCenter); double deltY = y * (JsBgRadis / distanceOftouchPointToCenter); mJsCenter->setposition(Vec2(mJsPos.x + deltX,mJsPos.y + deltY)); } else { mJsCenter->setposition(point); } // 3. 分发joystick event JoystickEvent* JsEvent = JoystickEvent::create(); JsEvent->mAnagle = angle; Director::getInstance()->getEventdispatcher()->dispatchCustomEvent( JoystickEvent::EVENT_JOYSTICK,JsEvent);}voID Joystick::ontouchended(touch *touch,Event *unused_event) { log("ontouchended"); // 事件结束,还原joystick中心点位置 mJsCenter->setposition(mJsPos);}集成到项目 我以proj.linx为例 拷贝相关cpp .h到工程并在CMakeLists.txt中增加cpp
set(GAME_SRC Classes/AppDelegate.cpp Classes/HelloWorldScene.cpp Classes/Joystick.cpp #新增 ${PLATFORM_SPECIFIC_SRC})添加joystick到场景
auto joystick = Joystick::create();scene->addChild(joystick);注册监听joystick event
#include "Joystick.h" auto _Listener = EventListenerCustom::create(JoystickEvent::EVENT_JOYSTICK,[=](EventCustom* event){ JoystickEvent* Jsevent = static_cast<JoystickEvent*>(event->getUserData()); log("--------------got joystick event,%p,angle=%f",Jsevent,Jsevent->mAnagle); // do you business you'd like to }); _eventdispatcher->addEventListenerWithFixedPriority(_Listener,1);效果图
以上是内存溢出为你收集整理的cocos2d-x-3.3-018-游戏摇杆joystick源码分享全部内容,希望文章能够帮你解决cocos2d-x-3.3-018-游戏摇杆joystick源码分享所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)