issue #2790: Vector<FiniteTimeAction*> for Sequence::create(arr) and Spawn::create(arr).

This commit is contained in:
James Chen 2013-11-28 11:04:39 +08:00
parent 80004548c8
commit c152652c5b
6 changed files with 81 additions and 42 deletions

View File

@ -198,21 +198,21 @@ Sequence* Sequence::createWithVariableList(FiniteTimeAction *pAction1, va_list a
return ((Sequence*)pPrev);
}
Sequence* Sequence::create(Array* arrayOfActions)
Sequence* Sequence::create(const Vector<FiniteTimeAction*>& arrayOfActions)
{
Sequence* pRet = NULL;
do
{
long count = arrayOfActions->count();
long count = arrayOfActions.count();
CC_BREAK_IF(count == 0);
FiniteTimeAction* prev = static_cast<FiniteTimeAction*>(arrayOfActions->getObjectAtIndex(0));
auto prev = arrayOfActions[0];
if (count > 1)
{
for (long i = 1; i < count; ++i)
{
prev = createWithTwoActions(prev, static_cast<FiniteTimeAction*>(arrayOfActions->getObjectAtIndex(i)));
prev = createWithTwoActions(prev, arrayOfActions[i]);
}
}
else
@ -571,19 +571,19 @@ Spawn* Spawn::createWithVariableList(FiniteTimeAction *pAction1, va_list args)
return ((Spawn*)pPrev);
}
Spawn* Spawn::create(Array *arrayOfActions)
Spawn* Spawn::create(const Vector<FiniteTimeAction*>& arrayOfActions)
{
Spawn* pRet = NULL;
do
{
long count = arrayOfActions->count();
long count = arrayOfActions.count();
CC_BREAK_IF(count == 0);
FiniteTimeAction* prev = static_cast<FiniteTimeAction*>(arrayOfActions->getObjectAtIndex(0));
auto prev = arrayOfActions[0];
if (count > 1)
{
for (int i = 1; i < arrayOfActions->count(); ++i)
for (int i = 1; i < arrayOfActions.count(); ++i)
{
prev = createWithTwoActions(prev, static_cast<FiniteTimeAction*>(arrayOfActions->getObjectAtIndex(i)));
prev = createWithTwoActions(prev, arrayOfActions[i]);
}
}
else
@ -591,7 +591,7 @@ Spawn* Spawn::create(Array *arrayOfActions)
// If only one action is added to Spawn, make up a Spawn by adding a simplest finite time action.
prev = createWithTwoActions(prev, ExtraAction::create());
}
pRet = (Spawn*)prev;
pRet = static_cast<Spawn*>(prev);
}while (0);
return pRet;

View File

@ -32,6 +32,7 @@ THE SOFTWARE.
#include "CCProtocols.h"
#include "CCSpriteFrame.h"
#include "CCAnimation.h"
#include <CCVector.h>
#include <vector>
NS_CC_BEGIN
@ -99,7 +100,7 @@ public:
* in lua :local create(local object1,local object2, ...)
* @endcode
*/
static Sequence* create(Array *arrayOfActions);
static Sequence* create(const Vector<FiniteTimeAction*>& arrayOfActions);
/** helper constructor to create an array of sequence-able actions */
static Sequence* createWithVariableList(FiniteTimeAction *pAction1, va_list args);
/** creates the action */
@ -246,7 +247,7 @@ public:
static Spawn* createWithVariableList(FiniteTimeAction *pAction1, va_list args);
/** helper constructor to create an array of spawned actions given an array */
static Spawn* create(Array *arrayOfActions);
static Spawn* create(const Vector<FiniteTimeAction*>& arrayOfActions);
/** creates the Spawn action */
static Spawn* createWithTwoActions(FiniteTimeAction *pAction1, FiniteTimeAction *pAction2);

View File

@ -60,6 +60,7 @@ THE SOFTWARE.
#include "CCDictionary.h"
#include "CCObject.h"
#include "CCArray.h"
#include "CCVector.h"
#include "CCGeometry.h"
#include "CCSet.h"
#include "CCAutoreleasePool.h"

View File

@ -39,21 +39,50 @@ public:
Vector<T>(long capacity=7)
: _data()
{
CCLOG("In the constructor of Vector.");
init(capacity);
}
virtual ~Vector<T>() {
for( auto it=std::begin(_data); it != std::end(_data); ++it )
(*it)->release();
CCLOG("In the destructor of Vector.");
removeAllObjects();
}
Vector<T>(const Vector<T>& other)
{
CCLOG("In the copy constructor!");
copy(other);
}
const Vector<T>& operator=(const Vector<T>& other)
{
CCLOG("In the assignment operator!");
copy(other);
return *this;
}
T operator[](long index) const
{
return getObjectAtIndex(index);
}
/** Initializes an array with capacity */
bool init(long capacity)
{
_data.reserve(capacity);
return true;
}
void copy(const Vector<T>& other)
{
if (this == &other)
return;
removeAllObjects();
init(other.count());
addObjectsFromArray(other);
}
// Querying an Array
/** Returns element count of the array */
@ -131,9 +160,9 @@ public:
}
/** Add all elements of an existing array */
void addObjectsFromArray(T otherArray)
void addObjectsFromArray(const Vector<T>& otherArray)
{
for( auto it = std::begin(otherArray); it != std::end(otherArray); ++it ) {
for( auto it = otherArray.begin(); it != otherArray.end(); ++it ) {
_data.push_back( *it );
(*it)->retain();
}
@ -180,17 +209,17 @@ public:
void removeObjectAtIndex(long index)
{
auto it = std::next( begin(), index );
_data.erase(it);
(*it)->release();
_data.erase(it);
}
/** Removes all objects */
void removeAllObjects()
{
for( auto it = std::begin(_data); it != std::end(_data); ++it ) {
_data.erase(it);
(*it)->release();
}
_data.clear();
}
/** Fast way to remove a certain object */
@ -254,9 +283,16 @@ public:
typedef typename std::vector<T>::const_iterator const_iterator;
iterator begin() { return _data.begin(); }
const_iterator begin() const { return _data.begin(); }
iterator end() { return _data.end(); }
const_iterator cbegin() { return _data.cbegin(); }
const_iterator cend() { return _data.cend(); }
const_iterator end() const { return _data.end(); }
iterator cbegin() { return _data.cbegin(); }
const_iterator cbegin() const { return _data.cbegin(); }
iterator cend() { return _data.cend(); }
const_iterator cend() const { return _data.cend(); }
protected:
std::vector<T> _data;

View File

@ -620,7 +620,7 @@ Object* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* chann
float lastKeyframeTime = 0;
Array *actions = Array::create();
Vector<FiniteTimeAction*> actions;
Array *keyframes = channel->getKeyframes();
long numKeyframes = keyframes->count();
@ -631,7 +631,7 @@ Object* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* chann
float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime;
lastKeyframeTime = keyframe->getTime();
if(timeSinceLastKeyframe > 0) {
actions->addObject(DelayTime::create(timeSinceLastKeyframe));
actions.addObject(DelayTime::create(timeSinceLastKeyframe));
}
Array* keyVal = static_cast<Array *>(keyframe->getValue());
@ -646,7 +646,7 @@ Object* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* chann
CallFunc *callbackClone = (static_cast<CallFunc*>(callback))->clone();
if(callbackClone != NULL) {
actions->addObject(callbackClone);
actions.addObject(callbackClone);
}
}
}
@ -680,7 +680,7 @@ Object* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* chann
{
// XXX: how to fix this warning?
CallFuncN *callback = CallFuncN::create(target, selCallFunc);
actions->addObject(callback);
actions.addObject(callback);
}
}
else
@ -690,7 +690,7 @@ Object* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* chann
}
}
}
if(actions->count() < 1) return NULL;
if(actions.count() < 1) return NULL;
return (Object *) Sequence::create(actions);
}
@ -699,9 +699,9 @@ Object* CCBAnimationManager::actionForSoundChannel(CCBSequenceProperty* channel)
float lastKeyframeTime = 0;
Array *actions = Array::create();
Vector<FiniteTimeAction*> actions;
Array *keyframes = channel->getKeyframes();
int numKeyframes = keyframes->count();
long numKeyframes = keyframes->count();
for (int i = 0; i < numKeyframes; ++i) {
@ -709,7 +709,7 @@ Object* CCBAnimationManager::actionForSoundChannel(CCBSequenceProperty* channel)
float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime;
lastKeyframeTime = keyframe->getTime();
if(timeSinceLastKeyframe > 0) {
actions->addObject(DelayTime::create(timeSinceLastKeyframe));
actions.addObject(DelayTime::create(timeSinceLastKeyframe));
}
stringstream ss (stringstream::in | stringstream::out);
@ -729,12 +729,12 @@ Object* CCBAnimationManager::actionForSoundChannel(CCBSequenceProperty* channel)
ss >> gain;
ss.flush();
actions->addObject(CCBSoundEffect::actionWithSoundFile(soundFile, pitch, pan, gain));
actions.addObject(CCBSoundEffect::actionWithSoundFile(soundFile, pitch, pan, gain));
}
if(actions->count() < 1) return NULL;
if(actions.count() < 1) return NULL;
return (Object *) Sequence::create(actions);
return Sequence::create(actions);
}
@ -742,19 +742,19 @@ Object* CCBAnimationManager::actionForSoundChannel(CCBSequenceProperty* channel)
void CCBAnimationManager::runAction(Node *pNode, CCBSequenceProperty *pSeqProp, float fTweenDuration)
{
Array *keyframes = pSeqProp->getKeyframes();
int numKeyframes = keyframes->count();
long numKeyframes = keyframes->count();
if (numKeyframes > 1)
{
// Make an animation!
Array *actions = Array::create();
Vector<FiniteTimeAction*> actions;
CCBKeyframe *keyframeFirst = (CCBKeyframe*)keyframes->getObjectAtIndex(0);
float timeFirst = keyframeFirst->getTime() + fTweenDuration;
if (timeFirst > 0)
{
actions->addObject(DelayTime::create(timeFirst));
actions.addObject(DelayTime::create(timeFirst));
}
for (int i = 0; i < numKeyframes - 1; ++i)
@ -768,11 +768,11 @@ void CCBAnimationManager::runAction(Node *pNode, CCBSequenceProperty *pSeqProp,
// Apply easing
action = getEaseAction(action, kf0->getEasingType(), kf0->getEasingOpt());
actions->addObject(action);
actions.addObject(action);
}
}
FiniteTimeAction *seq = Sequence::create(actions);
auto seq = Sequence::create(actions);
pNode->runAction(seq);
}
}

View File

@ -276,7 +276,8 @@ Spawn * ActionNode::refreshActionProperty()
{
return NULL;
}
Array* cSpawnArray = Array::create();
Vector<FiniteTimeAction*> cSpawnArray;
for (int n = 0; n < _frameArrayNum; n++)
{
Array* cArray = (Array*)(_frameArray->getObjectAtIndex(n));
@ -285,8 +286,8 @@ Spawn * ActionNode::refreshActionProperty()
continue;
}
Array* cSequenceArray = Array::create();
int frameCount = cArray->count();
Vector<FiniteTimeAction*> cSequenceArray;
long frameCount = cArray->count();
for (int i = 0; i < frameCount; i++)
{
ActionFrame* frame = (ActionFrame*)(cArray->getObjectAtIndex(i));
@ -298,13 +299,13 @@ Spawn * ActionNode::refreshActionProperty()
ActionFrame* srcFrame = (ActionFrame*)(cArray->getObjectAtIndex(i-1));
float duration = (frame->getFrameIndex() - srcFrame->getFrameIndex()) * getUnitTime();
Action* cAction = frame->getAction(duration);
cSequenceArray->addObject(cAction);
cSequenceArray.addObject(static_cast<FiniteTimeAction*>(cAction));
}
}
Sequence* cSequence = Sequence::create(cSequenceArray);
if (cSequence != NULL)
{
cSpawnArray->addObject(cSequence);
cSpawnArray.addObject(cSequence);
}
}