根据HiMi的写的android摇杆
本站文章均为 李华明Himi 原创,转载务必在明显处注明:(作者新浪微博: @李华明Himi )
转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/iphone-cocos2dx/721.html
Cpp: #include "HeroRun.h" //更新遥感位置: void HeroRun::updatePos(float dt) { //ccpAdd= x1 + x2,y1 + y2; //ccpSub= x1- x2,y1 - y2; jsSprite->setPosition(ccpAdd(jsSprite->getPosition(), ccpMult(ccpSub(currentPoint,jsSprite->getPosition()),0.5))); } //启动遥感; void HeroRun::Active() { if(!active){ active= true; schedule(schedule_selector(HeroRun::updatePos),0.1f);//更新遥感位置; //触摸事件都是由touchDispatcher分发的:就是触摸事件分发器; CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);//添加触摸委托; } } /************************************************************************/ /* 对对象的写法,如果存在则删除,或者,如果不存在则创建; */ /************************************************************************/ //解除遥感: void HeroRun::Inactive() { if(active) { active= false; this->unschedule(schedule_selector(HeroRun::updatePos));//删除 CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);//删除 } } //遥感方向: CCPoint HeroRun::getDirection() { //ccpNormalize(v)// 返回v的标准化向量,就是长度为1,当前点减去中心点的方式获取方向; returnccpNormalize(ccpSub(centerPoint, currentPoint)); } //遥感力度://ccpDistance 点1到点2的距离; float HeroRun::getVelocity() { returnccpDistance(centerPoint, currentPoint); } //初始化遥感; HeroRun* HeroRun::HRockerWithCenter(CCPointaPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg, bool_isFollowRole){ HeroRun*jstick = new HeroRun; jstick->initWithCenter(aPoint,aRadius, aJsSprite, aJsBg, _isFollowRole); returnjstick; } //点中 bool HeroRun::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent) { if(!active) { returnfalse; } this->setVisible(true); CCPointtouchPoint = pTouch->getLocationInView();//点在窗口内; touchPoint= CCDirector::sharedDirector()->convertToGL(touchPoint);//转为GL坐标; if(!isFollowRole) { if(ccpDistance(touchPoint, centerPoint) > radius)//如果两点大于半径就取消; { returnfalse; } } currentPoint= touchPoint;//重设当前位置; if(isFollowRole) { centerPoint= currentPoint; jsSprite->setPosition(currentPoint); this->getChildByTag(88)->setPosition(currentPoint);//获取精灵设置当前位置; } returntrue; } //移动; void HeroRun::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent) { CCPointtouchPoint = pTouch->getLocationInView(); touchPoint= CCDirector::sharedDirector()->convertToGL(touchPoint); //ccpNormalize(ccpSub(touchPoint,centerPoint) 获取方向; if(ccpDistance(touchPoint,centerPoint) > radius)//大于半径获取当前点; { currentPoint= ccpAdd(centerPoint, ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)),radius)); } else { currentPoint= touchPoint; } } //移动结束: void HeroRun::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent) { currentPoint= centerPoint; if(isFollowRole) { this->setVisible(false); } } //初始化中心点; HeroRun* HeroRun::initWithCenter(CCPointaPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg, bool_isFollowRole) { isFollowRole= _isFollowRole; active= false; radius= aRadius; if(!_isFollowRole) { centerPoint= aPoint; } else { centerPoint= ccp(0, 0); } currentPoint= centerPoint; jsSprite= aJsSprite; jsSprite->setPosition(currentPoint); aJsBg->setPosition(centerPoint); aJsBg->setTag(88); this->addChild(aJsBg); this->addChild(jsSprite); if(isFollowRole) { this->setVisible(false); } this->Active();//激活遥感; returnthis; }
.h
#ifndef __HeroRun_H__ #define __HeroRun_H__ #include "cocos2d.h" USING_NS_CC; /************************************************************************/ /* 遥感实现; */ /************************************************************************/ class HeroRun:public CCLayer { public: staticHeroRun * HRockerWithCenter(CCPoint aPoint, float aRadius, CCSprite * aJsSprite, CCSprite * aJsBg, bool _isFollowRole); //启动遥感; voidActive(); //解除遥感; voidInactive(); private: HeroRun* initWithCenter(CCPoint aPoint, float aRadius, CCSprite *aJsSprite, CCSprite * aJsBg, bool_isFollowRole); CCPointcenterPoint;//遥感中心; CCPointcurrentPoint;//遥感当前位置; boolactive;//是否激活遥感; floatradius;//遥感半径; CCSprite* jsSprite; boolisFollowRole;//是否跟随用户点击; CCPointgetDirection();//获取方向; floatgetVelocity();//获取速率;遥感力度,从点1到点2的距离来算; voidupdatePos(float dt);//更新位置; //触摸事件; virtualbool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtualvoid ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); virtualvoid ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); }; #endif
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。