Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using 3D modals


Cocos2d-x version 3 supports an exciting new function called 3D modals. We can use and display 3D modals in Cocos2d-x. In this recipe, you will learn how to use 3D modals.

Getting ready

You have to add the 3D object data into your project and clean your project. The resource files present in the COCOS_ROOT/test/cpp-tests/Resources/Sprite3DTest folder are—body.png and girl.c3b

How to do it...

Let's try to display a 3D model and move it.

auto size = Director::getInstance()->getWinSize();

// create 3D modal
auto sprite3d = Sprite3D::create("res/girl.c3b");
sprite3d->setPosition(Vec2(size.width/2, 100));
this->addChild(sprite3d);

// action 3D modal
auto animation3d = Animation3D::create("res/girl.c3b");
auto animate3d = Animate3D::create(animation3d);
auto repeat = RepeatForever::create(animate3d);
sprite3d->runAction(repeat);

How it works...

You can create the 3D sprite from a 3D model in the same way as we made a 2D sprite and displayed it. The Placement method and...