cocos2dx学习记录---简单摇杆实现
效果图:
#ifndef _Rocker_H_
#define _Rocker_H_
#include "cocos2d.h"
USING_NS_CC;
//摇杆类
class Rocker : public Node
{
public:
static Rocker* create(std::string bgName,float bgRadius,std::string rokerNmae,float rockerRadius);//静态创建摇杆实例
bool init(std::string bgName,float bgRadius,std::string rokerNmae,float rockerRadius);//初始化方法
bool onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent);
void onTouchMoved(cocos2d::Touch *pTouch, cocos2d::Event *pEvent);
void onTouchEnded(cocos2d::Touch *pTouch, cocos2d::Event *pEvent);
void onTouchCancelled(cocos2d::Touch *pTouch, cocos2d::Event *pEvent);
void addMoveListener(Node *ref);//添加控制节点
void callHandleEvent(float interval);//摇杆控制回调事件
private:
Sprite* spriteBg,*spriteRocker;//背景精灵,摇杆控制精灵
float radiusBg,radiusRocker;//背景半径,摇杆半径
Rocker* mRocker;//摇杆实例
Node *control;//需要控制的节点
Point touchStart , tuchMove;//开始触控的坐标点,移动时的坐标点
bool isMove;//标记是否正在操作摇杆
};
#endif // !_Rocker_H_
#include "Rocker.h"
#include "math.h"
/* @param 背景纹理,背景半径,摇杆纹理,摇杆半径 */
Rocker* Rocker::create(std::string bgName,float bgRadius,std::string rokerNmae,float rockerRadius){
Rocker *rocker = new Rocker();
if(rocker && rocker->init(bgName,bgRadius,rokerNmae,rockerRadius)){
rocker -> autorelease();
return rocker;
}
CC_SAFE_DELETE(rocker);
return nullptr;
}
bool Rocker::init(std::string bgName,float bgRadius,std::string rokerNmae,float rockerRadius){
radiusBg = bgRadius;
radiusRocker = rockerRadius;
//实例化背景精灵
this -> spriteBg = Sprite::create(bgName);
if(!this -> spriteBg)
return false;
float scale = bgRadius * 2 / this -> spriteBg -> getContentSize().width;//按比例缩放到设置大小
this -> spriteBg -> setScale(scale);
this -> addChild(this -> spriteBg);
//实例化摇杆精灵
this -> spriteRocker = Sprite::create(rokerNmae);
if(!this -> spriteRocker)
return false;
scale = rockerRadius *2 / (this -> spriteRocker -> getContentSize()).width;//按比例缩放到设置大小
this -> spriteRocker -> setScale(scale);
this -> addChild(this -> spriteRocker);
//创建触摸监听事件
auto listener = EventListenerTouchOneByOne::create();
listener -> onTouchBegan = CC_CALLBACK_2(Rocker::onTouchBegan,this);
listener -> onTouchMoved = CC_CALLBACK_2(Rocker::onTouchMoved,this);
listener -> onTouchEnded = CC_CALLBACK_2(Rocker::onTouchEnded,this);
listener -> onTouchCancelled = CC_CALLBACK_2(Rocker::onTouchCancelled,this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this);
this->schedule(schedule_selector(Rocker::callHandleEvent));//开始定时回调
return true;
}
bool Rocker::onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent){
Point point = this->convertTouchToNodeSpaceAR(pTouch);
if(point.x * point.x + point.y * point.y < radiusRocker * radiusRocker){//判断触摸点是否在摇杆上
touchStart = point;
isMove = true;//标记开始控制摇杆
return true;
}else{
return false;
}
}
void Rocker::onTouchMoved(cocos2d::Touch *pTouch, cocos2d::Event *pEvent){
Point point = this->convertTouchToNodeSpaceAR(pTouch);
if(point.x * point.x + point.y * point.y < radiusBg * radiusBg){//限制摇杆不能超出背景半径
spriteRocker -> setPosition(point);
tuchMove = point;
}else{
/* float x = radiusBg * sin(atan(point.y / point.x));
float y = radiusBg * cos(atan(point.y / point.x));*/
point.normalize();
point = point * radiusBg;
tuchMove = point;
spriteRocker -> setPosition(point);
}
}
void Rocker::onTouchEnded(cocos2d::Touch *pTouch, cocos2d::Event *pEvent){
spriteRocker -> setPosition(Point(0,0));//将摇杆设置为起始位置
isMove = false;
}
void Rocker::onTouchCancelled(cocos2d::Touch *pTouch, cocos2d::Event *pEvent){
spriteRocker -> setPosition(Point(0,0));//将摇杆设置为起始位置
isMove = false;
}
void Rocker::addMoveListener(Node *ref){
control = ref;
}
void Rocker::callHandleEvent(float interval){
if(control && isMove){//拥有控制节点并且可以操作摇杆
Point refPoint = control -> getPosition();
control -> setPosition(refPoint.x + tuchMove.x / 10,tuchMove.y / 10 + refPoint.y);
}
}