关于扩展ActionInterval的新动作问题

最近看cocos2d-x高级开发教程的《捕鱼达人》,在测试自定义动作时,没有看到鱼儿随着曲线运动调整角度。随后做了下实验:

  1. 将鱼儿的起点和终点从屏幕外,修改为屏幕内,这样就可以看到期间发生了什么问题-》随后的测试表明只有两帧动作,很奇怪,接着做了实验2
  2. 在ActionInterval内部打印动作的持续时间_duration,发现动作时间周期为0,这才导致实验1只看到2帧

最后在代码中增加了黑色字体部分,才实现这个动作。
3. 类似这个动作定义(在其他Action上附加其他功能)的例如系统的自定义变速动作,Speed和ActionEase等,可以参考他们的写法。
ActionMoveWithRotation::~ActionMoveWithRotation()
{
CC_SAFE_RELEASE(pInnerAction);
}

ActionMoveWithRotation* ActionMoveWithRotation::createWithAction(ActionInterval action)
{
ActionMoveWithRotation
act = new(std::nothrow) ActionMoveWithRotation();
if(act&&act->initWithAction(action)&&
act->initWithDuration(action->getDuration()))
{
act->autorelease();
return act;
}

CC_SAFE_DELETE(act);
return NULL;

}

bool ActionMoveWithRotation::initWithAction(ActionInterval* action)
{
pInnerAction = action;
CC_SAFE_RETAIN(pInnerAction);
return true;
}

bool ActionMoveWithRotation::isDone() const
{
return pInnerAction->isDone();
}

void ActionMoveWithRotation::step(float dt)
{
Point prevPos = pInnerTarget->getPosition();
pInnerAction->step(dt);
Point curPos = pInnerTarget->getPosition();
float tan = -(curPos.y-prevPos.y)/(curPos.x-prevPos.x);
float degree = atan(tan);
degree = degree/3.14159f*180;
pInnerTarget->setRotation(degree);
}

void ActionMoveWithRotation::startWithTarget(Node* target)
{
pInnerTarget = target;
ActionInterval::startWithTarget(target);
pInnerAction->startWithTarget(target);
}

void ActionMoveWithRotation::setInnerAction(ActionInterval* action)
{
if(action!=pInnerAction)
{
CC_SAFE_RELEASE(pInnerAction);
pInnerAction = action;
CC_SAFE_RETAIN(pInnerAction);
}
}

ActionMoveWithRotation* ActionMoveWithRotation::clone() const
{
ActionMoveWithRotation *copy = ActionMoveWithRotation::createWithAction(pInnerAction);
if(copy)
{
copy->autorelease();
return copy;
}
CC_SAFE_DELETE(copy);
return NULL;
}

void ActionMoveWithRotation::update(float dt)
{
Point prevPos = pInnerTarget->getPosition();
pInnerAction->update(dt);
Point curPos = pInnerTarget->getPosition();
float tan = -(curPos.y-prevPos.y)/(curPos.x-prevPos.x);
float degree = atan(tan);
degree = degree/3.14159f*180;
CCLOG(“actionmovewithrotation dt=%f\n”,dt);
pInnerTarget->setRotation(degree);
}