Cocos2d-x初学指南(6): 模拟一个触摸摇杆做为虚拟按键
这类方法很多,网上一大堆,我给大家找了一个,但是这个还没完善如何判断按键是左还是右。于是我加了点自己的笨方法,有好的方法大家评论指出,谢谢
本文出自小柒的博客http://blog.csdn.net/cq361106306
#include "cocos2d.h" using namespace cocos2d; class Joystick : public CCLayer {//本文出自小柒的博客http://blog.csdn.net/cq361106306
public: Joystick(void); ~Joystick(void); public: CCPoint centerPoint; // 摇杆中心 CCPoint currentPoint; // 摇杆当前位置 bool active; // 是否激活摇杆 float radius; // 摇杆半径 CCSprite *jsSprite; // 摇杆实例 //************************************ // Method: Active // FullName: Joystick::Active // Access: public // Returns: void // Qualifier: 设置摇杆功能可用 //************************************ void Active(); //************************************ // Method: Inactive // FullName: Joystick::Inactive // Access: public // Returns: void // Qualifier: 设置摇杆功能不可用 //************************************ void Inactive(); //************************************ // Method: getDirection // FullName: Joystick::getDirection // Access: public // Returns: cocos2d::CCPoint // Qualifier: 获取摇杆方向,这里返回的是单位向量 //************************************ CCPoint getDirection(); //************************************ // Method: getVelocity // FullName: Joystick::getVelocity // Access: public // Returns: float // Qualifier: 获取摇杆的力度 //************************************ float getVelocity(); //************************************ // Method: updatePos // FullName: Joystick::updatePos // Access: public // Returns: void // Qualifier: 刷新函数,交给日程管理器 // Parameter: ccTime dt //************************************ void updatePos(ccTime dt); //************************************ // Method: JoystickWithCenter // FullName: Joystick::JoystickWithCenter // Access: public static // Returns: Joystick* // Qualifier: 初始化摇杆 // Parameter: CCPoint aPoint 摇杆中心 // Parameter: float aRadius 摇杆半径 // Parameter: CCSprite * aJsSprite 摇杆控制点 // Parameter: CCSprite * aJsBg 摇杆背景 //************************************ static Joystick* JoystickWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg); Joystick* initWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg); // 以下是复写Touch响应函数 virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); LAYER_NODE_FUNC(Joystick); };
#include "myCircle.h" Joystick::Joystick(void) { } Joystick::~Joystick(void) { } void Joystick::updatePos(ccTime dt) { jsSprite->setPosition(ccpAdd(jsSprite->getPosition(), ccpMult(ccpSub(currentPoint, jsSprite->getPosition()), 0.5))); } void Joystick::Active() { if(!active) { active = true; schedule(schedule_selector(Joystick::updatePos)); // 添加刷新函数 CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false); // 添加触摸委托 } } void Joystick::Inactive() { if(active) { active = false; this->unschedule(schedule_selector(Joystick::updatePos)); // 删除刷新函数 CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); // 删除委托 } } bool Joystick::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { if(!active) return false; CCPoint touchPoint = pTouch->locationInView(pTouch->view()); touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); if(ccpDistance(touchPoint, centerPoint) > radius) return false; currentPoint = touchPoint; return true; } void Joystick::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) { CCPoint touchPoint = pTouch->locationInView(pTouch->view()); touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); if(ccpDistance(touchPoint, centerPoint) > radius) { currentPoint = ccpAdd(centerPoint, ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius)); } else{ currentPoint = touchPoint; } } void Joystick::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) { currentPoint = centerPoint; } CCPoint Joystick::getDirection() { return ccpNormalize(ccpSub(centerPoint, currentPoint)); } float Joystick::getVelocity() { return ccpDistance(centerPoint, currentPoint); } Joystick* Joystick:: JoystickWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg) { Joystick *jstick=Joystick::node(); jstick->initWithCenter(aPoint,aRadius,aJsSprite,aJsBg); return jstick; } Joystick* Joystick::initWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg) { active = false; radius = aRadius; centerPoint = aPoint; currentPoint = centerPoint; jsSprite = aJsSprite; jsSprite->setPosition(centerPoint); aJsBg->setPosition(centerPoint); this->addChild(jsSprite); this->addChild(aJsBg); return this; }
本文出自小柒的博客http://blog.csdn.net/cq361106306
下面给大家调用这个东西的效果
//判断方向
CCPoint direct=myCircle->getDirection();
CCPoint right=CCPoint(1,0);
CCPoint left=CCPoint(-1,0);
if(ccpAngle(direct,left)<0.707 &&myCircle->currentPoint.x>myCircle->centerPoint.x)
{
CCLog("left");
}
if(ccpAngle(direct,right)<0.707&&myCircle->currentPoint.x<myCircle->centerPoint.x)
{
CCLog("right");
}