Cocos2d-xna: horizontal strategic game development lab 6 CCAnimate create role Animation

Source: Internet
Author: User

Since its appearance, the game has been increasing the demand of players. Game Developers not only consider simple location movement, such as making various actors in the game into animations, they can greatly improve the quality of the game, in our game of the leaders of the Three Kingdoms, the soldiers and generals have various actions. These actions correspond to animations, making the game more interactive, this time you use the CCAnimate in the cocos2d-xna for role animation.

First, we need to prepare the animation resources required in the game. According to the design, the game should have at least such roles and occupations: Masters, soldiers (infantry, gunshots, cavalry, and bows ), their roles are different because they belong to different forces.

The role animation is named according to a rule to facilitate management:

{Id }_{ n} indicates the positive direction, while {id} f _ {n} indicates the opposite direction. For example

A1 indicates the role id. The displayed actions are as follows:

0-3: attack action

4-5: Walking

6-6: Standing

7-8: Death

In this experiment game, we have two forces, Yi Jun and Huang jiejun, respectively. Following these rules, we have made the following resources:

A1: Yellow towel Army Infantry

A2: Yellow towel army gun

A3: Yellow towel army cavalry

A4: Yellow towel army Gong Bing

B1: Yijun Infantry

B2: yundun Gunner

B3: Yijun cavalry

B4: Yi Jun Gong Bing

In addition, two heroes are added:

Hero02: Guan Yu

Hero11: Zhang Jiao

Then, complete the preparation and naming of their various frames (Here you may need the help of art, I have completed it and can browse it in the final file), and package it with TexturePackerGUI.

Picture bag: http://files.cnblogs.com/nowpaper/SanguoCommander5_actors.rar

Release it and save it as a plist and png Image named ActorsPack1. In the future, there may be Pack2, so save it separately:

Put the final plist file under the project Content, you can create a folder such as plist, and set the Content pipeline of the. plist file correctly:

This image is not merged. You should create a sub-file named imagesand put actorspack1.png in it.

Now, you can start coding the project in the next step.

From the design experience of the game itself, the best way is data-driven logic. Therefore, when we describe a role animation class, there is a clear data class first, here we can include and process the complex data of the underlying game role, such as the most basic features, occupations, and types of the role:

Enum ActorType {None, Soldier, Hero} enum ActorPro {None, Infantry, Pikeman, Cavalvy, Archer} enum ActorDir {None, Right, Left} class ActorData {public ActorData (string id) {ActorID = id;} // actor id public string ActorID {get; private set;} // actor group public string GroupID {get; private set ;} // type public ActorType {get; private set ;}// occupation public ActorPro {get; private set ;}// obtain public static ActorData getActorData (string id, string groupid, ActorType type, ActorPro pro) {ActorData data = new ActorData (id); data. groupID = groupid; data. actorType = type; data. actorPro = pro; return data ;}}

At the top of the list, we define three enumeration types to indicate the type, occupation, and direction. The direction is used for animation, and the type and occupation are the basic data requirements, you can note that a static getActorData method is designed to easily create basic test data, because according to the practice of game data processing, the data is configured in a form, the program reads the configuration and parses it into program data, making it easy for game designers to adjust it at any time.

The next step is to create an animation class. We use the class inheritance method to abstract the animation class and create an ActorBase class. This class only manages the most basic animation of a role, inherits from CCSprite, and controls the animation through the CCAnimate action:

Class ActorBase: CCSprite {public ActorData {get; private set;} private CCAnimate _ action_attack; private CCAnimate _ ALLOW; private CCAnimate _ action_run; private CCAnimate _ ALLOW; private CCAnimate _ action_stand; private CCAnimate _ action_stand_flip; private CCAnimate _ action_dead; private CCAnimate _ action_dead_flip; public ActorDir {get; set;} publi C ActorBase (ActorData data) {ActorData = data; // create an attack animation List _ attackFrames = new List (); List _ attackFrames_flip = new List (); for (int I = 0; I <4; I ++) {_ attackFrames. add (CCSpriteFrameCache. sharedSpriteFrameCache (). spriteFrameByName (data. actorID + "_" + I + ". png "); _ attackFrames_flip.Add (CCSpriteFrameCache. sharedSpriteFrameCache (). spriteFrameByName (data. actorID + "f _" + I + ". png ");} _ act Ion_attack = CCAnimate. actionWithAnimation (CCAnimation. animationWithFrames (_ attackFrames, 0.1f); _ action_attack_flip = CCAnimate. actionWithAnimation (CCAnimation. animationWithFrames (_ attackFrames_flip, 0.1f); // create a walking animation List _ runFrames = new List (); List _ runFrames_flip = new List (); for (int I = 4; I <6; I ++) {_ runFrames. add (CCSpriteFrameCache. sharedSpriteFrameCache (). spriteFrameByName (data. acto RID + "_" + I + ". png "); _ runFrames_flip.Add (CCSpriteFrameCache. sharedSpriteFrameCache (). spriteFrameByName (data. actorID + "f _" + I + ". png ");} _ action_run = CCAnimate. actionWithAnimation (CCAnimation. animationWithFrames (_ runFrames, 0.1f); _ action_run_flip = CCAnimate. actionWithAnimation (CCAnimation. animationWithFrames (_ runFrames_flip, 0.1f); // create a standing animation List _ standFrames = new List (); List _ stand Frames_flip = new List (); for (int I = 6; I <7; I ++) {_ standFrames. add (CCSpriteFrameCache. sharedSpriteFrameCache (). spriteFrameByName (data. actorID + "_" + I + ". png "); _ standFrames_flip.Add (CCSpriteFrameCache. sharedSpriteFrameCache (). spriteFrameByName (data. actorID + "f _" + I + ". png ");} _ action_stand = CCAnimate. actionWithAnimation (CCAnimation. animationWithFrames (_ standFrames, 0.2f); _ action _ Stand_flip = CCAnimate. actionWithAnimation (CCAnimation. animationWithFrames (_ standFrames_flip, 0.2f); // create a death animation List _ deadFrames = new List (); List _ deadFrames_flip = new List (); for (int I = 7; I <9; I ++) {_ deadFrames. add (CCSpriteFrameCache. sharedSpriteFrameCache (). spriteFrameByName (data. actorID + "_" + I + ". png "); _ deadFrames_flip.Add (CCSpriteFrameCache. sharedSpriteFrameCache (). spriteFrameByN Ame (data. actorID + "f _" + I + ". png ");} _ action_dead = CCAnimate. actionWithAnimation (CCAnimation. animationWithFrames (_ deadFrames, 0.3f); _ action_dead_flip = CCAnimate. actionWithAnimation (CCAnimation. animationWithFrames (_ deadFrames_flip, 0.3f); // initialize the default frame base. initWithSpriteFrame (_ standFrames [0]);} CCAction _ currentAnimateAction; public void StateToRun () {if (ActorDir = Roles. actorDir. left) Run AnimateAction_RepeatForever (_ action_run); else response (_ action_run_flip);} // Attack Status public void StateToAttack () {currentAnimateActionStop (); if (ActorDir = Roles. actorDir. left) terminate (_ action_attack); else RunAnimateAction_RepeatForever (_ action_attack_flip);} // death animation public void StateToDead () {currentAnimateActionStop (); if (ActorDir = Roles. actorD Ir. left) _ currentAnimateAction = runAction (_ action_dead); else _ currentAnimateAction = runAction (_ action_dead_flip);} // The Standing animation public void StateToStand () {if (ActorDir = Roles. actorDir. left) terminate (_ action_stand); else RunAnimateAction_RepeatForever (_ action_stand_flip);} // stop the current animation private void currentAnimateActionStop () {if (_ currentAnimateAction! = Null) this. stopAction (_ currentAnimateAction);} // unified playback loop animation method private void RunAnimateAction_RepeatForever (CCAnimate action) {currentAnimateActionStop (); _ currentAnimateAction = runAction (CCRepeatForever. actionWithAction (action ));}}

The above Code adds some comments, hoping to help you read the code. The animation production process is as follows:

First, we need to know the frames which form the set into the processing class of CCAnimation, and then CCAnimate loads it and forms a specific animation behavior, if you are interested, you can view the underlying code of cocos2d. CCSprite actually carries a CCTexture to represent the image, and CCAnimate is a logical change of CCTexture.

In the following code: StateToRun, StateToAttack, StateToDead, StateToStand, and other methods are used to process the animation of the role State. For example, the StateToAttack method is called when an attack occurs.

CurrentAnimateActionStop and RunAnimateAction_RepeatForever are used to conveniently process the specific state of an animation. because such animations are cyclic, such as walking, standing, and attacking, unified code is more convenient.

Then let's test the above Code to see the direct effect, or for convenience. In this section, we directly place the animation on the Start interface, which makes browsing very convenient. Open SceneStart. cs, write the following code in the constructor:

// List id_buff = new List () {"B1", "B2", "B3", "B4", "Hero02", "A1 ", "A2", "A3", "A4", "Hero11"}; for (int I = 0; I <2; I ++) {for (int j = 0; j <5; j ++) {var actor = new ActorBase (new ActorData (id_buff [I * 5 + j]); actor. actorDir = (ActorDir) (I + 1); actor. position = new CCPoint (64 + I * 64, 64 + j * 64); if (j % 2 = 1) actor. stateToRun (); else actor. stateToAttack (); this. addChild (actor );}}//

In the code above, an id_buff in the List structure is used to describe the ID, which is actually the prefix name of the role, which is then arranged in two columns in order and is animated by the singular number, dual numbers are displayed as attack animations.

The above shows the effect of running the test, and we will add them to the game interface to make the game really ready to play.

This example project: https://github.com/Nowpaper/SanguoCommander_cocos2dxna_Sample
In this example, the project name is SanguoCommander6.

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.