大佬教程收集整理的这篇文章主要介绍了cocos2d 绕椭圆移动,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#ifndef __JumpGame__OvalInterval__ #define __JumpGame__OvalInterval__ #include "CCActionInterval.h"//包含系统延时类动作头文件 using namespace cocos2d; // 定义一个结构来包含确定椭圆的参数 typedef struct OvalConfig { //中心点坐标 Vec2 centerPosition; //椭圆a长半轴 float a; //椭圆b短半轴 float b; //是否逆时针运动 bool moveInAnticlockwise; //two zOrder std::pair<int,int> zOrder; } lOvalConfig; /** 以椭圆方式移动 */ class CC_DLL MoveOvalBy : public ActionInterval{ public: MoveOvalBy(); //用“动作持续时间”和“椭圆控制参数”初始化动作 bool initWithDuration(float t,const OvalConfig& c); virtual MoveOvalBy* clone() const override; virtual MoveOvalBy* reverse() const override; virtual void update(float t);//利用update函数来不断的设定坐标 virtual void startWithTarget(Node *target) override; public: //用“动作持续时间”和“椭圆控制参数”创建动作 static MoveOvalBy *create(float t,const OvalConfig& c); protected: OvalConfig _config; //x = a * cos(t) t = [0,2Pi] inline float getPositionXAtOval(float t ){//返回X坐标 //参数方程 if(_config.moveInAnticlockwise == falsE){ return _config.a * cos(6.2831852 * (1 - t)); }else{ return _config.a * cos(6.2831852 * t); } } //y = b * sin(t) t = [0,2Pi] inline float getPositionYAtOval(float t ){//返回Y坐标 //参数方程 if(_config.moveInAnticlockwise == falsE){ return _config.b * sin(6.2831852 * (1 - t)); }else{ return _config.b * sin(6.2831852 * t); } } private: CC_DISALLOW_COPY_AND_ASSIGN(MoveOvalBy); }; #endif
a等于b的时候就是圆形了。
有时候在游戏中稍微用上点数学知识感觉很爽。
调用如下:
auto size = this->getContentSize(); auto ball = Sprite::createWithSpriteFramename("defenceBall.png"); this->addChild(ball); ball->setPosition(Vec2(size.width * 0.5,size.height * 0.5) + Vec2(0,10)); OvalConfig config; config.a = 100; config.b = 20; config.centerPosition = ball->getPosition(); config.moveInAnticlockwise = true; config.zOrder = make_pair(-1,0); auto moveAction = MoveOvalBy::create(1.0,config); ball->runAction(RepeatForever::create(moveAction));
以上是大佬教程为你收集整理的cocos2d 绕椭圆移动全部内容,希望文章能够帮你解决cocos2d 绕椭圆移动所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。