Introduction to frame animation using Cocos2d-x development examples

Source: Internet
Author: User

Swift IOS swift tutorial cocos2d-x mobile games

Next we will introduce the use of Frame Animation through an example. As shown in this example, click go to start playing the animation. At this time, the title of the play button changes to stop, and click STOP to stop the animation.


Next let's take a look at the specific program code. First, let's take a look at the helloworldscene. h file. Its code is as follows:

# Ifndef _ helloworld_scene_h __# DEFINE _ helloworld_scene_h _ # include "cocos2d. H "class helloworld: Public cocos2d: layer {bool isplaying; // playback ID ① cocos2d: SPRITE * sprite; ② public: static cocos2d: Scene * createscene (); virtual bool Init (); voidonaction (cocos2d: ref * psender); ③ create_func (helloworld) ;};# endif/_ helloworld_scene_h __

The Code in line ① declares a Boolean variable isplaying, which is used to save the playback status. If it is true, the video is being played, and if it is false, the video is stopped. Line 2 code cocos2d: SPRITE * sprite declares an genie variable. Row ③ declares a function for callback when different menus are selected.

Helloworldscene implementation code helloworldscene. CCP file. The helloworld: Init () function code is as follows: bool helloworld: Init () {If (! Layer: Init () {returnfalse;} sizevisiblesize = Director: getinstance ()-> getvisiblesize (); pointorigin = Director: getinstance ()-> getvisibleorigin (); spriteframecache: getinstance ()-> addspriteframeswithfile ("Run. plist "); autobackground = sprite: createwithspriteframename (" background.png "); background-> setanchorpoint (point: zero); this-> addchild (background, 0 ); sprite = sprite: createwithspriteframename ("h1.png"); sprite-> setposition (point (visiblesize. width/2, visiblesize. height/2); this-> addchild (sprite); isplaying = false; // toggle menu autogosprite = sprite: createwithspriteframename ("go.png"); ① autostopsprite = sprite :: parameters ("stop.png"); ② parameters = menuitemsprite: Create (gosprite, gosprite); ③ auto parameters = menuitemsprite: Create (stopsprite, stopsprite); ④ auto togglemenuitem = menuitemtoggle:: createwithcallback (cc_callback_1 (helloworld: onaction, this), gotogglemenuitem, stoptogglemenuitem, null); ⑤ togglemenuitem-> setposition (Director: getinstance () -> converttogl (point (930,540); ⑥ auto Mn = menu: Create (togglemenuitem, null); Mn-> setposition (point: zero ); this-> addchild (Mn); returntrue ;}

The first line of the above Code is the create go Button wizard, and the third line of the Code is the create go button (menu item ). Line ② of the Code is the stop button wizard. Line ④ of the Code corresponds to the stop button (menu item ). In the fifth line of code, creating go and stop are two switching menu items. Line 6 of Code sets the position of the switch menu item.

The helloworldscene implementation code helloworldscene. CCP file. The helloworld: onaction (ref * psender) Function Code is as follows:

Void helloworld: onaction (ref * psender) {If (! Isplaying) {// animation start ////////////////////// animation * animation = Animation:: Create (); ① for (INT I = 1; I <= 4; I ++) {_ string * framename = _ string: createwithformat ("hsf-d.png ", i); ② log ("framename = % s", framename-> getcstring (); spriteframe * spriteframe = spriteframecache: getinstance () -> getspriteframebyname (framename-> getcstring (); ③ animation-> addspriteframe (spriteframe); ④} animation-> setdelayperunit (0.15f ); // set the playback time of two frames. ⑤ animation-> setrestoreoriginalframe (true); // restore the initial state after the animation is executed. ⑥ animate * Action = Animate: Create (animation ); 7. Sprite-> runaction (repeatforever: Create (Action )); else ////////////// // animation end /////////////////// isplaying = true ;} else {sprite-> stopallactions (); required isplaying = false ;}}

The first line of code above is to create an animation object, which is an animation object. Then we need to cyclically place the frames into the animation object. Row ② is the file name for obtaining the frame image, and string is the Cocos2d-x string data type. The Code in line ③ creates a Sprite frame object through the frame name. The Code in line ④ adds the sprite frame object to the animation object.

The fifth line of code is animation-> setdelayperunit (0.15f), which is used to set the playback time of two frames. We play this animation with four frames. Line 6 code animation-> setrestoreoriginalframe (true) indicates whether the animation is restored to the initial state after execution. Line 7 Code creates an animate object through an animation object. Line 7 Code sprite-> runaction (repeatforever: Create (Action) is used to execute an animation action in an infinite loop mode.

The first line of code sprite-> stopallactions () stops all actions.

 

More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): c ++ development" book exchange discussion site: http://www.cOcoagame.netWelcome to cocos2d-x Technology Discussion Group: 257760386, 327403678

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.