void Hook::runRotateAction()
{
this->setAnchorPoint(CCPointMake(0.5, 1));
CCActionInterval *action1 = CCRotateTo::create(2,-90.0);
CCActionInterval *action2 = CCRotateTo::create(2, 90.0);
float rotate = this->getRotation();
CCSequence *action3;
if(rotate>0)
{
action3 = CCSequence::create(action1,action2,NULL);
}
else
{
action3 = CCSequence::create(action2,action1,NULL);
}
CCRepeatForever *repeat = CCRepeatForever::create(action3);
this->runAction(repeat);
}
void Hook::runBack()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCActionInterval *action4 = CCMoveTo::create(2, ccp(winSize.width/2-2, winSize.height/2-20));
CCCallFunc *func = CCCallFunc::create(this, callfunc_selector(Hook::runRotateAction));
CCSequence *seq = CCSequence::create(action4,func,NULL);
this->runAction(seq);
}
void Hook::runToPoint(CCPoint point)
{
this->stopAllActions();
CCPoint start = this->getPosition();
float distance = sqrtf((start.x-point.x)*(start.x-point.x) + (start.y-point.y)*(start.y-point.y));
float time = distance*0.01;
CCActionInterval *actionTo = CCMoveTo::create(time, point);
CCCallFunc *fun = CCCallFunc::create(this, callfunc_selector(Hook::runBack));
CCSequence *seq = CCSequence::create(actionTo,fun,NULL);
this->runAction(seq);
}
void Hook::runTargetAction()
{
float x;
float y;
if(this->getRotation()>0)
{
if(this->getRotation()<=45)
{
y=0.0;
x= 240-160*tan(this->getRotation()*3.1415/180);
}
else
{
y=160-240/tan(this->getRotation()*3.1415/180);
x= 0.0;
}
}
else if(this->getRotation()<0)
{
if(this->getRotation()<=-45)
{
y=160+240/tan(this->getRotation()*3.1415/180);
x= 480;
}
else
{
y=0;
x= 240-160*tan(this->getRotation()*3.1415/180);
}
}
else if(this->getRotation() == 0)
{
x= 240;
y= 160;
}
this->runToPoint(ccp(x, y));
}