forked from lshain/SneakyInput-Cocos2dx-3.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SneakyJoystickSkinnedBase.cpp
105 lines (90 loc) · 2.22 KB
/
SneakyJoystickSkinnedBase.cpp
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
#include "SneakyJoystickSkinnedBase.h"
using namespace cocos2d;
SneakyJoystickSkinnedBase::~SneakyJoystickSkinnedBase()
{
if (backgroundSprite)
{
backgroundSprite->release();
backgroundSprite = NULL;
}
if (thumbSprite)
{
thumbSprite->release();
thumbSprite = NULL;
}
if (joystick)
{
joystick->release();
joystick = NULL;
}
}
bool SneakyJoystickSkinnedBase::init()
{
bool pRet = false;
if(CCLayer::init()){
this->backgroundSprite = NULL;
this->thumbSprite = NULL;
this->joystick = NULL;
this->schedule(schedule_selector(SneakyJoystickSkinnedBase::updatePositions));
pRet = true;
}
return pRet;
}
void SneakyJoystickSkinnedBase::updatePositions(float delta)
{
if(joystick && thumbSprite)
thumbSprite->setPosition(joystick->getStickPosition());
}
void SneakyJoystickSkinnedBase::setContentSize(CCSize s)
{
CCLayer::setContentSize(s);
backgroundSprite->setContentSize(s);
//joystick.joystickRadius = s.width/2;
}
void SneakyJoystickSkinnedBase::setBackgroundSprite(CCSprite *aSprite)
{
if(backgroundSprite){
if(backgroundSprite->getParent())
backgroundSprite->getParent()->removeChild(backgroundSprite, true);
backgroundSprite->release();
}
aSprite->retain();
backgroundSprite = aSprite;
if(aSprite){
this->addChild(backgroundSprite, 0);
this->setContentSize(backgroundSprite->getContentSize());
}
}
void SneakyJoystickSkinnedBase::setThumbSprite(CCSprite *aSprite)
{
if(thumbSprite){
if(thumbSprite->getParent())
thumbSprite->getParent()->removeChild(thumbSprite, true);
thumbSprite->release();
}
aSprite->retain();
thumbSprite = aSprite;
if(aSprite){
this->addChild(thumbSprite, 1);
//joystick->setThumbRadius(thumbSprite->getContentSize().width/2);
}
}
void SneakyJoystickSkinnedBase::setJoystick(SneakyJoystick *aJoystick)
{
if(joystick){
if(joystick->getParent())
joystick->getParent()->removeChild(joystick, true);
joystick->release();
}
aJoystick->retain();
joystick = aJoystick;
if(aJoystick){
this->addChild(aJoystick, 2);
if(thumbSprite)
joystick->setThumbRadius(thumbSprite->boundingBox().size.width/2);
else
joystick->setThumbRadius(0);
if(backgroundSprite)
joystick->setJoystickRadius(backgroundSprite->boundingBox().size.width/2);
}
}