Cocos2d-x
【Bean Run】
Bean & parkour
【Game link】
【Github】
【Particle System】
Particle effects system editor, which can switch effect images, parameters, and use special spark effect
【Github】
【Box 2D】
Game levels made with box 2D
【Github】
【Note】
Cocos2d-x only includes basic library, let’s see some of them
Button event:
register during initialization phasebuttonEventHelloWorldScene.cpp 1
2
3// Button
c3btn.playbtn = dynamic_cast<Button*>(rootNode->getChildByName("PlayBtn"));//指定按鈕
c3btn.playbtn->addTouchEventListener(CC_CALLBACK_2(HelloWorld::PlayBtnTouchEvent, this));implement button event
buttonEventHelloWorldScene.cpp 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20void HelloWorld::PlayBtnTouchEvent(Ref *pSender, Widget::TouchEventType type)
{
switch (type)
{
case Widget::TouchEventType::BEGAN:
log("Touch Down");
break;
case Widget::TouchEventType::MOVED:
log("Touch Move");
break;
case Widget::TouchEventType::ENDED:
log("Touch Up");
break;
case Widget::TouchEventType::CANCELED:
log("Touch Cancelled");
break;
default:
break;
}
}Particle Effect: use in used list and free list to manage
CParticleSystemCParticleSystem.h 1
2list<CParticle*> _FreeList;
list<CParticle*> _InUsedList;initialize
CParticleSystemCParticleSystem.cpp 1
2
3
4
5
6
7
8
9
10
11
12void CParticleSystem::init(cocos2d::Layer &inlayer)
{
_iFree = NUMBER_PARTICLES;
_iInUsed = 0;
_pParticles = new CParticle[NUMBER_PARTICLES];
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("particletexture.plist");
for (int i = 0; i < NUMBER_PARTICLES; i++) {
_pParticles[i].setParticle("flare.png", inlayer);
_FreeList.push_front(&_pParticles[i]);
}
}spawn particle and update list, check particle lifetime, and put the ones reach lifetime into free list
CParticleSystemCParticleSystem.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
33void CParticleSystem::doStep(float dt)
{
CParticle *get;
list <CParticle *>::iterator it;
if (_bEmitterOn) {
int n = (int)(_fElpasedTime * _iNumParticles);
if (n > _iGenParticles) {
for (int i = 0; i < n - _iGenParticles; i++) {
if (_iFree != 0) {
if (_iEmitterType == 0) {
get = _FreeList.front();
_FreeList.pop_front();
_InUsedList.push_front(get);
_iFree--; _iInUsed++;
}
}
}
_iGenParticles = n;
}
}
if (_iInUsed != 0) {
for (it = _InUsedList.begin(); it != _InUsedList.end(); ) {
if ((*it)->doStep(dt)) {
_FreeList.push_front((*it));
it = _InUsedList.erase(it);
_iFree++; _iInUsed--;
}
else it++;
}
}
}Joint: set multiple objects and bind them together
jointJointScene.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
26auto circleSprite = _csbRoot->getChildByName("circle01_pul");
Point locA = circleSprite->getPosition();
Size size = circleSprite->getContentSize();
float scale = circleSprite->getScale();
b2CircleShape circleShape;
circleShape.m_radius = size.width*0.5f*scale / PTM_RATIO;
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(locA.x / PTM_RATIO, locA.y / PTM_RATIO);
bodyDef.userData = circleSprite;
b2Body* bodyA = _b2World->CreateBody(&bodyDef);
b2FixtureDef fixtureDef;
fixtureDef.shape = &circleShape;
fixtureDef.density = 10.0f;
fixtureDef.friction = 0.2f;
bodyA->CreateFixture(&fixtureDef);
b2PulleyJointDef JointDef;
JointDef.Initialize(bodyA, bodyB,
b2Vec2( locA.x / PTM_RATIO, (locA.y +150) / PTM_RATIO),
b2Vec2( locB.x / PTM_RATIO, (locB.y +150) / PTM_RATIO),
bodyA->GetWorldCenter(),
bodyB->GetWorldCenter(),
1);
_b2World->CreateJoint(&JointDef);