#ifndef _HROCKER_H_ #define _HROCKER_H_ #include "cocos2d.h" USING_NS_CC; //用于标识摇杆与摇杆的背景 typedef enum tagForHRocker { tag_rocker, tag_rockerBG }; //用于标识摇杆方向 typedef enum{ rocker_stay, rocker_right, rocker_up, rocker_left, rocker_down, }tagDirecton; class HRocker:public Layer{ public: HRocker(void); ~HRocker(void); virtual bool init(); //创建摇杆(摇杆的操作题图片资源名,摇杆背景图片资源名,起始坐标) static HRocker* createHRocker(const char *rockerImageName,const char *rockerBGImageName,Point position); //启动摇杆(显示摇杆、监听摇杆触屏事件) void startRocker(bool _isStopOther); //停止摇杆(隐藏摇杆,取消摇杆的触屏监听) void stopRocker(); //判断控制杆方向,用来判断精灵上、下、左、右运动 int rocketDirection; //当前人物行走方向,用来判断精灵的朝向,精灵脸朝右还是朝左 bool rocketRun; void update(float dt); CREATE_FUNC(HRocker); EventListenerTouchOneByOne* listener; private: //自定义初始化函数 void rockerInit(const char* rockerImageName,const char* rockerBGImageName,Point position); //是否可操作摇杆 bool isCanMove; //获取当前摇杆与用户触屏点的角度 float getRad(Point pos1,Point pos2); //摇杆背景的坐标 Point rockerBGPosition; //摇杆背景的半径 float rockerBGR; //触屏事件 virtual bool onTouchBegan(Touch *pTouch, Event *pEvent); virtual void onTouchMoved(Touch *pTouch, Event *pEvent); virtual void onTouchEnded(Touch *pTouch, Event *pEvent); }; #endif
******************************************************
#include "HRocker.h"
#include "Hero.h"
const double PI=3.1415;
HRocker::HRocker(void)
{
rocketRun=false;
}
HRocker::~HRocker(void)
{
}
bool HRocker::init()
{
listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(HRocker::onTouchBegan,this);
listener->onTouchMoved = CC_CALLBACK_2(HRocker::onTouchMoved,this);
listener->onTouchEnded = CC_CALLBACK_2(HRocker::onTouchEnded,this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this);
return true;
}
//创建摇杆(摇杆的操作题图片资源名,摇杆背景图片资源名,起始坐标)
HRocker* HRocker::createHRocker(const char *rockerImageName,const char *rockerBGImageName,Point 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,Point position)
{
Sprite *spRockerBG = Sprite::create(rockerBGImageName);
spRockerBG->setPosition(position);
spRockerBG->setVisible(false);
addChild(spRockerBG,0,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;//表示摇杆方向不变
}
//启动摇杆(显示摇杆、监听摇杆触屏事件)
void HRocker::startRocker(bool _isStopOther)
{
Sprite *rocker = (Sprite*)this->getChildByTag(tag_rocker);
rocker->setVisible(true);
Sprite *rockerBG = (Sprite *)this->getChildByTag(tag_rockerBG);
rockerBG->setVisible(true);
//想在这里监听
}
//停止摇杆(隐藏摇杆,取消摇杆的触屏监听)
void HRocker::stopRocker()
{
Sprite *rocker = (Sprite *)this->getChildByTag(tag_rocker);
rocker->setVisible(false);
Sprite * rockerBG = (Sprite *)this->getChildByTag(tag_rockerBG);
rockerBG->setVisible(false);
//这里取消监听
_eventDispatcher->removeEventListener(listener);
}
//获取当前摇杆与用户触屏点的角度
float HRocker::getRad(Point pos1,Point 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;
}
Point getAngelePosition(float r,float angle){
return ccp(r*cos(angle),r*sin(angle));
}
//抬起事件
bool HRocker::onTouchBegan(Touch *pTouch, Event *pEvent)
{
Point point = pTouch->getLocation();
Sprite *rocker = (Sprite *)this->getChildByTag(tag_rocker);
if (rocker->boundingBox().containsPoint(point))
{
isCanMove = true;
CCLOG("begin");
}
return true;
}
//移动事件
void HRocker::onTouchMoved(Touch *pTouch, Event *pEvent)
{
if (!isCanMove)
{
return;
}
Point point = pTouch->getLocation();
Sprite *rocker = (Sprite *)this->getChildByTag(tag_rocker);
//得到摇杆与触屏点所形成的角度
float angle = getRad(rockerBGPosition,point);
//判断两个圆的圆心距是否大于摇杆背景的半径
if (sqrt(pow((rockerBGPosition.x - point.x),2) + pow((rockerBGPosition.y - point.y),2)) >= rockerBGR)
{
//保证内部小圆运动的长度限制
rocker->setPosition(ccpAdd(getAngelePosition(rockerBGR,angle),ccp(rockerBGPosition.x,rockerBGPosition.y)));
// CCLOG("touch");
}
else
{
//当没有超过,让摇杆跟随用户触屏点移动即可
rocker->setPosition(point);
//CCLOG("touch");
}
//判断方向
if(angle>=-PI/4&&angle<PI/4)
{
rocketDirection=rocker_right;
rocketRun=false;
CCLOG("%d",rocketDirection);
}
else if(angle>=PI/4&&angle<3*PI/4)
{
rocketDirection=rocker_up;
CCLOG("%d",rocketDirection);
}
else if((angle>=3*PI/4&&angle<=PI)||(angle>=-PI&&angle<-3*PI/4))
{
rocketDirection=rocker_left;
rocketRun=true;
CCLOG("%d",rocketDirection);
}
else if(angle>=-3*PI/4&&angle<-PI/4)
{
rocketDirection=rocker_down;
CCLOG("%d",rocketDirection);
}
}
//离开事件
void HRocker::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");
}
void HRocker::update(float dt)
{
if(isCanMove)
{
}
}
***********************HelloWord.h******************************
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include ".\HRocker.h"
//#include ".\Hero.h"
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
void menuCloseCallback(cocos2d::Ref* pSender);
CREATE_FUNC(HelloWorld);
void update(float dt);
private:
HRocker* rocker;
//Hero* hero;
};
#endif // __HELLOWORLD_SCENE_H__
*************************HelloWord.cpp***********************
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto Bg = Sprite::create("background_1.jpg");
Bg->setPosition(visibleSize.width/2,visibleSize.height/2);
this->addChild(Bg);
//添加摇杆
rocker = HRocker::createHRocker("Direction_bt.png","Direction_bc.png",ccp(110,60));//其中第一张图片为摇杆的按钮,第二张为背景
this->addChild(rocker,2);
rocker->startRocker(true);
//启动updata事件
this->scheduleUpdate();
return true;
}
//自己创建 一个精灵(英雄)类 吧 然后在这里引用
void HelloWorld::update(float dt)
{
//判断是否按下摇杆及其类型
switch(rocker->rocketDirection)
{
case 1:
hero->SetAnimation("run_animation.plist","run_animation.png","run_",8,rocker->rocketRun);//"run_"为run_animation.png集合图片中每张图片的公共名称部分
hero->setPosition(ccp(hero->getPosition().x+2,hero->getPosition().y)); //向右走
break;
case 2:
hero->SetAnimation("run_animation.plist","run_animation.png","run_",8,rocker->rocketRun);//"run_"为run_animation.png集合图片中每张图片的公共名称部分
hero->setPosition(ccp(hero->getPosition().x, hero->getPosition().y+2)); //向上走
break;
case 3:
hero->SetAnimation("run_animation.plist","run_animation.png","run_",8,rocker->rocketRun);//"run_"为run_animation.png集合图片中每张图片的公共名称部分
hero->setPosition(ccp(hero->getPosition().x-2,hero->getPosition().y)); //向左走
break;
case 4:
hero->SetAnimation("run_animation.plist","run_animation.png","run_",8,rocker->rocketRun);//"run_"为run_animation.png集合图片中每张图片的公共名称部分
hero->setPosition(ccp(hero->getPosition().x,hero->getPosition().y-2)); //向下走
break;
default:
hero->StopAnimation();//停止所有动画和运动
break;
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。