void Animation::Update(float dt) { mTimer += dt; if (mTimer >= mLength&&mLooping) { mDone = true; Restart(); // always looping for now } int count = mMainline->GetKeyframeCount(); Key *keyframe = mMainline->GetKeyframe(mCurrKeyframe); float currTime = keyframe->GetTime(); Key *keyframeNext = NULL; int next = mCurrKeyframe+1; if (next > count-1&&mLooping) // looping next = 0; keyframeNext = mMainline->GetKeyframe(next); if (keyframeNext) { float nextTime = keyframeNext->GetTime(); if (next == 0) nextTime = mLength; if (mTimer >= nextTime) { mCurrKeyframe = next; keyframe = keyframeNext; currTime = keyframe->GetTime(); next = mCurrKeyframe+1; if (next > count-1&&mLooping) // looping next = 0; keyframeNext = mMainline->GetKeyframe(next); if (keyframeNext == NULL) return; nextTime = keyframeNext->GetTime(); if (next == 0) nextTime = mLength; } float t = (mTimer-currTime)/(nextTime-currTime); int count = keyframe->GetObjectRefCount(); for (int i=0;i<count;i++) { ObjectRef *ref = keyframe->GetObjectRef(i); ObjectRef *refNext = keyframeNext->GetObjectRef(i); if (ref && refNext) { Key *keyRef = mTimelines[ref->timeline]->GetKeyframe(ref->key); Object *obj = keyRef->GetObject(0); // should be only 1 object Key *keyRefNext = mTimelines[refNext->timeline]->GetKeyframe(refNext->key); Object *objNext = keyRefNext->GetObject(0); float x = obj->x+(objNext->x-obj->x)*t; float y = obj->y+(objNext->y-obj->y)*t; float angle = objNext->angle-obj->angle; if (keyRef->IsSpinCounterClockwise()) { if (angle < 0) angle = (objNext->angle+360)-obj->angle; } else { if (angle > 0) { angle = (objNext->angle-360)-obj->angle; } } if (ref->timeline != refNext->timeline) t = 0; angle = obj->angle+(angle)*t; if (angle >= 360) angle -= 360; float px = obj->pivot_x+(objNext->pivot_x-obj->pivot_x)*t; float py = obj->pivot_y+(objNext->pivot_y-obj->pivot_y)*t; CCPoint newPos = ccp(mPosition.x+x, mPosition.y+y); obj->sprite->setPosition(newPos); obj->sprite->setRotation(-angle); obj->sprite->setAnchorPoint(ccp(px, py)); } } } }
void Animation::Init(TiXmlNode *node, CCSpriterX *animator) { int intValue; float floatValue; TiXmlElement *element = node->ToElement(); if (element) { if (element->QueryIntAttribute("id", &intValue) == TIXML_SUCCESS) mId = intValue; mName = element->Attribute("name"); if (element->QueryFloatAttribute("length", &floatValue) == TIXML_SUCCESS) mLength = floatValue/1000.0f; // was in milliseconds, convert to seconds instead const char *looping = element->Attribute("looping"); // was set to "false" in alpha, but in fact looping all the time mLooping = true; for (TiXmlNode* lineNode = node->FirstChild(); lineNode; lineNode = lineNode->NextSibling()) { element = lineNode->ToElement(); const char *tabLine = element->Value(); if (strcmp(tabLine, "mainline")==0) // 1 mainline only { mMainline = new Timeline(); mMainline->Init(lineNode, animator); } else if (strcmp(tabLine, "timeline")==0) { Timeline *timeline = new Timeline(); timeline->Init(lineNode, animator); mTimelines.push_back(timeline); } } { int count = mMainline->GetKeyframeCount(); Key *keyframe = mMainline->GetKeyframe(mCurrKeyframe); float currTime = keyframe->GetTime(); Key *keyframeNext = NULL; int next = mCurrKeyframe+1; if (next > count-1) next = 0; keyframeNext = mMainline->GetKeyframe(next); if (keyframeNext) { float nextTime = keyframeNext->GetTime(); if (next == 0) nextTime = mLength; if (mTimer >= nextTime) { mCurrKeyframe = next; keyframe = keyframeNext; currTime = keyframe->GetTime(); next = mCurrKeyframe+1; if (next > count-1&&mLooping) // looping next = 0; keyframeNext = mMainline->GetKeyframe(next); if (keyframeNext == NULL) return; nextTime = keyframeNext->GetTime(); if (next == 0) nextTime = mLength; } float t = (mTimer-currTime)/(nextTime-currTime); int count = keyframe->GetObjectRefCount(); for (int i=0;i<count;i++) { ObjectRef *ref = keyframe->GetObjectRef(i); ObjectRef *refNext = keyframeNext->GetObjectRef(i); if (ref && refNext) { Key *keyRef = mTimelines[ref->timeline]->GetKeyframe(ref->key); Object *obj = keyRef->GetObject(0); // should be only 1 object Key *keyRefNext = mTimelines[refNext->timeline]->GetKeyframe(refNext->key); Object *objNext = keyRefNext->GetObject(0); float x = obj->x+(objNext->x-obj->x)*t; float y = obj->y+(objNext->y-obj->y)*t; float angle = objNext->angle-obj->angle; if (keyRef->IsSpinCounterClockwise()) { if (angle < 0) angle = (objNext->angle+360)-obj->angle; } else { if (angle > 0) { angle = (objNext->angle-360)-obj->angle; } } if (ref->timeline != refNext->timeline) t = 0; angle = obj->angle+(angle)*t; if (angle >= 360) angle -= 360; float px = obj->pivot_x+(objNext->pivot_x-obj->pivot_x)*t; float py = obj->pivot_y+(objNext->pivot_y-obj->pivot_y)*t; CCPoint newPos = ccp(mPosition.x+x, mPosition.y+y); obj->sprite->setPosition(newPos); obj->sprite->setRotation(-angle); obj->sprite->setAnchorPoint(ccp(px, py)); } } } } } }