前端之家收集整理的这篇文章主要介绍了
Cocos2d-3.x_创建3D精灵,播放模型动画,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Ball.h"
USING_NS_CC;
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld);
private:
};
#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
// 根据模型和纹理生成3D精灵
auto sprite = Sprite3D::create("tortoise.c3b");
sprite->setPosition(visibleSize / 2.0);
sprite->setScale(0.3f);
this->addChild(sprite);
// 给3D精灵自定义动作:在1秒钟之内,环绕x轴和y轴旋转45度,z轴不动
//sprite->runAction(RepeatForever::create(RotateBy::create(1.0f,Vec3(45,45,0))));
// 播放模型动画
auto animation = Animation3D::create("tortoise.c3b");
auto animate = Animate3D::create(animation);
// 播放一次模型动画
//sprite->runAction(animate);
// 永远播放模型动画
sprite->runAction(RepeatForever::create(animate));
return true;
}