【iOS-cocos2d-X 游戏开发之十二】自定义Cocos2dx摇杆(增强Joystick),增加摇杆跟随用户触点作为摇杆坐标,让摇杆不再死板!

 

本站文章均为 李华明Himi 原创,转载务必在明显处注明:
转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/iphone-cocos2dx/721.html

          ☞ 点击订阅 ☜
 本博客最新动态!及时将最新博文通知您!

对于虚拟摇杆在游戏开发中必不可少,Android方面的是由Himi自己实现封装的,大家可以移步到这里查看详细实现机制:

【Android游戏开发二十四】360°平滑游戏摇杆(触屏方向导航) 

那么在Cocos2d引擎已提供此摇杆类(Joystick),所以Himi也就懒得重写了,但是Cocos2dx中并没有封装,那么这里Himi给出Cocos2dx版的Joystick(HRocker类),并且Himi对此类添加了一个跟随用户触点作为摇杆坐标的功能!

这里不多说代码结构直接贴出源码,然后重点说下使用与方法参数,具体实现可以参考源码以及Android部分Himi的实现机制;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
//  HRocker.h
//  RockerPro
//
//  Created by Himi on 12-3-30.
//  Copyright (c) 2012年 Himi. All rights reserved.
//
 
#ifndef RockerPro_HRocker_h
#define RockerPro_HRocker_h
 
#ifndef HRocker_H
#define HRocker_H
#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();
 
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; //是否跟随用户点击
 
     CCPoint getDirection();
 
     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);
     LAYER_NODE_FUNC(HRocker);
};
#endif
 
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
//  HRocker.cpp
//  RockerPro
//
//  Created by Himi on 12-3-30.
//  Copyright (c) 2012年 Himi. All rights reserved.
//
#include "HRocker.h"
 
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)); //添加刷新函数
         CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate( this , 0, false ); //添加触摸委托
     } else {
     }
}
//解除摇杆
void   HRocker::Inactive()
{
     if (active) {
         active= false ;
         this ->unschedule(schedule_selector(HRocker::updatePos)); //删除刷新
         CCTouchDispatcher::sharedDispatcher()->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::node();
     jstick->initWithCenter(aPoint,aRadius,aJsSprite,aJsBg,_isFollowRole);
     return jstick;
}
bool HRocker::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
 
     if (!active)
         return false ;
     this ->setIsVisible( true );
     CCPoint touchPoint = touch->locationInView(touch->view());
     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);
     }

你可能感兴趣的:(游戏,android,float,引擎,layer,手机游戏)