Quick-cocos2d-x game development [8] -- animation and action, cocos2dx skeleton Animation

Source: Internet
Author: User

Quick-cocos2d-x game development [8] -- animation and action, cocos2dx skeleton Animation

Animations and actions are encapsulated in quick, so let's take a look.

In general, we can often use the quick Encapsulation Method for Frame Animation, which is very convenient. The following code is used to intuitively feel the situation,

For example, 14 frames of images, the use of cocos2d-x lua method to write is like this,

 local sp = display.newSprite("grossini_dance_01.png", display.cx, display.cy)    self:addChild(sp)        local animation = CCAnimation:create()    local number, name    for i = 1, 14 do        if i < 10 then            number = "0"..i        else            number = i        end        name = "grossini_dance_"..number..".png"        animation:addSpriteFrameWithFileName(name)    end    animation:setDelayPerUnit(2.8 / 14.0)    local action = CCAnimate:create(animation)    sp:runAction(action)

Each frame needs to be added to CCAnimation, which is the same as C ++, but quick is used in this way,

Display. addSpriteFramesWithFile ("hero. plist "," hero.png ") -- add the frame cache local sp = display. newSprite ("maid", display. cx, display. cy) self: addChild (sp) local frames = display. newFrames ("grossini_dance_10902d.png", 1, 14) local animation = display. newAnimation (frames, 2.8/14.0) sp: playAnimationOnce (animation)

The meanings of parameters of display. newFrames (pattern, begin, length, isReversed) are,

  • StringPatternMode string
  • IntegerBeginStart Index
  • IntegerLengthLength
  • BooleanIsReversedWhether it is a descending Index
Note that the image name in newFrames must be the image name in the frame cache. In other words, we need to use the image packaging tool to process the image, if multiple images are used, they certainly cannot be used. You can think of it that we will use image packaging tools to process images later, so quick will encapsulate this method directly.

If you don't believe it, you can check the source code of this function,

function display.newFrames(pattern, begin, length, isReversed)    local frames = {}    local step = 1    local last = begin + length - 1    if isReversed then        last, begin = begin, last        step = -1    end    for index = begin, last, step do        local frameName = string.format(pattern, index)        local frame = sharedSpriteFrameCache:spriteFrameByName(frameName)        if not frame then            printError("display.newFrames() - invalid frame, name %s", tostring(frameName))            return        end        frames[#frames + 1] = frame    end    return framesend


Directly call the spriteFrameByName function.


For playing an animation, quick provides two functions for the Sprite genie class,

function Sprite:playAnimationOnce(animation, removeWhenFinished, onComplete, delay)    return transition.playAnimationOnce(self, animation, removeWhenFinished, onComplete, delay)endfunction Sprite:playAnimationForever(animation, delay)    return transition.playAnimationForever(self, animation, delay)end

One is to play the animation once, and the other is to play the animation permanently. Easy to use!


The above is the animation usage. Next we will look at the use of the action,

The class encapsulated by the action is transition, which provides these functions,

Transition. newEasing (action, easingName, more)
Create effects for images
Transition.exe cute (target, action, args)
Execute an action
Transition. rotateTo (target, args)
Rotate the displayed object to the specified angle and return the CCAction object.
Transition. moveTo (target, args)
Move the displayed object to the specified position and return the CCAction object.
Transition. fadeTo (target, args)
The transparency of the displayed object is changed to a specified value, and the CCAction action object is returned.
Transition. scaleTo (target, args)
Scales the displayed object to a specified scale and returns the CCAction object.
Transition. sequence (actions)
Create an action sequence object.
Transition. playAnimationOnce (target, animation, removeWhenFinished, onComplete, delay)
Play an animation on the displayed object and return the CCAction object.

I think the single actions like move, scale, and fade are easy to remember and use, for example, it is quite good to use CCMoveTo for mobile. However, what I think is really good about quick encapsulation is,

Transition.exe cute (target, action, args)

Transition. sequence (actions)

These two, why? Let's see,


Transition.exe cute () is a powerful tool that can add various additional features for a single action.

The parameter table of transition.exe cute () supports the following parameters:

  • Delay: how long will the execution start?
  • Easing: the name of the easing effect and optional additional parameters. The effect name is case insensitive.
  • OnComplete: the function to be called after the action is executed.
  • Time: the time required to execute the action.

The following describes the easing effect supported by transition.exe cute:

  • BackIn
  • BackInOut
  • BackOut
  • Bounce
  • BounceIn
  • BounceInOut
  • BounceOut
  • Elastic. The default value of the additional parameter is 0.3.
  • ElasticIn. The default value of additional parameters is 0.3.
  • ElasticInOut. The default value of additional parameters is 0.3.
  • ElasticOut. The default value of the additional parameter is 0.3.
  • ExponentialIn. The default value of the additional parameter is 1.0.
  • ExponentialInOut. The default value of the additional parameter is 1.0.
  • ExponentialOut. The default value of the additional parameter is 1.0.
  • In. The default value of the additional parameter is 1.0.
  • InOut. The default value of the additional parameter is 1.0.
  • Out. The default value of the additional parameter is 1.0.
  • Rateaction. The default value of the additional parameter is 1.0.
  • SineIn
  • SineInOut
  • SineOut
This function can complete the speed effect in motion, as well as CCCallFunc, CCDelayTime and other functions. If you append them together, you don't need to write tedious function nesting and CCSequence. Liao Dazhen wrote it to my heart. Like this,

transition.execute(sprite, CCMoveTo:create(1.5, CCPoint(display.cx, display.cy)), {    delay = 1.0,    easing = "backout",    onComplete = function()        print("move completed")    end,})

Transition. sequence is also a convenient function. If multiple actions are executed in sequence before,

 local move1  = CCMoveBy:create(1, ccp(250,0))    local move2  = CCMoveBy:create(1, ccp(0,50))    local array  = CCArray:createWithCapacity(2)    array:addObject(move1)    array:addObject(move2)    local seq = CCSequence:create(array)

Each action must be packed in an array before a CCSequence can be created. But now,

local sequence = transition.sequence({        CCMoveBy:create(1, ccp(250,0)),        CCMoveBy:create(1, ccp(0,50))    })

Just like C ++, you can create and add them one by one, which is very convenient ~


The above is all content.


Animation problem of Flash 8 Motion Compensation room

Right-click to create a compensation animation is an action for a frame you have made. The component means that this animation is an animated film and video editing and is a whole, let's take a look at the library on the right. It exists as an animation, and clicking it is dynamic.
On the contrary, setting an animation on the frame attribute is only for this frame. It is in a State and does not represent the state of the entire animation. You can understand it slowly. Generally, when performing an animation, first, convert the object into a component, and then right-click it to make a compensation animation.

Suitable for cartoons, books, and games watched by boys aged 7 and 8

The most classic cartoon is the American cartoon "cat and mouse". Although the cartoon company has been doing this for a long time, the cartoon "cat and mouse" is constantly making new cartoons. And Doraemon make us unforgettable, and transformers, which accompanied our childhood, are very suitable for boys.
What kind of books should I look.
Ah! It turns out that this question is 0 points! So what should I do. The minimum consumption is also 5 points! You can use the reward enhancement function to raise the reward rate by 5 points, and then use the question inquiry function to ask me about the game, or you will not tell the game

Related Article

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.