本文主要是介绍cocos2d-x下实现摇杆,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现方法采至http://www.cocoachina.com/bbs/read.php?tid-16365-keyword-ҡ��.html
他的使用oc实现,我翻译成c++版的。。
Joystick.h文件
#ifndef Joystick_H
#define Joystick_H
#include "cocos2d.h"
using namespace cocos2d;
class Joystick :public CCLayer {
public :
CCPoint centerPoint;//摇杆中心
CCPoint currentPoint;//摇杆当前位置
bool active;//是否激活摇杆
float radius;//摇杆半径
CCSprite *jsSprite;
void Active();
void Inactive();
CCPoint getDirection();
float getVelocity();
void updatePos(ccTime dt);
//初始化 aPoint是摇杆中心 aRadius是摇杆半径 aJsSprite是摇杆控制点 aJsBg是摇杆背景
static Joystick* JoystickWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg);
Joystick * initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg);
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);
};
#endif
Joystick.cpp文件
#include "Joystick.h"
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);//添加触摸委托
}else {
}
}
//冻结摇杆
void Joystick::Inactive()
{
if (active) {
active=false;
this->unschedule(schedule_selector(Joystick::updatePos));//删除刷新
CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);//删除委托
}else {
}
}
bool Joystick::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
if (!active)
return false;
CCPoint touchPoint = touch->locationInView(touch->view());
touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);
if (ccpDistance(touchPoint, centerPoint) > radius)
return false;
currentPoint = touchPoint;
return true;
}
void Joystick::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
CCPoint touchPoint = touch->locationInView(touch->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* touch, CCEvent* event)
{
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;
}
最终效果图为
在helloworld 里面添加
CCSprite *testPointL=CCSprite::spriteWithFile("LeftPoint.png");//摇杆
CCSprite *testBGL=CCSprite::spriteWithFile("LeftPlane.png");//摇杆背景
Joystick *testJSL=Joystick::JoystickWithCenter(ccp(80.0f,80.0f),60.0f ,testPointL ,testBGL);
this->addChild(testJSL);
testJSL->Active();
这篇关于cocos2d-x下实现摇杆的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!