Imitation "Thunder Fighter" Flying shooter Tour development--gameobject

Source: Internet
Author: User

Reprint Please specify: http://www.cnblogs.com/thorqq/p/5646509.html

In the previous article, we introduced the functions of various game objects and the integration of classes, now let's look at the source code of Gameobject

Collision Body

GameObject.h

1 classGameobject: PublicSprite2 {3  Public:4 Gameobject ();5 6     Virtual voidSetbodysize (Constsize&s);7     Virtual voidSetbodysize (floatWfloath);8     Virtual Constsize&getbodysize ();9     Virtual Constsize& getorignbodysize ()Const;Ten  One     Virtual voidSetbodycenter (Constvec2&v); A     Virtual voidSetbodycenter (floatXfloaty); -     Virtual Constvec2& Getbodycenter ()Const; -  the     //get the position and size of the body under world coordinates -     VirtualRect Getbodybox ()Const; -  -  + protected: -     //the size and position of the rigid body for collision detection +VEC2 M_bodycenter;//the coordinate of the center point of a rigid body (relative to the coordinates of the sprite anchor point) ASize m_bodysize;//the width and height of a rigid body at};

GameObject.cpp

1 voidGameobject::setbodysize (Constsize&s)2 {3M_bodysize =s;4 }5 6 voidGameobject::setbodysize (floatWfloath)7 {8 setbodysize (Size (W, h));9 }Ten  One Constsize&gameobject::getbodysize () A { -     returnm_bodysize; - } the  - voidGameobject::setbodycenter (Constvec2&v) - { -M_bodycenter =v; + } -  + voidGameobject::setbodycenter (floatXfloaty) A { atM_bodycenter =Vec2 (x, y); - } -  - Constvec2& Gameobject::getbodycenter ()Const - { -     returnM_bodycenter; in } -  to //get the position and size of the body under world coordinates +Rect Gameobject::getbodybox ()Const - { theVEC2 pos =getPosition (); *  $     returnRect (pos.x + m_bodycenter.x-m_bodysize.width *getanchorpoint (). x,Panax NotoginsengPos.y + m_bodycenter.y-m_bodysize.height *getanchorpoint (). Y, - M_bodysize.width, the m_bodysize.height); +}

The definition of the collision body is simple, the center coordinate + width is high, and then the common Get/set method is added. One of the more useful is the Getbodybox () method. Since the center coordinate of the collision body is relative to the coordinates of the sprite anchor point, if it is to be used to determine whether the collision of two collisions (whether there is overlapping area), it is necessary to obtain the position and size of two colliding bodies under world coordinates, then call the Getbodybox () method to get the Rect object, the bool Intersectsrect (const rect& Rect) method of the Rect is then called to determine whether a collision has occurred for the two colliding bodies.

Pause/Resume

GameObject.h

1     Virtual void Override ; 2     Virtual void Override ; 3     void Pause (Node *pnode); 4     void Resume (Node *pnode);

GameObject.cpp

1 voidgameobject::p ause ()2 {3      This->pause ( This);4 }5 6 voidGameobject::resume ()7 {8      This->resume ( This);9 }Ten  One voidGameobject::p ause (Node *Pnode) A { - Node::p ause (); -  the      for(Auto p:pnode->GetChildren ()) -     { -P->pause (); -     } + } -  + voidGameobject::resume (Node *Pnode) A { at Node::resume (); -  -      for(Auto p:pnode->GetChildren ()) -     { -P->resume (); -     } in}

When you call pause and resume, the pause and resume of all child nodes are called. In this way, when the player plane pauses, wingman as its child node, will also follow the pause.

Initialization

Look at the code first

1 BOOLGameobject::initspritewithfilelist (ConstSTD::VECTOR&LT;STD::string>& FileList,floatDura)2 {3Spriteframe *frame = spriteframecache::getinstance ()->getspriteframebyname (filelist.at (0));4     if(NULL = =frame)5     {6Debug_log ("Error Get frame of '%s '", filelist.at (0). C_STR ());7Ccassert (Frame,"Error Get Frame");8     }9 Sprite::initwithspriteframe (frame);Ten  One     //Animation A     if(Filelist.size () >1) -     { -animation* Animation =animation::create (); theAnimation->setdelayperunit (dura); -          for(Unsigned i =0; I < filelist.size (); i++) -         { -spriteframe* pframe = Ccspriteframecache::getinstance ()Getspriteframebyname (Filelist[i]); +             if(NULL = =pframe) -             { +                 Continue; A             } atAnimation->Addspriteframe (pframe); -         } -  -         //Set Duplicate -animate* animate =animate::create (animation); -repeat* Repeat =repeat::create (animate, cc_repeat_forever); inM_panimatesequence =sequence::create (Repeat, NULL); -M_panimatesequence->retain (); to runaction (m_panimatesequence); +     } -  the     return true; *}

This function is initialized by a sequence of frames. Here are two input parameters: FileList is a list of pictures, and Dura is the interval between each picture. How is skeletal animation? Look at the following code:

1 BOOLGameobject::initarmature (ConstSTD::string& Armaturename,floatScale )2 {3     if(Armaturename.length () <=0)4     {5         return true;6     }7 8M_parmature =cocostudio::armature::create (armaturename);9M_parmature->setposition (Getcontentsize ()/2);TenM_parmature->getanimation ()->play (Globaldata::getinstance ()->getarmaturedata (ArmatureName)defaultaction); OneM_parmature->Setscale (scale); A  - AddChild (m_parmature); -  the     return true; -}

The skeletal animation is created first through the name Armaturename of the skeletal animation, and then the default action DefaultAction (DefaultAction is obtained from the configuration file and the read and write of the configuration file is detailed later). Finally, the skeletal animation is added to the sprite.

Here is a question, why not only support frame sequence animation, but also support skeletal animation it? We know that skeletal animation is more expressive than frame sequence animation, but the problem with it is that skeletal animation is more of a resource. If there is only one simple animation, or a bullet as fast and more than the number of game objects, should try to use the frame sequence animation, and even for bullets, only a single picture to express it, there is no need to animate. And for the player aircraft, boss, because it involves deformation, it will have to use skeletal animation.

In addition, there is a place where you can optimize. We can inherit gameobject from node, and when the game object is a sequence frame animation, add a sprite child node, and if it is a skeletal animation, add a armature child node.

Download Reference Code

Reprint Please specify: http://www.cnblogs.com/thorqq/p/5646509.html

At the beginning of the next chapter, we will introduce the subclass of Gameobject

Imitation "Thunder Fighter" Flying shooter Tour development--gameobject

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.