H # include "Hero."
USING_NS_CC;
Hero: a Hero * : createHeroSprite (Point position, int direction, const char * name)
{
Hero Hero *=new Hero ();
If (hero & amp; & Hero - & gt; The init ())
{
Hero - & gt; Autorelease ();
Hero - & gt; HeroInit (position, direction, name);
The return of hero.
}
CC_SAFE_DELETE (hero);
return NULL;
}
Bool Hero: : init ()
{
if (! Layer: : init ())
{
return false;
}
return true;
}
Void Hero: : heroInit (Point position, int direction, const char * name)
{
This - & gt; IsRun=false;
This - & gt; The position=position;
Sprite=Sprite: : create (String: : createWithFormat (" % s11. The PNG ", name) - & gt; GetCString ());
Sprite - & gt; SetPosition (position);
AddChild (Sprite);
Auto * action=createAnimate1 (1, "stand", 7);
The action - & gt; SetTag (100);
Sprite - & gt; RunAction (action);
}
The Animate * Hero: : createAnimate1 (int direction, const char * action, int num)
{
Auto * m_frameCache=SpriteFrameCache: : getInstance ();
M_frameCache - & gt; AddSpriteFramesWithFile (" hero. Plist ", "hero. PNG");
Vector
Auto * frame=m_frameCache - & gt; GetSpriteFrameByName (String: : createWithFormat (" % s % d % d.p ng ", action, direction, I) - & gt; GetCString ());
FrameArray. PushBack (frame);
}
Animation Animation *=Animation: : createWithSpriteFrames (frameArray);
Animation - & gt; SetLoops (1);//said a infinite loop
Animation - & gt; SetDelayPerUnit (0.1 f);//every two images of the interval, the less number of images, the less the minimum interval
//animation packaging into an action
Return the Animate: : create (animation);
}
The Animate * Hero: : createAnimate (int direction, const char * action, int num, int time)
{
Auto * m_frameCache=SpriteFrameCache: : getInstance ();
M_frameCache - & gt; AddSpriteFramesWithFile (" hero. Plist ", "hero. PNG");
Vector
Auto * frame=m_frameCache - & gt; GetSpriteFrameByName (String: : createWithFormat (" % s % d % d.p ng ", action, direction, I) - & gt; GetCString ());
FrameArray. PushBack (frame);
}
Animation Animation *=Animation: : createWithSpriteFrames (frameArray);
Animation - & gt; SetLoops (time);//said a infinite loop
Animation - & gt; SetDelayPerUnit (0.1 f);//every two images of the interval, the less number of images, the less the minimum interval
//animation packaging into an action
Return the Animate: : create (animation);
}
Void Hero: : setAction (int direction, const char * action, int num)
{
Sprite - & gt; StopActionByTag (100);
Auto * the animate=createAnimate1 (direction, action, num);
The animate - & gt; SetTag (100);
Sprite - & gt; RunAction (animate);
}
Void Hero: : moveTo (float x, float y)
{
Float r=SQRT (x * * y x + y);
Position. X +=x/r;
Position. + y=y/r;
Sprite - & gt; SetPosition (position);
}
Void Hero: : runAttack enemy Hero (*)
{
Sprite - & gt; StopAllActions ();
Point target;
If (enemy - & gt; Direction==1)
{
Target=Vec2 (enemy - & gt; The position x - 20, enemy - & gt; Position. The y + 30);
}
The else
{
Target=Vec2 (enemy - & gt; The position x + 20, enemy - & gt; Position. The y + 10);
}
Auto * moveto=moveto: : create (0.5 f, target);
Auto * run=createAnimate (direction, "run", 5, 1);
Auto * spawn=spawn: : create (moveto, run, NULL);
Auto * callFunc=callFunc: : create (CC_CALLBACK_0 (Hero: : doAttack, this));
Auto * sequence=sequence: : create (spawn, callFunc, NULL);
Sprite - & gt; RunAction (sequence);
}
Void Hero: : doAttack ()
{
Auto * the animate=createAnimate (direction, "attack", 14, 1);
Auto * callFunc=callFunc: : create (CC_CALLBACK_0 (Hero: : runBack, this));
Auto * sequence=sequence: : create (animate, callFunc, NULL);
Sprite - & gt; RunAction (sequence);
}
Void Hero: : runBack ()
{
Sprite - & gt; StopAllActions ();
Auto * back=createAnimate (direction, "back", 5, 1);
Auto * moveto=moveto: : create (0.5 f, position);
Auto * spawn=spawn: : create (back to moveto, NULL);
Auto * callFunc=callFunc: : create (CC_CALLBACK_0 (Hero: : doStand, this));
Auto * sequence=sequence: : create (spawn, callFunc, NULL);
Sprite - & gt; RunAction (sequence);
}
Void Hero: : doStand ()
{
Sprite - & gt; StopAllActions ();
Auto * the animate=createAnimate (direction, "stand", 8, 1);
Sprite - & gt; RunAction (animate);
}
Void Hero: : waitingAttack ()
{
ScheduleOnce (schedule_selector (Hero: : beAttacked), 1.1 f);
}
Void Hero: : beAttacked float (dt)
{
Sprite - & gt; StopAllActions ();
Auto * the animate=createAnimate (direction, "underatt", 2, 1);
Auto * callFunc=callFunc: : create (CC_CALLBACK_0 (Hero: : waiting, this));
Auto * sequence=sequence: : create (animate, callFunc, NULL);
Sprite - & gt; RunAction (sequence);
}
Void Hero: : waiting ()
{
ScheduleOnce (schedule_selector (Hero: : waitingUpdate), 0.5 f);
}
Void Hero: : waitingUpdate float (dt)
{
DoStand ();
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull