Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please tell me, if you feel good please support a lot of praise. Thank you! Hopy;)
Disclaimer: All translations provided by this blog are from the Internet and are for learning and communication purposes only. Also, do not remove this statement when reproduced. In case of any dispute, there is no relationship between the blogger and the person who published the translation. Thank you for your cooperation!
In the previous blog post, we created the basics of the game-the little cute gopher that emerged from the hole. We spent a lot of time thinking about how to organize the footage and coordinate the placement to make our game look better and more efficient on the Iphone,ipad and HD screens.
In the following blog post, we will add some interesting animations to the laughter of the hamster and the expression of K, adding the game logic so that you can tap and score, and of course have some essential sound effects.
If you do not have a copy of the project, you can download the source code for the grab a copy from here.
Defining animations: Usability
In order to make the game more interesting, we will make 2 animations for the Gopher. First, it will have a slight smile when it pops up (making you want to beat it more), and if you hit it, it will change a "knocked" face for you to see.
But before we get started, let's discuss the usefulness of defining animations in code.
Recall cocos2d Animations Tutorial one step in creating an animation is to create a list of Sprite frames. For each picture in your animation, you must add the sprite frame to an array as follows:
[animFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"myImage.png"]];
The picture order of our hamster giggle animation is this:
Mole_laugh1.png,mole_laugh2.png,mole_augh3.png,mole_augh2.png,mole_augh3.png,mole_laugh1.png.
We can hard code to write so many lines into the animated code, like this:
[animFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh1.png"]];[animFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh2.png"]];[animFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh3.png"]];[animFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mole_laugh2.png"]];// And so on...
But it's not so beautiful. To be clear, we prefer to use a property list instead of the hard code above.
Attribute list (Property Lists)
If you've never used a property list, in Xcode they're like a hierarchical file that contains data (such as arrays, dictionaries, strings, numbers, and so on), and creating it is very simple and easy to read from the code.
Let's see how it's done in Xcode. Right-click Resources, choose Add\new File ... ", select Mac OS x\resource\property List, click Next. Name the new file as Laughanim.plist, Click End. The laughanim.plist in the editor should look like this:
Each attribute list has a root element. It is often an array or a dictionary. The list of attributes in this example will contain an array, each element is a picture name that makes up a smile animation, so click on the second column of the root element (type, which is currently a dictionary) and change it to an array type.
Next, the rightmost small button-adds a new entry to the array, by default the entry type is string-which is exactly what we want. Change its name to "Mole_laugh1.png".
Click the + button to add a new line, and then repeat all the frames of the animation, such as:
Next, repeat the process to the animation when the gopher is knocked. Create a new property file name is called Hitanim.plist, and then set it like so:
Now it's time to add code to load these animations. Open HelloWorldScene.h and then add a member variable for each animation, as follows:
@interface*laughAnim*hitAnim;
This is done by keeping a reference in your code that is easy to access and reuse for each animation.
Add a method below to create a ccanimation based on the picture in the attribute list:
-(Ccanimation *) Animationfromplist: (NSString*) animplist delay: (float) Delay {NSString*plistpath = [[NSBundleMainbundle] Pathforresource:animplist oftype:@"Plist"];//1 Nsarray*animimages = [NsarrayArraywithcontentsoffile:plistpath];//2 Nsmutablearray*animframes = [NsmutablearrayArray];//3 for(NSString*animimage in Animimages) {//4[Animframes Addobject:[[ccspriteframecache Sharedspriteframecache] spriteframebyname:animimage];//5}return[Ccanimation animationwithframes:animframes Delay:delay];//6}
cocos2d iOS Tour: How to write a gopher game (eight): Create a property list for animations