Cocos2d-x初学指南(6): 模拟一个触摸摇杆做为虚拟按键

来源:未知 责任编辑:智问网络 发表时间:2013-09-02 11:48 点击:

这类方法很多,网上一大堆,我给大家找了一个,但是这个还没完善如何判断按键是左还是右。于是我加了点自己的笨方法,有好的方法大家评论指出,谢谢

 \

 [cpp] 
#include "cocos2d.h"  
using namespace cocos2d;  
   
class Joystick :  
    public CCLayer  
 [cpp] 
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);  
}; 
[cpp] 
#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;    

 下面给大家调用这个东西的效果

 \


//判断方向
 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");
 }

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • cocos2d-x学习笔记(19)--label 、label atlas
    • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
    • Cocos2d-x学习(一):HelloWorld
    • cocos2dx在xcode下开发,编译到android上(2)
    • cocos2d 设置屏幕默认方向
    • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
    • Cocos2d-x 2.0 之 Actions “三板斧” 之一
    • cocos2d-x学习笔记(18)--游戏打包(windows平台)
    • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1