摇杆程序

#ifndef HROCKER_H

#define HROCKER_H



#include "cocos2d.h"



using namespace cocos2d;



//用于标识摇杆与摇杆的背景

typedef enum{

    tag_rocker,

    tag_rockerBG,

}tagForHRocker;





class HRocker:public CCLayer

{

public:

    HRocker(void);

    ~HRocker(void);

    

    //创建摇杆(摇杆的操作题图片资源名,摇杆背景图片资源名,起始坐标)

    static HRocker* createHRocker(const char *rockerImageName,const char *rockerBGImageName,CCPoint position);

    //启动摇杆(显示摇杆、监听摇杆触屏事件)

    void startRocker(bool _isStopOther);

    //停止摇杆(隐藏摇杆,取消摇杆的触屏监听)

    void stopRocker();

    CREATE_FUNC(HRocker);

private:

    //自定义初始化函数

    void rockerInit(const char* rockerImageName,const char* rockerBGImageName,CCPoint position);

    //是否可操作摇杆

    bool isCanMove;

    //获取当前摇杆与用户触屏点的角度

    float getRad(CCPoint pos1,CCPoint pos2);

    //摇杆背景的坐标

    CCPoint rockerBGPosition;

    //摇杆背景的半径

    float rockerBGR;

    //触屏事件

    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);



};



#endif
#include "HRocker.h"



HRocker::HRocker(void)

{

}



HRocker::~HRocker(void)

{

}



//创建摇杆(摇杆的操作题图片资源名,摇杆背景图片资源名,起始坐标)

HRocker* HRocker::createHRocker(const char *rockerImageName,const char *rockerBGImageName,CCPoint position)

{

    HRocker *layer = HRocker::create();

    if (layer)

    {

        layer->rockerInit(rockerImageName,rockerBGImageName,position);

        return layer;

    }

    CC_SAFE_DELETE(layer);

    return NULL;

}



//自定义初始化函数

void HRocker::rockerInit(const char* rockerImageName,const char* rockerBGImageName,CCPoint position)

{

    CCSprite *spRockerBG = CCSprite::create(rockerBGImageName);

    spRockerBG->setPosition(position);

    spRockerBG->setVisible(false);

    addChild(spRockerBG,0,tag_rockerBG);



    CCSprite *spRocker = CCSprite::create(rockerImageName);

    spRocker->setPosition(position);

    spRocker->setVisible(false);

    addChild(spRocker,1,tag_rocker);



    rockerBGPosition = position;



    rockerBGR = spRockerBG->getContentSize().width*0.5;

}



//启动摇杆(显示摇杆、监听摇杆触屏事件)

void HRocker::startRocker(bool _isStopOther)

{

    CCSprite *rocker = (CCSprite*)this->getChildByTag(tag_rocker);

    rocker->setVisible(true);



    CCSprite *rockerBG = (CCSprite *)this->getChildByTag(tag_rockerBG);

    rockerBG->setVisible(true);



    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-1,_isStopOther);

}



//停止摇杆(隐藏摇杆,取消摇杆的触屏监听)

void HRocker::stopRocker()

{

    CCSprite *rocker = (CCSprite *)this->getChildByTag(tag_rocker);

    rocker->setVisible(false);



    CCSprite * rockerBG = (CCSprite *)this->getChildByTag(tag_rockerBG);

    rockerBG->setVisible(false);



    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);

}





//获取当前摇杆与用户触屏点的角度

float HRocker::getRad(CCPoint pos1,CCPoint pos2)

{

    float px1 = pos1.x;

    float py1 = pos1.y;

    float px2 = pos2.x;

    float py2 = pos2.y;



    //得到两点x的距离

    float x = px2 - px1;

    //得到两点y的距离

    float y = py1 - py2;

    //算出斜边长度

    float xie = sqrt(pow(x,2) + pow(y,2));

    //得到这个角度的余弦值(通过三角函数中的店里:角度余弦值=斜边/斜边)

    float cosAngle = x / xie;

    //通过反余弦定理获取到期角度的弧度

    float rad = acos(cosAngle);

    //注意:当触屏的位置Y坐标<摇杆的Y坐标,我们要去反值-0~-180

    if (py2 < py1)

    {

        rad = -rad;

    }

    return rad;

}



CCPoint getAngelePosition(float r,float angle){

    return ccp(r*cos(angle),r*sin(angle));

}



//抬起事件

bool HRocker::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)

{

    CCPoint point = pTouch->getLocation();

    CCSprite *rocker = (CCSprite *)this->getChildByTag(tag_rocker);

    if (rocker->boundingBox().containsPoint(point))

    {

        isCanMove = true;

    }

    return true;

}

//移动事件

void HRocker::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)

{

    if (!isCanMove)

    {

        return;

    }

    CCPoint point = pTouch->getLocation();

    CCSprite *rocker = (CCSprite *)this->getChildByTag(tag_rocker);



    //判断两个圆的圆心距是否大于摇杆背景的半径

    if (sqrt(pow((rockerBGPosition.x - point.x),2) + pow((rockerBGPosition.y - point.y),2)) >= rockerBGR)

    {

        //得到摇杆与触屏点所形成的角度

        float angle = getRad(rockerBGPosition,point);

        //保证内部小圆运动的长度限制

        rocker->setPosition(ccpAdd(getAngelePosition(rockerBGR,angle),ccp(rockerBGPosition.x,rockerBGPosition.y)));

    }

    else

    {

        //当没有超过,让摇杆跟随用户触屏点移动即可

        rocker->setPosition(point);

    }

}

//离开事件

void HRocker::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)

{

    if (!isCanMove)

    {

        return;

    }

    CCSprite *rockerBG = (CCSprite*)this->getChildByTag(tag_rockerBG);

    CCSprite *rocker = (CCSprite*)this->getChildByTag(tag_rocker);

    rocker->stopAllActions();

    rocker->runAction(CCMoveTo::create(0.08,rockerBG->getPosition()));

    isCanMove = false;

}
// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    bool bRet = false;

    do 

    {

        HRocker* rocker = HRocker::createHRocker("rocker.png","rockerBG.png",ccp(240,160));

        addChild(rocker);

        rocker->startRocker(true);



        bRet = true;

    } while (0);



    return bRet;

}

 

摇杆程序

你可能感兴趣的:(程序)