时间: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