cocos2d-x的初步学习十二之虚拟摇杆Joystick
2013-06-15 16:15
337 查看
分类:
cocos2dx学习 2013-06-15 16:15
963人阅读 评论(2)
收藏
举报
Cocos2d-x跨平台摇杆Joystick游戏
这篇文章中,我们讲下虚拟摇杆,一般游戏中都会有虚拟摇杆,看了下别人写的Joystick,发现版本都是比较老的了,一些api已经改变了,稍微修改了下。OK,上代码:
摇杆类头文件:
[cpp]
view plaincopy
#include "cocos2d.h"
using namespace cocos2d;
class HRocker :public CCLayer {
public :
//初始化 aPoint是摇杆中心 aRadius是摇杆半径 aJsSprite是摇杆控制点 aJsBg是摇杆背景
static HRocker* HRockerWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole);
//启动摇杆
void Active();
//解除摇杆
void Inactive();
CCPoint getDirection();
private:
HRocker * initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole);
CCPoint centerPoint;//摇杆中心
CCPoint currentPoint;//摇杆当前位置
bool active;//是否激活摇杆
float radius;//摇杆半径
CCSprite *jsSprite;
bool isFollowRole;//是否跟随用户点击
float getVelocity();
void updatePos(CCTime dt);
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
CREATE_FUNC(HRocker);
};
.cpp:
[cpp]
view plaincopy
#include "Joystick.h"
using namespace cocos2d;
void HRocker::updatePos(CCTime dt){
jsSprite->setPosition(ccpAdd(jsSprite->getPosition(),ccpMult(ccpSub(currentPoint, jsSprite->getPosition()),0.5)));
}
//启动摇杆
void HRocker::Active()
{
if (!active) {
active=true;
schedule(schedule_selector(HRocker::updatePos));//添加刷新函数
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0,false);
}else {
}
}
//解除摇杆
void HRocker::Inactive()
{
if (active) {
active=false;
this->unschedule(schedule_selector(HRocker::updatePos));//删除刷新
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);//删除委托
}else {
}
}
//摇杆方位
CCPoint HRocker::getDirection()
{
return ccpNormalize(ccpSub(centerPoint, currentPoint));
}
//摇杆力度
float HRocker::getVelocity()
{
return ccpDistance(centerPoint, currentPoint);
}
HRocker* HRocker:: HRockerWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole){
HRocker *jstick=HRocker::create();
jstick->initWithCenter(aPoint,aRadius,aJsSprite,aJsBg,_isFollowRole);
return jstick;
}
bool HRocker::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
if (!active)
return false;
this->setVisible(true);
CCPoint touchPoint = touch->getLocationInView();
touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);
if(!isFollowRole){
if (ccpDistance(touchPoint, centerPoint) > radius){
return false;
}
}
currentPoint = touchPoint;
if(isFollowRole){
centerPoint=currentPoint;
jsSprite->setPosition(currentPoint);
this->getChildByTag(88)->setPosition(currentPoint);
}
return true;
}
void HRocker::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
CCPoint touchPoint = touch->getLocationInView();
touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);
if (ccpDistance(touchPoint, centerPoint) > radius)
{
currentPoint =ccpAdd(centerPoint,ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius));
}else {
currentPoint = touchPoint;
}
}
void HRocker::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
currentPoint = centerPoint;
if(isFollowRole){
this->setVisible(false);
}
}
HRocker* HRocker::initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole){
isFollowRole =_isFollowRole;
active = false;
radius = aRadius;
if(!_isFollowRole){
centerPoint =aPoint;
}else{
centerPoint =ccp(0,0);
}
currentPoint = centerPoint;
jsSprite = aJsSprite;
jsSprite->setPosition(centerPoint);
aJsBg->setPosition(centerPoint);
aJsBg->setTag(88);
this->addChild(aJsBg);
this->addChild(jsSprite);
if(isFollowRole){
this->setVisible(false);
}
this->Active();//激活摇杆
return this;
}
OK,我们看下实现加载这个类:
[cpp]
view plaincopy
//摇杆
CCSprite *controlSprite=CCSprite::create("cen.png");
//摇杆背景
CCSprite *bgSprite=CCSprite::create("control_bg.png");
joystick=HRocker::HRockerWithCenter(ccp(200.0f,200.0f),60.0f ,controlSprite ,bgSprite,false);
this->addChild(joystick,1);
this->scheduleUpdate();
发现里面有个getDirection()方法,可以作为判断方向,大家可以试试,我们看下效果图:
相关文章推荐
- cocos2d-x的初步学习十二之虚拟摇杆Joystick
- cocos2d-x的初步学习十三之虚拟摇杆SneakyInput
- cocos2D 虚拟摇杆Joystick功能实现
- cocos2d-x 3.x学习之虚拟摇杆
- ios joystick 虚拟摇杆实现 ( 非 Cocos2d )
- 【iOS-cocos2d-X 游戏开发之十二】自定义Cocos2dx摇杆(增强Joystick),增加摇杆跟随用户触点作为摇杆坐标,让摇杆不再死板!
- 【iOS-cocos2d-X 游戏开发之十二】自定义Cocos2dx摇杆(增强Joystick),增加摇杆跟随
- [cocos2d-x教程之 Hero教你打灰机] 如何制作一个虚拟摇杆
- cocos2d-x的初步学习九
- cocos2d-x的初步学习二十三之模仿微信打飞机二
- quick-cocos2d-x 学习系列之十二 关于websocket
- Cocos2d-x 3.2 学习笔记(十)Joystick 搖杆控件
- 【Cocosd2d实例教程五】Cocos2d添加虚拟摇杆控制器
- 我的Cocos2d-x学习笔记(十二)内存管理
- Cocos2d-js3.3虚拟摇杆的实现
- Cocos2d-x《赵云要格斗》--虚拟摇杆控制精灵上下左右运动 推荐
- 小羊驼和你一起学习cocos2d-x之五(结合摇杆控制玩家走动)
- cocos2d-x的初步学习二
- Cocos2d-X 摇杆的使用(Joystick)
- cocos2d-x的初步学习二十一之iosandroid跨平台环境配置