quick-cocos2d-x3.3 Study (10)---------Add animations

Source: Internet
Author: User

Note: When adding animations, you must first add the pictures to the cache.

1 -- Add a resource to the sprite frame cache 2 display.addspriteframes ("image/animations/binggu.plist  ""image/animations/binggu.png")

Add animations

Here we use frame animation to achieve the game character movement, the principle of frame animation is the continuous frame image (such as) in the rendering time by frame or interpolation to play out the way the dynamic effect. It's like flipping picture.

In the engine, the specific content of the frame animation is shown by the wizard, in order to display the dynamic picture, we need to constantly switch the content of the sprite display, by turning the static sprite into an animated player to achieve animation effect. Frame animations consist of frames, each frame is a texture, and we can use a texture sequence to create animations.

In the Quick frame, we can play an animation like this:

1--Create an array of 4 image frame objects that contain flying1.png to Flying4.png2Local frames = Display.newframes ("Flying.png",1,4 )3 4--creates an animated Animation object with an array that contains image frames5Local animation = Display.newanimation (frames,0.3/4 )6 7--loops the animation on the display object and returns the action animation action object8Tranisition.playanimationforever (self, animation)

Because our game objects have more than one kind of animation, so in this tutorial, we can first add these animations to the animation cache, so that when we need to play the corresponding animation, we can get the animation directly from the cache to play, instead of wasting time to recreate the animation

So, add the following function to the Player.lua file:

1 function Player:addanimationcache ()2Local animations = {"Flying","Drop"," die" }3Local Animationframenum = {4,3,4 }4  5        fori =0, #animations Do6--17Local frames = Display.newframes (Animations[i].."%d.png",1, Animationframenum[i])8--29Local animation = Display.newanimation (frames,0.3/4 )Ten--3 One Display.setanimationcache (Animations[i], animation) A    - End -   theEnd

Animations,animationframenum represents the total number of frames for each of the three animations and three animations of the character.

When traversing animations, the following one by one explains the function:

    1. Create an array of image frame objects that contain animations[i]1.png to animations[i]animationframenum[i].png, such as I = 1, is to create an array of image frame objects that contain flying1.png to flying4.png. Among them. is a string connection operator that can be used to concatenate two strings. When one is a different type, it also converts the type to a string.
    2. Creates an animated Animation object with an array of image frames, and parameter 0.3/4 means 0.3 seconds to play 4 frames.
    3. The animation object created in 2 is added to the animation cache with the specified name (Animations[i]) for subsequent reuse. That is, we can find the animation object in Animationcache by the names of the three animations such as animations = {"Flying", "drop", "Die"}.

After loading the animation into the cache, we can write down the corresponding function to perform the animation. As follows:

1 function player:flying ()2 transition.stoptarget (self)3Transition.playanimationforever (Self, Display.getanimationcache ("Flying" ) )4 5 End6 7 function Player:drop ()8 transition.stoptarget (self)9Transition.playanimationforever (Self, Display.getanimationcache ("Drop" ) )Ten End One  A function Player:die () - transition.stoptarget (self) -Transition.playanimationforever (Self, Display.getanimationcache (" die" ) ) theEnd

Finally, after creating the Player object, we can call the corresponding method above to play the specified animation.

Instance:

1 function Gamescene:ctor ()2--Add a resource to the sprite frame cache3Display.addspriteframes ("image/animations/binggu.plist","Image/animations/binggu.png")4 5Self.player = Display.newsprite ("#binggu_01. png" )6: Align (display. CENTER, DISPLAY.CX/4, display.cy)7 : AddTo (self)8 9--Create an array of 4 image frame objects that contain flying1.png to Flying4.pngTenLocal frames = Display.newframes ("Binggu_0%d.png",1,7 ) One  A--creates an animated Animation object with an array that contains image frames -Local animation = Display.newanimation (frames,0.3/4 ) -  the--loops the animation on the display object and returns the action animation action object - Transition.playanimationforever (Self.player, animation) -  -End

quick-cocos2d-x3.3 Study (10)---------Add animations

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.