cocos2d-x 3.2 椭圆运动


直接上代码

//
//  ovalAction.h
//  LSWGameIOS
//
//  Created by lsw on 14-10-27.
//
//

#ifndef __LSWGameIOS__ovalAction__
#define __LSWGameIOS__ovalAction__

#include "cocos2d.h"

typedef struct ovalConfig {
    cocos2d::Vec2 centerPos;
    float a;
    float b;
    bool moveClockDir;
    std::pair<int,int> zOrder;
}LovalConfig;

class MoveovalBy : public cocos2d::ActionInterval {
public:
    MoveovalBy();
    ~MoveovalBy();
    
    bool initWithDuration(float dt,const ovalConfig& c);
    virtual MoveovalBy* clone() const override;
    virtual MoveovalBy* reverse() const override;
    virtual void update(float dt);
    virtual void startWithTarget(cocos2d::Node *target) override;
    
    static MoveovalBy* create(float dt,const ovalConfig& c);
    
protected:
    ovalConfig _config;
    
    inline float getPosXAtoval(float dt) {
        if (_config.moveClockDir) {
            return _config.a * cos(4 * MATH_PIOVER2 * dt);
        }
        return _config.a * cos(4 * MATH_PIOVER2 * (1 - dt));
    }
    
    inline float getPosYAtoval(float dt) {
        if (_config.moveClockDir) {
            return _config.b * sin(4 * MATH_PIOVER2 * dt);
        }
        return _config.b * sin(4 * MATH_PIOVER2 * (1 - dt));
    }
    
    
};

#endif /* defined(__LSWGameIOS__ovalAction__) */


cpp文件

//
//  ovalAction.cpp
//  LSWGameIOS
//
//  Created by lsw on 14-10-27.
//
//

#include "ovalAction.h"

USING_NS_CC;

MoveovalBy::MoveovalBy()
{
    
}

MoveovalBy::~MoveovalBy()
{
    
}

MoveovalBy *MoveovalBy::create(float dt,const ovalConfig &c)
{
    auto moveovalBy = new MoveovalBy();
    if (moveovalBy && moveovalBy->initWithDuration(dt,c))
    {
        moveovalBy->autorelease();
        return moveovalBy;
    }
    
    return nullptr;
}

bool MoveovalBy::initWithDuration(float dt,const ovalConfig &c)
{
    if (ActionInterval::initWithDuration(dt))
    {
        _config = c;
        return true;
    }
    
    return false;
}

void MoveovalBy::update(float dt)
{
    if (_target)
    {
        float x = getPosXAtoval(dt);
        float y = getPosYAtoval(dt);
        _target->setPosition(_config.centerPos + Vec2(x,y));
        if (dt <= 0.5)
        {
            _target->setZOrder(_config.zOrder.first);
        }
        else
        {
            _target->setZOrder(_config.zOrder.second);
        }
    }
}

MoveovalBy *MoveovalBy::clone() const
{
    auto moveovalBy = new MoveovalBy();
    if (moveovalBy && moveovalBy->initWithDuration(_duration,_config))
    {
        moveovalBy->autorelease();
        return moveovalBy;
    }
    
    return nullptr;
}

MoveovalBy *MoveovalBy::reverse() const
{
    ovalConfig newConfig;
    newConfig.a = _config.a;
    newConfig.b = _config.b;
    newConfig.centerPos = _config.centerPos;
    newConfig.moveClockDir = !_config.moveClockDir;
    newConfig.zOrder = _config.zOrder;
    return MoveovalBy::create(_duration,newConfig);
}

void MoveovalBy::startWithTarget(Node *target)
{
    ActionInterval::startWithTarget(target);
}


调用方法

auto s1 = Sprite::create("CloseSelected.png");
    addChild(s1);
    s1->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
    auto s2 = Sprite::create("Closenormal.png");
    addChild(s2);
    ovalConfig c;
    c.a = 100;
    c.b = 10;
    c.centerPos = s1->getPosition();
    c.moveClockDir = false;
    c.zOrder.first = -1;
    c.zOrder.second = 1;
    s2->runAction(RepeatForever::create(MoveovalBy::create(1.0f,c)));

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...