Skip to content

Commit 34c2787

Browse files
committed
update two class
Joystick & ScrollingBackground
1 parent 4ffd063 commit 34c2787

File tree

6 files changed

+691
-0
lines changed

6 files changed

+691
-0
lines changed

Joystick/code/Joystick.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "Joystick.h"
2+
3+
Joystick* Joystick::create(const char *fnBg,float bgRadius,const char *fnHandle,float handleRadius)
4+
{
5+
Joystick *joystick = new Joystick();
6+
if (joystick && joystick->init(fnBg,bgRadius,fnHandle,handleRadius))
7+
{
8+
joystick->autorelease();
9+
return joystick;
10+
}
11+
12+
CC_SAFE_DELETE(joystick);
13+
return NULL;
14+
}
15+
16+
bool Joystick::init(const char *fnBg,float bgRadius,const char *fnHandle,float handleRadius)
17+
{
18+
//创建底盘精灵
19+
m_bg = CCSprite::create(fnBg);
20+
if(!m_bg)
21+
return false;
22+
this->addChild(m_bg);
23+
24+
//创建摇杆精灵
25+
m_handle = CCSprite::create(fnHandle);
26+
if(!m_handle)
27+
return false;
28+
this->addChild(m_handle);
29+
30+
//初始化参数
31+
m_touchEventListener = 0;
32+
m_touchEventSelector = 0;
33+
m_bgRadius = bgRadius;
34+
m_handleRadius = handleRadius;
35+
m_handlePos = ccp(0.0f,0.0f);
36+
37+
//设置底盘的大小
38+
float bgDiameter = bgRadius * 2;
39+
CCSize oriBgSize = m_bg->getContentSize();
40+
m_bg->setScaleX(bgDiameter / oriBgSize.width);
41+
m_bg->setScaleY(bgDiameter / oriBgSize.height);
42+
43+
//设置摇杆的大小
44+
float handleDiameter = handleRadius * 2;
45+
CCSize oriHandleSize = m_handle->getContentSize();
46+
m_handle->setScaleX(handleDiameter / oriHandleSize.width);
47+
m_handle->setScaleY(handleDiameter / oriHandleSize.height);
48+
49+
//设置定时器
50+
this->schedule(schedule_selector(Joystick::callHandleEvent));
51+
52+
return true;
53+
54+
}
55+
56+
bool Joystick::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
57+
{
58+
CCPoint point = this->convertTouchToNodeSpaceAR(pTouch);
59+
60+
//判断触点是否在摇杆上
61+
if(point.x*point.x+point.y*point.y < m_handleRadius*m_handleRadius)
62+
return true;
63+
else
64+
return false;
65+
}
66+
67+
void Joystick::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
68+
{
69+
//将摇杆限制在底盘的范围内
70+
CCPoint point = this->convertTouchToNodeSpaceAR(pTouch);
71+
72+
if(point.x*point.x+point.y*point.y > m_bgRadius*m_bgRadius)
73+
{
74+
point = point.normalize();
75+
point = point * m_bgRadius;
76+
}
77+
78+
m_handle->setPosition(point);
79+
}
80+
81+
void Joystick::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
82+
{
83+
m_handle->setPosition(ccp(0.0f,0.0f));
84+
}
85+
86+
void Joystick::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
87+
{
88+
m_handle->setPosition(ccp(0.0f,0.0f));
89+
}
90+
91+
void Joystick::onEnter()
92+
{
93+
CCNode::onEnter();
94+
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
95+
}
96+
97+
void Joystick::onExit()
98+
{
99+
CCNode::onExit();
100+
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
101+
}
102+
103+
void Joystick::callHandleEvent(float interval)
104+
{
105+
//调用摇杆事件处理方法
106+
CCPoint point = m_handle->getPosition();
107+
108+
if(m_touchEventListener && m_touchEventSelector)
109+
(m_touchEventListener->*m_touchEventSelector)(interval,point.x/m_bgRadius,point.y/m_bgRadius);
110+
}

Joystick/code/Joystick.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#ifndef __JOYSTICK_H__
2+
#define __JOYSTICK_H__
3+
4+
#include "cocos2d.h"
5+
USING_NS_CC;
6+
7+
typedef void (CCObject::*SEL_JoystickEvent)(float interval,float x, float y);
8+
#define joystickEvent_selector(_SELECTOR) (SEL_JoystickEvent)(&_SELECTOR)
9+
10+
/**
11+
* 操纵杆,背景和按钮都是是精灵类,它们的锚点一直都是ccp(0.5,0.5)
12+
* 可以用setHandleEventListener方法设置摇杆摇动事件的处理
13+
*/
14+
class Joystick : public CCNode,public CCTargetedTouchDelegate
15+
{
16+
public:
17+
Joystick(){}
18+
virtual ~Joystick(){}
19+
20+
/**
21+
* 创建一个摇杆实例的静态方法
22+
*@param fnBg 背景图片的文件名,用来创建摇杆的底盘精灵
23+
*@param bgRadius 摇杆的底盘的半径
24+
*@param fnHandle 摇杆图片的文件名,用来创建摇杆精灵
25+
*@param handleRadius 摇杆的半径
26+
*/
27+
static Joystick* create(const char *fnBg, float bgRadius,
28+
const char *fnHandle, float handleRadius);
29+
30+
bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
31+
void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
32+
void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
33+
void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
34+
35+
void onEnter();
36+
void onExit();
37+
38+
///设置摇杆移动时要调用的方法,这个方法的声明为void f(float interval, float x, float y),interval是时间间隔,传入的x、y的范围都是0-1.0F
39+
void setHandleEventListener(CCObject *target, SEL_JoystickEvent selector);
40+
41+
///这个方法每一帧都被调用,如果设置了摇杆事件的处理的话他会调用哪个处理方法
42+
void callHandleEvent(float interval);
43+
protected:
44+
CCSprite* m_bg; ///<底盘的精灵
45+
CCSprite* m_handle; ///<摇杆的精灵
46+
47+
float m_bgRadius; ///<底盘的半径
48+
float m_handleRadius; ///<摇杆的半径
49+
50+
CCPoint m_handlePos; ///<摇杆在底盘坐标系的坐标
51+
52+
CCObject* m_touchEventListener;
53+
SEL_JoystickEvent m_touchEventSelector;
54+
55+
bool init(const char *fnBg, float bgRadius,
56+
const char *fnHandle, float handleRadius);
57+
58+
};
59+
60+
inline void Joystick::setHandleEventListener(CCObject *target, SEL_JoystickEvent selector)
61+
{
62+
m_touchEventListener = target;
63+
m_touchEventSelector = selector;
64+
}
65+
66+
#endif

Joystick/help.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class HelloWorld : public cocos2d::CCLayer
2+
3+
{
4+
5+
public:
6+
7+
virtual bool init();
8+
9+
10+
static cocos2d::CCScene* scene();
11+
12+
void menuCloseCallback(CCObject* pSender);
13+
14+
15+
//摇杆事件处理
16+
void moveSpr(float interval,float x , float y);
17+
18+
19+
CREATE_FUNC(HelloWorld);
20+
21+
22+
23+
private:
24+
25+
CCSprite* m_pS;
26+
27+
};
28+
29+
30+
//创建摇杆实例的代码片段
31+
Joystick* j = Joystick::create("CloseNormal.png",40,"CloseNormal.png",15);
32+
33+
j->setPosition(ccp(40,40));
34+
35+
j->setHandleEventListener(this,joystickEvent_selector(HelloWorld::moveSpr));
36+
37+
this->addChild(j);
38+
39+
//摇杆事件处理
40+
void HelloWorld::moveSpr(float interval,float x , float y)
41+
42+
{
43+
44+
CCPoint pos = m_pS->getPosition();
45+
46+
pos.x+=x*.5;
47+
48+
pos.y+=y*.5;
49+
50+
m_pS->setPosition(pos);
51+
52+
}

0 commit comments

Comments
 (0)