Cocos2d-x移植的Tween算法

本文介绍了作者在Cocos2d-x *.11.0版本上手动实现Tween算法的过程,作为对已有实现的兴趣探索。Tween算法,也称补间动画,用于创建平滑的过渡效果。文章引用了一篇关于Tween的博客,并提到了在xcode 4.2平台上遇到的问题及解决方案,包括在Quintic算法的修正和Win32平台上的颜色问题。
摘要由CSDN通过智能技术生成

时间:3/14/2012

平台:xcode 4.2 + cocos2d-x  *.11.0 

首先说明,cocos2d-x已经有实现这些算法了,本人只是出于兴趣动手实现下

不知道tween算法的朋友看看这个:

原文链接:http://www.cnblogs.com/cloudgamer/archive/2009/01/06/Tween.html

Tween又称为补间动画或中间动画;

上图:



//
//  CCTween.h
//  cocos2dUI_b
//
//  Created by rorger on 12-3-12.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

/*
 find more information here:http://www.robertpenner.com/easing/
 http://www.cnblogs.com/cloudgamer/archive/2009/01/06/Tween.html
 */
#ifndef cocos2dUI_b_CCTween_h
#define cocos2dUI_b_CCTween_h
#include "cocos2d.h"
#include "platform.h"
USING_NS_CC;
typedef enum {
    CCTweenLinear,
    CCTweenQuadratic,
    CCTweenCubic,
    CCTweenQuartic,
    CCTweenQuintic,
    CCTweenSinusoidal,
    CCTweenExponential,
    CCTweenCircular,
    CCTweenElastic,
    CCTweenBack,
    CCTweenBounce
}CCTweenType;

typedef enum{
    CCTweenEaseIn,
    CCTweenEaseOut,
    CCTweenEaseInOut
}CCTweenEaseType;
 
//Linear,Quadratic,Cubic,Quartic,Quintic,Sinusoidal,Exponential,Circular,Elastic,Back,Bounce
class CCTween {
public:
    CCTween(CCTweenType type,CCTweenEaseType easeType);
    virtual bool init();
    virtual ~CCTween();

    static CCTween* tweenWithTypeEaseType(CCTweenType type,CCTweenEaseType easeType);
    CGFloat Tween(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration, CGFloat a=0, CGFloat p=0, CGFloat s=0);
    CGFloat Linear(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration);
    
    CGFloat Quadratic(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration);
    CGFloat QuadraticIn(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat QuadraticOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat QuadraticInOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    
    CGFloat Cubic(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration);
    CGFloat CubicIn(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat CubicOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat CubicInOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    
    CGFloat Quartic(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration);
    CGFloat QuarticIn(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat QuarticOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat QuarticInOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    
    CGFloat Quintic(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration);
    CGFloat QuinticIn(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat QuinticOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat QuinticInOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    
    CGFloat Sinusoidal(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration);
    CGFloat SinusoidalIn(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat SinusoidalOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat SinusoidalInOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    
    CGFloat Exponential(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration);
    CGFloat ExponentialIn(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat ExponentialOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat ExponentialInOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    
    CGFloat Circular(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration);
    CGFloat CircularIn(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat CircularOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat CircularInOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    
    CGFloat Elastic(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration, CGFloat a, CGFloat p);
    CGFloat ElasticIn(CGFloat t, CGFloat b, CGFloat c, CGFloat d, CGFloat a, CGFloat p);
    CGFloat ElasticOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d, CGFloat a, CGFloat p);
    CGFloat ElasticInOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d, CGFloat a, CGFloat p);
    
    CGFloat Back(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration, CGFloat s);
    CGFloat BackIn(CGFloat t, CGFloat b, CGFloat c, CGFloat d, CGFloat s);
    CGFloat BackOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d, CGFloat s);
    CGFloat BackInOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d, CGFloat s);
    
    CGFloat Bounce(CGFloat time, CGFloat beginValue, CGFloat changeValue, CGFloat duration);
    CGFloat BounceIn(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat BounceOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
    CGFloat BounceInOut(CGFloat t, CGFloat b, CGFloat c, CGFloat d);
     
public:
    CC_SYNTHESIZE(CCTweenEaseType, tweenEaseType, TweenEaseType);
    CC_SYNTHESIZE(CCTweenType, tweenType, TweenType);
};
#endif
//
//  CCTween.cpp
//  cocos2dUI_b
//
//  Created by rorger on 12-3-12.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#include "CCTween.h"
#include "math.h"

#define pi acos(-1.0)

CCTween::CCTween(CCTweenType type,CCTweenEaseType easeType){
    this->tweenType = type;
    this->tweenEaseType = easeType;
}

bool CCTween::init(){
    return true;
}

CCTween* CCTween::tweenWithTypeEaseType(CCTweenType type,CCTweenEaseType easeType){
    CCTween * pRet = new CCTween(type,easeType);
    if (pRet && pRet->init()) {
        return pRet;
    }
    return NULL;
}

CCTween::~CCTween(){
    
}

CGFloat CCTween::Linear(CGFloat t, CGFloat b, CGFloat c, CGFloat d){
    return c * (t / d) + b; 
    
}

CGFloat CCTween::Quadratic(CGFloat t, CGF
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值