1.
work with me the art and Planning said to give the protagonist and the protagonist Halo, around the oval movement. Cocos2d does not have, reference on the internet wrote a.
2. Elliptical Mathematics Knowledge
mathematical knowledge about ellipses I've forgotten it! Find some information on the Internet:
A is the long half of the ellipse, and B is the short half axis of the ellipse. O is the angle, the range is [0, 2π]. we need to know the position on the ellipse, and we can use the following formula:
3. Direct Code :
OvalActionInterval.h
#ifndef __jumpgame__ovalinterval__#define __jumpgame__ovalinterval__#include "CCActionInterval.h"// Contains system Delay class action header file using namespace cocos2d;//defines a structure to contain parameters that determine the ellipse typedef struct OVALCONFIG {//center point coordinates Vec2 Centerposition; Ellipse a long half shaft float A; Elliptic b short half shaft float B; Whether the anti-clockwise movement bool moveinanticlockwise; ZOrder std::p air<int, int> ZOrder;} lovalconfig;/** moves the */class cc_dll moveovalby:public Actioninterval{public:moveovalby () in an elliptical manner; Initialize the action with "Action duration" and "Ellipse control parameter" bool Initwithduration (float T, const ovalconfig& C); Virtual moveovalby* Clone () const override; Virtual moveovalby* reverse () const override; virtual void update (float t);//Use the update function to constantly set coordinates virtual void Startwithtarget (Node *target) Override;public://with "action Duration "and" Ellipse control parameter "Create action static Moveovalby *create (float t, const ovalconfig& C); Protected:ovalconfig _config; x = a * cos (t) t = [0, 2Pi] inline float getpositionxatoval (float t) {//return x coordinate//parametric equation 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) {//return y-coordinate//parametric equation 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
OvalActionInterval.cpp
#include "OvalActionInterval.h" Moveovalby::moveovalby () {}////moveovalby//moveovalby* moveovalby::create (float t, Const ovalconfig& C) {//using the parameters of the previously defined ellipse to initialize the ellipse moveovalby *action = new Moveovalby (); Action->initwithduration (t, c); Action->autorelease (); return action;} BOOL Moveovalby::initwithduration (float T, const ovalconfig& c) {if (Actioninterval::initwithduration (t)) {_ config = c; return true; } return false;} void Moveovalby::update (float t) {//t [0, 1]//log ("t:%f", t); if (_target) {float x = getpositionxatoval (t);//call before the coordinate calculation function to calculate the coordinate value of float y = getpositionyatoval (t); _target->setposition (_config.centerposition + Vec2 (x, y));//Since we draw the computed ellipse you do value is centered on the origin, so we need to add the center point coordinates if (t <= 0.5) {_target->setlocalzorder (_config.zorder.first); }else{_target->setlocalzorder (_config.zorder.second); }}}moveovalby* moveovalby::clone () const{Auto Action = new Moveovalby (); Action->initwithduration (_duration, _config); Action->autorelease (); return action;} moveovalby* moveovalby::reverse () const{ovalconfig newconfig; Newconfig.centerposition = _config.centerposition; NEWCONFIG.A = _CONFIG.A; newconfig.b = _config.b; Newconfig.moveinanticlockwise =!_config.moveinanticlockwise; Newconfig.zorder = _config.zorder; Return Moveovalby::create (_duration, newconfig);} void Moveovalby::startwithtarget (Node *target) {actioninterval::startwithtarget (target);}
Reference: http://blog.csdn.net/ufolr/article/details/7447773
I've also added zorder here, so there's a perspective.
A is a circle when it is equal to B.
Sometimes it's nice to have a little bit of knowledge in the game.
The call is as follows:
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, ten)); Ovalconfig config; CONFIG.A = +; CONFIG.B =; 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));
http://www.waitingfy.com/archives/1343
Cocos2d moving around an ellipse