头文件:
#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");}
说明:
总结以上是内存溢出为你收集整理的cocos2d-x 游戏开发之游戏控制器摇杆全部内容,希望文章能够帮你解决cocos2d-x 游戏开发之游戏控制器摇杆所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)