*Source from http://www.cocos2d-x.org/
*Contributed By: hunter_hb
This tutorial is about how to create the new scene or layer transition which I called "Triangle Shape Cut Triansition". You must have seen it in some popular games. Let's begin.
The project is build on cocos2dx3.0. You can download the code in the attached files.
I supposed you have known how to create a new project in cocos2dx3.0.
Use Visual studio IDE to open the win32 project, the new project has 2 class ,AppDelegate and HelloWorldScene.
Create a new class GameScene.(Make sure the new class is locate in /Classes directory.) We will use HelloWorldScene and GameScene to show the "Triangle Shape Cut Triansition".
We add code at the end of "init" function in HelloWorldScene.cpp.
1
2
3
| auto action = Sequence::create(DelayTime::create(10), CallFunc::create(this, callfunc_selector(HelloWorld::transitionToGameScene)), NULL);
this->runAction(action);
|
Next we add a function "transitionToGameScene" for HelloWorld class. ( You can see the comments to help understande the code.)
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
| void HelloWorld::transitionToGameScene()
{
auto size = Director::getInstance()->getWinSize();
auto clipper = ClippingNode::create();
auto stencil = DrawNode::create();
Point triangle[3];
triangle[0] = Point(-size.width * 1.5f, -size.height / 2);
triangle[1] = Point(size.width * 1.5f, -size.height / 2);
triangle[2] = Point(0, size.height);
Color4F green(0, 1, 0, 1);
stencil->drawPolygon(triangle, 3, green, 0, green);
clipper->setAnchorPoint(Point(0.5f, 0.5f));
clipper->setPosition(size.width / 2, size.height / 2);
clipper->setStencil(stencil);
clipper->setInverted(true);
Sprite* blackRect = Sprite::create("black_screen.png");
clipper->addChild(blackRect);
this->addChild(clipper, 500);
stencil->runAction(EaseSineOut::create(Spawn::create(ScaleTo::create(2.5f, 0.0f, 0.0f), RotateBy::create(2.5f, 540),
Sequence::create(DelayTime::create(2.5), CallFunc::create(this, callfunc_selector(HelloWorld::toGameScene)), NULL), NULL)));
}
|
Next we add a function "toGameScene" for HelloWolrd class.
1
2
3
4
5
6
| void HelloWorld::toGameScene()
{
auto scene = GameScene::createScene();
Director::getInstance()->replaceScene(scene);
}
|
Next we modify the GameScene class. In the GameScene.h.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| class GameScene : public Layer
{
public:
GameScene(void);
~GameScene(void);
CREATE_FUNC(GameScene);
static Scene* createScene();
virtual bool init() override;
void toGameScene();
void startGame();
};
|
**In the GameScene.cpp "init" function **
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| bool GameScene::init()
{
if (!Layer::init())
{
return false;
}
auto size = Director::getInstance()->getWinSize();
auto background = Sprite::create("background.png");
background->setPosition(size.width/2, size.height/2);
this->addChild(background);
this->toGameScene();
return true;
}
|
In the GameScene.cpp "toGameScene" function.
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
| void GameScene::toGameScene()
{
auto size = Director::getInstance()->getWinSize();
auto clipper = ClippingNode::create();
auto stencil = DrawNode::create();
Point triangle[3];
triangle[0] = Point(-size.width * 1.5f, -size.height/2);
triangle[1] = Point(size.width * 1.5f, -size.height/2);
triangle[2] = Point(0, size.height);
Color4F green(0, 1, 0, 1);
stencil->drawPolygon(triangle, 3, green, 0, green);
clipper->setAnchorPoint(Point(0.5f, 0.5f));
clipper->setPosition(size.width/2, size.height/2);
clipper->setStencil(stencil);
clipper->setInverted(true);
Sprite* blackRect = Sprite::create("black_screen.png");
clipper->addChild(blackRect);
this->addChild(clipper, 500);
stencil->setScale(0.0f);
stencil->runAction(EaseSineIn::create(Spawn::create(ScaleTo::create(2.5f, 1.0f, 1.0f), RotateBy::create(2.5f, 540),
Sequence::create(DelayTime::create(1.0), CallFunc::create(this, callfunc_selector(GameScene::startGame)), NULL) , NULL)));
}
|
Không có nhận xét nào:
Đăng nhận xét