头文件:
#ifndef__HROCKER_H__ #define__HROCKER_H__ #include"cocos2d.h" #include"cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; //用于标识摇杆与摇杆的背景 typedefenum{ tag_rocker,tag_rockerBG,}tagForHRocker; //用于标识摇杆方向 typedefenum{ rocker_stay,rocker_right,rocker_up,rocker_left,rocker_down,}tagDirecton; classHRocker:publiccocos2d::Layer { public: HRocker(void); ~HRocker(void); //创建摇杆(摇杆的操作题图片资源名,摇杆背景图片资源名,起始坐标) staticHRocker*createHRocker(constchar*rockerImageName,constchar*rockerBGImageName,Vec2position); //启动摇杆(显示摇杆、监听摇杆触屏事件) voidstartRocker(bool_isStopOther); //停止摇杆(隐藏摇杆,取消摇杆的触屏监听) voidstopRocker(); //判断控制杆方向,用来判断精灵上、下、左、右运动 introcketDirection; //当前人物行走方向,用来判断精灵的朝向,精灵脸朝右还是朝左 boolrocketRun; //触屏事件 virtualboolonTouchBegan(Touch*pTouch,cocos2d::Event*pEvent); virtualvoidonTouchMoved(Touch*pTouch,cocos2d::Event*pEvent); virtualvoidonTouchEnded(Touch*pTouch,cocos2d::Event*pEvent); CREATE_FUNC(HRocker); private: //自定义初始化函数 voidrockerInit(constchar*rockerImageName,constVec2position); //是否可操作摇杆 boolisCanMove; //获取当前摇杆与用户触屏点的角度 floatgetRad(Vec2pos1,Vec2pos2); //摇杆背景的坐标 Vec2rockerBGPosition; //摇杆背景的半径 floatrockerBGR; }; #endif
源文件:
#include"HRocker.h" constdoublePI=3.1415; USING_NS_CC; HRocker::HRocker(void) { rocketRun=false; } HRocker::~HRocker(void) { } //创建摇杆(摇杆的操作题图片资源名,摇杆背景图片资源名,起始坐标) HRocker*HRocker::createHRocker(constchar*rockerImageName,Vec2position) { HRocker*layer=HRocker::create(); if(layer) { layer->rockerInit(rockerImageName,rockerBGImageName,position); returnlayer; } CC_SAFE_DELETE(layer); returnNULL; } //自定义初始化函数 voidHRocker::rockerInit(constchar*rockerImageName,constVec2position) { Sprite*spRockerBG=Sprite::create(rockerBGImageName); spRockerBG->setPosition(position); spRockerBG->setVisible(false); addChild(spRockerBG,tag_rockerBG); Sprite*spRocker=Sprite::create(rockerImageName); spRocker->setPosition(position); spRocker->setVisible(false); addChild(spRocker,1,tag_rocker); rockerBGPosition=position; rockerBGR=spRockerBG->getContentSize().width*0.5;// rocketDirection=-1;//表示摇杆方向不变 } //启动摇杆(显示摇杆、监听摇杆触屏事件) voidHRocker::startRocker(bool_isStopOther) { Sprite*rocker=(Sprite*)this->getChildByTag(tag_rocker); rocker->setVisible(true); Sprite*rockerBG=(Sprite*)this->getChildByTag(tag_rockerBG); rockerBG->setVisible(true); autodispatcher=Director::getInstance()->getEventDispatcher(); autoTouchListener=EventListenerTouchOneByOne::create(); TouchListener->onTouchBegan=CC_CALLBACK_2(HRocker::onTouchBegan,this); TouchListener->onTouchMoved=CC_CALLBACK_2(HRocker::onTouchMoved,this); TouchListener->onTouchEnded=CC_CALLBACK_2(HRocker::onTouchEnded,this); TouchListener->setSwallowTouches(true); dispatcher->addEventListenerWithSceneGraPHPriority(TouchListener,this); } //停止摇杆(隐藏摇杆,取消摇杆的触屏监听) voidHRocker::stopRocker() { Sprite*rocker=(Sprite*)this->getChildByTag(tag_rocker); rocker->setVisible(false); Sprite*rockerBG=(Sprite*)this->getChildByTag(tag_rockerBG); rockerBG->setVisible(false); this->removeChildByTag(tag_rocker);this->removeChildByTag(tag_rockerBG); } //获取当前摇杆与用户触屏点的角度 floatHRocker::getRad(Vec2pos1,Vec2pos2) { floatpx1=pos1.x; floatpy1=pos1.y; floatpx2=pos2.x; floatpy2=pos2.y; //得到两点x的距离 floatx=px2-px1; //得到两点y的距离 floaty=py1-py2; //算出斜边长度 floatxie=sqrt(pow(x,2)+pow(y,2)); //得到这个角度的余弦值(通过三角函数中的店里:角度余弦值=斜边/斜边) floatcosAngle=x/xie; //通过反余弦定理获取到期角度的弧度 floatrad=acos(cosAngle); //注意:当触屏的位置Y坐标<摇杆的Y坐标,我们要去反值-0~-180 if(py2<py1) { rad=-rad; } returnrad; } Vec2getAngelePosition(floatr,floatangle){ returnPoint(r*cos(angle),r*sin(angle)); } //抬起事件 boolHRocker::onTouchBegan(Touch*pTouch,Event*pEvent) { Vec2Vec2=pTouch->getLocation(); Sprite*rocker=(Sprite*)this->getChildByTag(tag_rocker); if(rocker->getBoundingBox().containsPoint(Vec2)) { isCanMove=true; CCLOG("begin"); } returntrue; } //移动事件 voidHRocker::onTouchMoved(Touch*pTouch,Event*pEvent) { if(!isCanMove) { return; } Vec2Vec2=pTouch->getLocation(); Sprite*rocker=(Sprite*)this->getChildByTag(tag_rocker); //得到摇杆与触屏点所形成的角度 floatangle=getRad(rockerBGPosition,Vec2); //判断两个圆的圆心距是否大于摇杆背景的半径 if(sqrt(pow((rockerBGPosition.x-Vec2.x),2)+pow((rockerBGPosition.y-Vec2.y),2))>=rockerBGR) { //保证内部小圆运动的长度限制 rocker->setPosition(getAngelePosition(rockerBGR,angle)+Point(rockerBGPosition.x,rockerBGPosition.y)); // CCLOG("touch"); } else { //当没有超过,让摇杆跟随用户触屏点移动即可 rocker->setPosition(Vec2); //CCLOG("touch"); } //判断方向 if(angle>=-PI/4&&angle<PI/4) { rocketDirection=rocker_right; rocketRun=false; CCLOG("%d",rocketDirection); } elseif(angle>=PI/4&&angle<3*PI/4) { rocketDirection=rocker_up; CCLOG("%d",rocketDirection); } elseif((angle>=3*PI/4&&angle<=PI)||(angle>=-PI&&angle<-3*PI/4)) { rocketDirection=rocker_left; rocketRun=true; CCLOG("%d",rocketDirection); } elseif(angle>=-3*PI/4&&angle<-PI/4) { rocketDirection=rocker_down; CCLOG("%d",rocketDirection); } } //离开事件 voidHRocker::onTouchEnded(Touch*pTouch,Event*pEvent) { if(!isCanMove) { return; } Sprite*rockerBG=(Sprite*)this->getChildByTag(tag_rockerBG); Sprite*rocker=(Sprite*)this->getChildByTag(tag_rocker); rocker->stopAllActions(); rocker->runAction(MoveTo::create(0.08f,rockerBG->getPosition())); isCanMove=false; rocketDirection=rocker_stay; CCLOG("%d",rocketDirection); CCLOG("end"); }
说明:
原文链接:https://www.f2er.com/cocos2dx/342178.html